FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-gitlab/tests/unit/test_base.py at jlvillal/devel_docs · python-gitlab/python-gitlab · GitHub
python-gitlab
python-gitlab
Repository navigation
Code
Issues
16
(16)
Pull requests
15
(15)
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-gitlab
/
tests
/
unit
/
test_base.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
203 lines (158 loc) · 6.66 KB
Breadcrumbs
python-gitlab
/
tests
/
unit
/
test_base.py
Copy path
File metadata and controls
203 lines (158 loc) · 6.66 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 Gauvain Pocentek <gauvain@pocentek.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import
pickle
import
pytest
import
gitlab
from
gitlab
import
base
class
FakeGitlab
(
object
):
pass
class
FakeObject
(
base
.
RESTObject
):
pass
class
FakeManager
(
base
.
RESTManager
):
_obj_cls
=
FakeObject
_path
=
"/tests"
@
pytest
.
fixture
def
fake_gitlab
():
return
FakeGitlab
()
@
pytest
.
fixture
def
fake_manager
(
fake_gitlab
):
return
FakeManager
(
fake_gitlab
)
class
TestRESTManager
:
def
test_computed_path_simple
(
self
):
class
MGR
(
base
.
RESTManager
):
_path
=
"/tests"
_obj_cls
=
object
mgr
=
MGR
(
FakeGitlab
())
assert
mgr
.
_computed_path
==
"/tests"
def
test_computed_path_with_parent
(
self
):
class
MGR
(
base
.
RESTManager
):
_path
=
"/tests/{test_id}/cases"
_obj_cls
=
object
_from_parent_attrs
=
{
"test_id"
:
"id"
}
class
Parent
(
object
):
id
=
42
mgr
=
MGR
(
FakeGitlab
(),
parent
=
Parent
())
assert
mgr
.
_computed_path
==
"/tests/42/cases"
def
test_path_property
(
self
):
class
MGR
(
base
.
RESTManager
):
_path
=
"/tests"
_obj_cls
=
object
mgr
=
MGR
(
FakeGitlab
())
assert
mgr
.
path
==
"/tests"
class
TestRESTObject
:
def
test_instantiate
(
self
,
fake_gitlab
,
fake_manager
):
obj
=
FakeObject
(
fake_manager
, {
"foo"
:
"bar"
})
assert
{
"foo"
:
"bar"
}
==
obj
.
_attrs
assert
{}
==
obj
.
_updated_attrs
assert
obj
.
_create_managers
()
is
None
assert
fake_manager
==
obj
.
manager
assert
fake_gitlab
==
obj
.
manager
.
gitlab
def
test_instantiate_non_dict
(
self
,
fake_gitlab
,
fake_manager
):
with
pytest
.
raises
(
gitlab
.
exceptions
.
GitlabParsingError
):
FakeObject
(
fake_manager
, [
"a"
,
"list"
,
"fails"
])
def
test_missing_attribute_does_not_raise_custom
(
self
,
fake_gitlab
,
fake_manager
):
"""Ensure a missing attribute does not raise our custom error message
if the RESTObject was not created from a list"""
obj
=
FakeObject
(
manager
=
fake_manager
,
attrs
=
{
"foo"
:
"bar"
})
with
pytest
.
raises
(
AttributeError
)
as
excinfo
:
obj
.
missing_attribute
exc_str
=
str
(
excinfo
.
value
)
assert
"missing_attribute"
in
exc_str
assert
"was created via a list()"
not
in
exc_str
assert
base
.
_URL_ATTRIBUTE_ERROR
not
in
exc_str
def
test_missing_attribute_from_list_raises_custom
(
self
,
fake_gitlab
,
fake_manager
):
"""Ensure a missing attribute raises our custom error message if the
RESTObject was created from a list"""
obj
=
FakeObject
(
manager
=
fake_manager
,
attrs
=
{
"foo"
:
"bar"
},
created_from_list
=
True
)
with
pytest
.
raises
(
AttributeError
)
as
excinfo
:
obj
.
missing_attribute
exc_str
=
str
(
excinfo
.
value
)
assert
"missing_attribute"
in
exc_str
assert
"was created via a list()"
in
exc_str
assert
base
.
_URL_ATTRIBUTE_ERROR
in
exc_str
def
test_picklability
(
self
,
fake_manager
):
obj
=
FakeObject
(
fake_manager
, {
"foo"
:
"bar"
})
original_obj_module
=
obj
.
_module
pickled
=
pickle
.
dumps
(
obj
)
unpickled
=
pickle
.
loads
(
pickled
)
assert
isinstance
(
unpickled
,
FakeObject
)
assert
hasattr
(
unpickled
,
"_module"
)
assert
unpickled
.
_module
==
original_obj_module
pickle
.
dumps
(
unpickled
)
def
test_attrs
(
self
,
fake_manager
):
obj
=
FakeObject
(
fake_manager
, {
"foo"
:
"bar"
})
assert
"bar"
==
obj
.
foo
with
pytest
.
raises
(
AttributeError
):
getattr
(
obj
,
"bar"
)
obj
.
bar
=
"baz"
assert
"baz"
==
obj
.
bar
assert
{
"foo"
:
"bar"
}
==
obj
.
_attrs
assert
{
"bar"
:
"baz"
}
==
obj
.
_updated_attrs
def
test_get_id
(
self
,
fake_manager
):
obj
=
FakeObject
(
fake_manager
, {
"foo"
:
"bar"
})
obj
.
id
=
42
assert
42
==
obj
.
get_id
()
obj
.
id
=
None
assert
obj
.
get_id
()
is
None
def
test_custom_id_attr
(
self
,
fake_manager
):
class
OtherFakeObject
(
FakeObject
):
_id_attr
=
"foo"
obj
=
OtherFakeObject
(
fake_manager
, {
"foo"
:
"bar"
})
assert
"bar"
==
obj
.
get_id
()
def
test_update_attrs
(
self
,
fake_manager
):
obj
=
FakeObject
(
fake_manager
, {
"foo"
:
"bar"
})
obj
.
bar
=
"baz"
obj
.
_update_attrs
({
"foo"
:
"foo"
,
"bar"
:
"bar"
})
assert
{
"foo"
:
"foo"
,
"bar"
:
"bar"
}
==
obj
.
_attrs
assert
{}
==
obj
.
_updated_attrs
def
test_update_attrs_deleted
(
self
,
fake_manager
):
obj
=
FakeObject
(
fake_manager
, {
"foo"
:
"foo"
,
"bar"
:
"bar"
})
obj
.
bar
=
"baz"
obj
.
_update_attrs
({
"foo"
:
"foo"
})
assert
{
"foo"
:
"foo"
}
==
obj
.
_attrs
assert
{}
==
obj
.
_updated_attrs
def
test_dir_unique
(
self
,
fake_manager
):
obj
=
FakeObject
(
fake_manager
, {
"manager"
:
"foo"
})
assert
len
(
dir
(
obj
))
==
len
(
set
(
dir
(
obj
)))
def
test_create_managers
(
self
,
fake_gitlab
,
fake_manager
):
class
ObjectWithManager
(
FakeObject
):
fakes
:
"FakeManager"
obj
=
ObjectWithManager
(
fake_manager
, {
"foo"
:
"bar"
})
obj
.
id
=
42
assert
isinstance
(
obj
.
fakes
,
FakeManager
)
assert
obj
.
fakes
.
gitlab
==
fake_gitlab
assert
obj
.
fakes
.
_parent
==
obj
def
test_equality
(
self
,
fake_manager
):
obj1
=
FakeObject
(
fake_manager
, {
"id"
:
"foo"
})
obj2
=
FakeObject
(
fake_manager
, {
"id"
:
"foo"
,
"other_attr"
:
"bar"
})
assert
obj1
==
obj2
def
test_equality_custom_id
(
self
,
fake_manager
):
class
OtherFakeObject
(
FakeObject
):
_id_attr
=
"foo"
obj1
=
OtherFakeObject
(
fake_manager
, {
"foo"
:
"bar"
})
obj2
=
OtherFakeObject
(
fake_manager
, {
"foo"
:
"bar"
,
"other_attr"
:
"baz"
})
assert
obj1
==
obj2
def
test_inequality
(
self
,
fake_manager
):
obj1
=
FakeObject
(
fake_manager
, {
"id"
:
"foo"
})
obj2
=
FakeObject
(
fake_manager
, {
"id"
:
"bar"
})
assert
obj1
!=
obj2
def
test_inequality_no_id
(
self
,
fake_manager
):
obj1
=
FakeObject
(
fake_manager
, {
"attr1"
:
"foo"
})
obj2
=
FakeObject
(
fake_manager
, {
"attr1"
:
"bar"
})
assert
obj1
!=
obj2
Back
|
FazBrowse Home
|
New Git URL