|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2017 Gauvain Pocentek <gauvain@pocentek.net> |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU Lesser General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +try: |
| 19 | +importunittest |
| 20 | +exceptImportError: |
| 21 | +importunittest2asunittest |
| 22 | + |
| 23 | +fromgitlabimportbase |
| 24 | + |
| 25 | + |
| 26 | +classFakeGitlab(object): |
| 27 | +pass |
| 28 | + |
| 29 | + |
| 30 | +classFakeObject(base.RESTObject): |
| 31 | +pass |
| 32 | + |
| 33 | + |
| 34 | +classFakeManager(base.RESTManager): |
| 35 | +_obj_cls=FakeObject |
| 36 | +_path='/tests' |
| 37 | + |
| 38 | + |
| 39 | +classTestRESTManager(unittest.TestCase): |
| 40 | +deftest_computed_path_simple(self): |
| 41 | +classMGR(base.RESTManager): |
| 42 | +_path='/tests' |
| 43 | +_obj_cls=object |
| 44 | + |
| 45 | +mgr=MGR(FakeGitlab()) |
| 46 | +self.assertEqual(mgr._computed_path,'/tests') |
| 47 | + |
| 48 | +deftest_computed_path_with_parent(self): |
| 49 | +classMGR(base.RESTManager): |
| 50 | +_path='/tests/%(test_id)s/cases' |
| 51 | +_obj_cls=object |
| 52 | +_from_parent_attrs= {'test_id':'id'} |
| 53 | + |
| 54 | +classParent(object): |
| 55 | +id=42 |
| 56 | + |
| 57 | +classBrokenParent(object): |
| 58 | +no_id=0 |
| 59 | + |
| 60 | +mgr=MGR(FakeGitlab(),parent=Parent()) |
| 61 | +self.assertEqual(mgr._computed_path,'/tests/42/cases') |
| 62 | + |
| 63 | +self.assertRaises(AttributeError,MGR,FakeGitlab(), |
| 64 | +parent=BrokenParent()) |
| 65 | + |
| 66 | +deftest_path_property(self): |
| 67 | +classMGR(base.RESTManager): |
| 68 | +_path='/tests' |
| 69 | +_obj_cls=object |
| 70 | + |
| 71 | +mgr=MGR(FakeGitlab()) |
| 72 | +self.assertEqual(mgr.path,'/tests') |
| 73 | + |
| 74 | + |
| 75 | +classTestRESTObject(unittest.TestCase): |
| 76 | +defsetUp(self): |
| 77 | +self.gitlab=FakeGitlab() |
| 78 | +self.manager=FakeManager(self.gitlab) |
| 79 | + |
| 80 | +deftest_instanciate(self): |
| 81 | +obj=FakeObject(self.manager, {'foo':'bar'}) |
| 82 | + |
| 83 | +self.assertDictEqual({'foo':'bar'},obj._attrs) |
| 84 | +self.assertDictEqual({},obj._updated_attrs) |
| 85 | +self.assertEqual(None,obj._create_managers()) |
| 86 | +self.assertEqual(self.manager,obj.manager) |
| 87 | +self.assertEqual(self.gitlab,obj.manager.gitlab) |
| 88 | + |
| 89 | +deftest_attrs(self): |
| 90 | +obj=FakeObject(self.manager, {'foo':'bar'}) |
| 91 | + |
| 92 | +self.assertEqual('bar',obj.foo) |
| 93 | +self.assertRaises(AttributeError,getattr,obj,'bar') |
| 94 | + |
| 95 | +obj.bar='baz' |
| 96 | +self.assertEqual('baz',obj.bar) |
| 97 | +self.assertDictEqual({'foo':'bar'},obj._attrs) |
| 98 | +self.assertDictEqual({'bar':'baz'},obj._updated_attrs) |
| 99 | + |
| 100 | +deftest_get_id(self): |
| 101 | +obj=FakeObject(self.manager, {'foo':'bar'}) |
| 102 | +obj.id=42 |
| 103 | +self.assertEqual(42,obj.get_id()) |
| 104 | + |
| 105 | +obj.id=None |
| 106 | +self.assertEqual(None,obj.get_id()) |
| 107 | + |
| 108 | +deftest_custom_id_attr(self): |
| 109 | +classOtherFakeObject(FakeObject): |
| 110 | +_id_attr='foo' |
| 111 | + |
| 112 | +obj=OtherFakeObject(self.manager, {'foo':'bar'}) |
| 113 | +self.assertEqual('bar',obj.get_id()) |
| 114 | + |
| 115 | +deftest_update_attrs(self): |
| 116 | +obj=FakeObject(self.manager, {'foo':'bar'}) |
| 117 | +obj.bar='baz' |
| 118 | +obj._update_attrs({'foo':'foo','bar':'bar'}) |
| 119 | +self.assertDictEqual({'foo':'foo','bar':'bar'},obj._attrs) |
| 120 | +self.assertDictEqual({},obj._updated_attrs) |
| 121 | + |
| 122 | +deftest_create_managers(self): |
| 123 | +classObjectWithManager(FakeObject): |
| 124 | +_managers= (('fakes','FakeManager'), ) |
| 125 | + |
| 126 | +obj=ObjectWithManager(self.manager, {'foo':'bar'}) |
| 127 | +self.assertIsInstance(obj.fakes,FakeManager) |
| 128 | +self.assertEqual(obj.fakes.gitlab,self.gitlab) |
| 129 | +self.assertEqual(obj.fakes._parent,obj) |