Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0d94ee2

Browse files
author
Gauvain Pocentek
committed
Unit tests for REST* classes
1 parenta5b39a5 commit0d94ee2

File tree

2 files changed

+140
-4
lines changed

2 files changed

+140
-4
lines changed

‎gitlab/base.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,8 @@ class RESTObject(object):
540540
another. This allows smart updates, if the object allows it.
541541
542542
You can redefine ``_id_attr`` in child classes to specify which attribute
543-
must be used as uniq ID. None means that the object can be updated without
544-
ID in the url.
543+
must be used as uniq ID.``None`` means that the object can be updated
544+
withoutID in the url.
545545
"""
546546
_id_attr='id'
547547

@@ -594,8 +594,8 @@ def _create_managers(self):
594594
self.__dict__[attr]=manager
595595

596596
def_update_attrs(self,new_attrs):
597-
self._updated_attrs= {}
598-
self._attrs.update(new_attrs)
597+
self.__dict__['_updated_attrs']= {}
598+
self.__dict__['_attrs'].update(new_attrs)
599599

600600
defget_id(self):
601601
ifself._id_attrisNone:
@@ -649,6 +649,13 @@ class RESTManager(object):
649649
_obj_cls=None
650650

651651
def__init__(self,gl,parent=None):
652+
"""REST manager constructor.
653+
654+
Args:
655+
gl (Gitlab): :class:`~gitlab.Gitlab` connection to use to make
656+
requests.
657+
parent: REST object to which the manager is attached.
658+
"""
652659
self.gitlab=gl
653660
self._parent=parent# for nested managers
654661
self._computed_path=self._compute_path()

‎gitlab/tests/test_base.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp