Skip to content

Commit 0d94ee2

Browse files
author
Gauvain Pocentek
committed
Unit tests for REST* classes
1 parent a5b39a5 commit 0d94ee2

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+
without ID 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
def get_id(self):
601601
if self._id_attr is None:
@@ -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+
import unittest
20+
except ImportError:
21+
import unittest2 as unittest
22+
23+
from gitlab import base
24+
25+
26+
class FakeGitlab(object):
27+
pass
28+
29+
30+
class FakeObject(base.RESTObject):
31+
pass
32+
33+
34+
class FakeManager(base.RESTManager):
35+
_obj_cls = FakeObject
36+
_path = '/tests'
37+
38+
39+
class TestRESTManager(unittest.TestCase):
40+
def test_computed_path_simple(self):
41+
class MGR(base.RESTManager):
42+
_path = '/tests'
43+
_obj_cls = object
44+
45+
mgr = MGR(FakeGitlab())
46+
self.assertEqual(mgr._computed_path, '/tests')
47+
48+
def test_computed_path_with_parent(self):
49+
class MGR(base.RESTManager):
50+
_path = '/tests/%(test_id)s/cases'
51+
_obj_cls = object
52+
_from_parent_attrs = {'test_id': 'id'}
53+
54+
class Parent(object):
55+
id = 42
56+
57+
class BrokenParent(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+
def test_path_property(self):
67+
class MGR(base.RESTManager):
68+
_path = '/tests'
69+
_obj_cls = object
70+
71+
mgr = MGR(FakeGitlab())
72+
self.assertEqual(mgr.path, '/tests')
73+
74+
75+
class TestRESTObject(unittest.TestCase):
76+
def setUp(self):
77+
self.gitlab = FakeGitlab()
78+
self.manager = FakeManager(self.gitlab)
79+
80+
def test_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+
def test_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+
def test_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+
def test_custom_id_attr(self):
109+
class OtherFakeObject(FakeObject):
110+
_id_attr = 'foo'
111+
112+
obj = OtherFakeObject(self.manager, {'foo': 'bar'})
113+
self.assertEqual('bar', obj.get_id())
114+
115+
def test_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+
def test_create_managers(self):
123+
class ObjectWithManager(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)