|
| 1 | +importpickle |
| 2 | + |
| 3 | +importpytest |
| 4 | + |
| 5 | +importgitlab |
| 6 | +fromgitlabimportbase |
| 7 | +fromtests.unitimporthelpers |
| 8 | +fromtests.unit.helpersimportFakeManager# noqa: F401, needed for _create_managers |
| 9 | + |
| 10 | + |
| 11 | +deftest_instantiate(gl,fake_manager): |
| 12 | +attrs= {"foo":"bar"} |
| 13 | +obj=helpers.FakeObject(fake_manager,attrs.copy()) |
| 14 | + |
| 15 | +assertattrs==obj._attrs |
| 16 | +assert {}==obj._updated_attrs |
| 17 | +assertobj._create_managers()isNone |
| 18 | +assertfake_manager==obj.manager |
| 19 | +assertgl==obj.manager.gitlab |
| 20 | +assertstr(obj)==f"{type(obj)} =>{attrs}" |
| 21 | + |
| 22 | + |
| 23 | +deftest_instantiate_non_dict(gl,fake_manager): |
| 24 | +withpytest.raises(gitlab.exceptions.GitlabParsingError): |
| 25 | +helpers.FakeObject(fake_manager, ["a","list","fails"]) |
| 26 | + |
| 27 | + |
| 28 | +deftest_missing_attribute_does_not_raise_custom(gl,fake_manager): |
| 29 | +"""Ensure a missing attribute does not raise our custom error message |
| 30 | + if the RESTObject was not created from a list""" |
| 31 | +obj=helpers.FakeObject(manager=fake_manager,attrs={"foo":"bar"}) |
| 32 | +withpytest.raises(AttributeError)asexcinfo: |
| 33 | +obj.missing_attribute |
| 34 | +exc_str=str(excinfo.value) |
| 35 | +assert"missing_attribute"inexc_str |
| 36 | +assert"was created via a list()"notinexc_str |
| 37 | +assertbase._URL_ATTRIBUTE_ERRORnotinexc_str |
| 38 | + |
| 39 | + |
| 40 | +deftest_missing_attribute_from_list_raises_custom(gl,fake_manager): |
| 41 | +"""Ensure a missing attribute raises our custom error message if the |
| 42 | + RESTObject was created from a list""" |
| 43 | +obj=helpers.FakeObject( |
| 44 | +manager=fake_manager,attrs={"foo":"bar"},created_from_list=True |
| 45 | + ) |
| 46 | +withpytest.raises(AttributeError)asexcinfo: |
| 47 | +obj.missing_attribute |
| 48 | +exc_str=str(excinfo.value) |
| 49 | +assert"missing_attribute"inexc_str |
| 50 | +assert"was created via a list()"inexc_str |
| 51 | +assertbase._URL_ATTRIBUTE_ERRORinexc_str |
| 52 | + |
| 53 | + |
| 54 | +deftest_picklability(fake_manager): |
| 55 | +obj=helpers.FakeObject(fake_manager, {"foo":"bar"}) |
| 56 | +original_obj_module=obj._module |
| 57 | +pickled=pickle.dumps(obj) |
| 58 | +unpickled=pickle.loads(pickled) |
| 59 | +assertisinstance(unpickled,helpers.FakeObject) |
| 60 | +asserthasattr(unpickled,"_module") |
| 61 | +assertunpickled._module==original_obj_module |
| 62 | +pickle.dumps(unpickled) |
| 63 | + |
| 64 | + |
| 65 | +deftest_attrs(fake_manager): |
| 66 | +obj=helpers.FakeObject(fake_manager, {"foo":"bar"}) |
| 67 | + |
| 68 | +assert"bar"==obj.foo |
| 69 | +withpytest.raises(AttributeError): |
| 70 | +getattr(obj,"bar") |
| 71 | + |
| 72 | +obj.bar="baz" |
| 73 | +assert"baz"==obj.bar |
| 74 | +assert {"foo":"bar"}==obj._attrs |
| 75 | +assert {"bar":"baz"}==obj._updated_attrs |
| 76 | + |
| 77 | + |
| 78 | +deftest_get_id(fake_manager): |
| 79 | +obj=helpers.FakeObject(fake_manager, {"foo":"bar"}) |
| 80 | +obj.id=42 |
| 81 | +assert42==obj.get_id() |
| 82 | + |
| 83 | +obj.id=None |
| 84 | +assertobj.get_id()isNone |
| 85 | + |
| 86 | + |
| 87 | +deftest_encoded_id(fake_manager): |
| 88 | +obj=helpers.FakeObject(fake_manager, {"foo":"bar"}) |
| 89 | +obj.id=42 |
| 90 | +assert42==obj.encoded_id |
| 91 | + |
| 92 | +obj.id=None |
| 93 | +assertobj.encoded_idisNone |
| 94 | + |
| 95 | +obj.id="plain" |
| 96 | +assert"plain"==obj.encoded_id |
| 97 | + |
| 98 | +obj.id="a/path" |
| 99 | +assert"a%2Fpath"==obj.encoded_id |
| 100 | + |
| 101 | +# If you assign it again it does not double URL-encode |
| 102 | +obj.id=obj.encoded_id |
| 103 | +assert"a%2Fpath"==obj.encoded_id |
| 104 | + |
| 105 | + |
| 106 | +deftest_custom_id_attr(fake_manager): |
| 107 | +obj=helpers.OtherFakeObject(fake_manager, {"foo":"bar"}) |
| 108 | +assert"bar"==obj.get_id() |
| 109 | + |
| 110 | + |
| 111 | +deftest_update_attrs(fake_manager): |
| 112 | +obj=helpers.FakeObject(fake_manager, {"foo":"bar"}) |
| 113 | +obj.bar="baz" |
| 114 | +obj._update_attrs({"foo":"foo","bar":"bar"}) |
| 115 | +assert {"foo":"foo","bar":"bar"}==obj._attrs |
| 116 | +assert {}==obj._updated_attrs |
| 117 | + |
| 118 | + |
| 119 | +deftest_update_attrs_deleted(fake_manager): |
| 120 | +obj=helpers.FakeObject(fake_manager, {"foo":"foo","bar":"bar"}) |
| 121 | +obj.bar="baz" |
| 122 | +obj._update_attrs({"foo":"foo"}) |
| 123 | +assert {"foo":"foo"}==obj._attrs |
| 124 | +assert {}==obj._updated_attrs |
| 125 | + |
| 126 | + |
| 127 | +deftest_dir_unique(fake_manager): |
| 128 | +obj=helpers.FakeObject(fake_manager, {"manager":"foo"}) |
| 129 | +assertlen(dir(obj))==len(set(dir(obj))) |
| 130 | + |
| 131 | + |
| 132 | +deftest_create_managers(gl,fake_manager): |
| 133 | +classObjectWithManager(helpers.FakeObject): |
| 134 | +fakes:"FakeManager" |
| 135 | + |
| 136 | +obj=ObjectWithManager(fake_manager, {"foo":"bar"}) |
| 137 | +obj.id=42 |
| 138 | +assertisinstance(obj.fakes,helpers.FakeManager) |
| 139 | +assertobj.fakes.gitlab==gl |
| 140 | +assertobj.fakes._parent==obj |
| 141 | + |
| 142 | + |
| 143 | +deftest_equality(fake_manager): |
| 144 | +obj1=helpers.FakeObject(fake_manager, {"id":"foo"}) |
| 145 | +obj2=helpers.FakeObject(fake_manager, {"id":"foo","other_attr":"bar"}) |
| 146 | +assertobj1==obj2 |
| 147 | +assertlen(set((obj1,obj2)))==1 |
| 148 | + |
| 149 | + |
| 150 | +deftest_equality_custom_id(fake_manager): |
| 151 | +obj1=helpers.OtherFakeObject(fake_manager, {"foo":"bar"}) |
| 152 | +obj2=helpers.OtherFakeObject(fake_manager, {"foo":"bar","other_attr":"baz"}) |
| 153 | +assertobj1==obj2 |
| 154 | + |
| 155 | + |
| 156 | +deftest_equality_no_id(fake_manager): |
| 157 | +obj1=helpers.FakeObject(fake_manager, {"attr1":"foo"}) |
| 158 | +obj2=helpers.FakeObject(fake_manager, {"attr1":"bar"}) |
| 159 | +assertnotobj1==obj2 |
| 160 | + |
| 161 | + |
| 162 | +deftest_inequality(fake_manager): |
| 163 | +obj1=helpers.FakeObject(fake_manager, {"id":"foo"}) |
| 164 | +obj2=helpers.FakeObject(fake_manager, {"id":"bar"}) |
| 165 | +assertobj1!=obj2 |
| 166 | + |
| 167 | + |
| 168 | +deftest_inequality_no_id(fake_manager): |
| 169 | +obj1=helpers.FakeObject(fake_manager, {"attr1":"foo"}) |
| 170 | +obj2=helpers.FakeObject(fake_manager, {"attr1":"bar"}) |
| 171 | +assertobj1!=obj2 |
| 172 | +assertlen(set((obj1,obj2)))==2 |
| 173 | + |
| 174 | + |
| 175 | +deftest_equality_with_other_objects(fake_manager): |
| 176 | +obj1=helpers.FakeObject(fake_manager, {"id":"foo"}) |
| 177 | +obj2=None |
| 178 | +assertnotobj1==obj2 |
| 179 | + |
| 180 | + |
| 181 | +deftest_dunder_str(fake_manager): |
| 182 | +fake_object=helpers.FakeObject(fake_manager, {"attr1":"foo"}) |
| 183 | +assertstr(fake_object)== ( |
| 184 | +"<class 'tests.unit.helpers.FakeObject'> => {'attr1': 'foo'}" |
| 185 | + ) |
| 186 | + |
| 187 | + |
| 188 | +@pytest.mark.parametrize( |
| 189 | +"id_attr,repr_attr, attrs, expected_repr", |
| 190 | + [ |
| 191 | + ("id",None, {"id":1},"<ReprObject id:1>"), |
| 192 | + ( |
| 193 | +"id", |
| 194 | +"name", |
| 195 | + {"id":1,"name":"fake"}, |
| 196 | +"<ReprObject id:1 name:fake>", |
| 197 | + ), |
| 198 | + ("name","name", {"name":"fake"},"<ReprObject name:fake>"), |
| 199 | + ("id","name", {"id":1},"<ReprObject id:1>"), |
| 200 | + (None,None, {},"<ReprObject>"), |
| 201 | + (None,"name", {"name":"fake"},"<ReprObject name:fake>"), |
| 202 | + (None,"name", {},"<ReprObject>"), |
| 203 | + ], |
| 204 | +ids=[ |
| 205 | +"GetMixin with id", |
| 206 | +"GetMixin with id and _repr_attr", |
| 207 | +"GetMixin with _repr_attr matching _id_attr", |
| 208 | +"GetMixin with _repr_attr without _repr_attr value defined", |
| 209 | +"GetWithoutIDMixin", |
| 210 | +"GetWithoutIDMixin with _repr_attr", |
| 211 | +"GetWithoutIDMixin with _repr_attr without _repr_attr value defined", |
| 212 | + ], |
| 213 | +) |
| 214 | +deftest_dunder_repr(fake_manager,id_attr,repr_attr,attrs,expected_repr): |
| 215 | +classReprObject(helpers.FakeObject): |
| 216 | +_id_attr=id_attr |
| 217 | +_repr_attr=repr_attr |
| 218 | + |
| 219 | +fake_object=ReprObject(fake_manager,attrs) |
| 220 | + |
| 221 | +assertrepr(fake_object)==expected_repr |
| 222 | + |
| 223 | + |
| 224 | +deftest_pformat(fake_manager): |
| 225 | +fake_object=helpers.FakeObject( |
| 226 | +fake_manager, {"attr1":"foo"*10,"ham":"eggs"*15} |
| 227 | + ) |
| 228 | +assertfake_object.pformat()== ( |
| 229 | +"<class 'tests.unit.helpers.FakeObject'> => " |
| 230 | +"\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n" |
| 231 | +" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}" |
| 232 | + ) |
| 233 | + |
| 234 | + |
| 235 | +deftest_pprint(capfd,fake_manager): |
| 236 | +fake_object=helpers.FakeObject( |
| 237 | +fake_manager, {"attr1":"foo"*10,"ham":"eggs"*15} |
| 238 | + ) |
| 239 | +result=fake_object.pprint() |
| 240 | +assertresultisNone |
| 241 | +stdout,stderr=capfd.readouterr() |
| 242 | +assertstdout== ( |
| 243 | +"<class 'tests.unit.helpers.FakeObject'> => " |
| 244 | +"\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n" |
| 245 | +" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}\n" |
| 246 | + ) |
| 247 | +assertstderr=="" |
| 248 | + |
| 249 | + |
| 250 | +deftest_repr(fake_manager): |
| 251 | +attrs= {"attr1":"foo"} |
| 252 | +obj=helpers.FakeObject(fake_manager,attrs) |
| 253 | +assertrepr(obj)=="<FakeObject id:None>" |
| 254 | + |
| 255 | +helpers.FakeObject._id_attr=None |
| 256 | +assertrepr(obj)=="<FakeObject>" |
| 257 | + |
| 258 | + |
| 259 | +deftest_attributes_get(fake_object): |
| 260 | +assertfake_object.attr1=="foo" |
| 261 | +result=fake_object.attributes |
| 262 | +assertresult== {"attr1":"foo","alist": [1,2,3]} |
| 263 | + |
| 264 | + |
| 265 | +deftest_attributes_shows_updates(fake_object): |
| 266 | +# Updated attribute value is reflected in `attributes` |
| 267 | +fake_object.attr1="hello" |
| 268 | +assertfake_object.attributes== {"attr1":"hello","alist": [1,2,3]} |
| 269 | +assertfake_object.attr1=="hello" |
| 270 | +# New attribute is in `attributes` |
| 271 | +fake_object.new_attrib="spam" |
| 272 | +assertfake_object.attributes== { |
| 273 | +"attr1":"hello", |
| 274 | +"new_attrib":"spam", |
| 275 | +"alist": [1,2,3], |
| 276 | + } |
| 277 | + |
| 278 | + |
| 279 | +deftest_attributes_is_copy(fake_object): |
| 280 | +# Modifying the dictionary does not cause modifications to the object |
| 281 | +result=fake_object.attributes |
| 282 | +result["alist"].append(10) |
| 283 | +assertresult== {"attr1":"foo","alist": [1,2,3,10]} |
| 284 | +assertfake_object.attributes== {"attr1":"foo","alist": [1,2,3]} |
| 285 | + |
| 286 | + |
| 287 | +deftest_attributes_has_parent_attrs(fake_object_with_parent): |
| 288 | +assertfake_object_with_parent.attr1=="foo" |
| 289 | +result=fake_object_with_parent.attributes |
| 290 | +assertresult== {"attr1":"foo","alist": [1,2,3],"test_id":"42"} |
| 291 | + |
| 292 | + |
| 293 | +deftest_asdict(fake_object): |
| 294 | +assertfake_object.attr1=="foo" |
| 295 | +result=fake_object.asdict() |
| 296 | +assertresult== {"attr1":"foo","alist": [1,2,3]} |
| 297 | + |
| 298 | + |
| 299 | +deftest_asdict_no_parent_attrs(fake_object_with_parent): |
| 300 | +assertfake_object_with_parent.attr1=="foo" |
| 301 | +result=fake_object_with_parent.asdict() |
| 302 | +assertresult== {"attr1":"foo","alist": [1,2,3]} |
| 303 | +assert"test_id"notinfake_object_with_parent.asdict() |
| 304 | +assert"test_id"notinfake_object_with_parent.asdict(with_parent_attrs=False) |
| 305 | +assert"test_id"infake_object_with_parent.asdict(with_parent_attrs=True) |
| 306 | + |
| 307 | + |
| 308 | +deftest_asdict_modify_dict_does_not_change_object(fake_object): |
| 309 | +result=fake_object.asdict() |
| 310 | +# Demonstrate modifying the dictionary does not modify the object |
| 311 | +result["attr1"]="testing" |
| 312 | +result["alist"].append(4) |
| 313 | +assertresult== {"attr1":"testing","alist": [1,2,3,4]} |
| 314 | +assertfake_object.attr1=="foo" |
| 315 | +assertfake_object.alist== [1,2,3] |
| 316 | + |
| 317 | + |
| 318 | +deftest_asdict_modify_dict_does_not_change_object2(fake_object): |
| 319 | +# Modify attribute and then ensure modifying a list in the returned dict won't |
| 320 | +# modify the list in the object. |
| 321 | +fake_object.attr1= [9,7,8] |
| 322 | +assertfake_object.asdict()== { |
| 323 | +"attr1": [9,7,8], |
| 324 | +"alist": [1,2,3], |
| 325 | + } |
| 326 | +result=fake_object.asdict() |
| 327 | +result["attr1"].append(1) |
| 328 | +assertfake_object.asdict()== { |
| 329 | +"attr1": [9,7,8], |
| 330 | +"alist": [1,2,3], |
| 331 | + } |
| 332 | + |
| 333 | + |
| 334 | +deftest_asdict_modify_object(fake_object): |
| 335 | +# asdict() returns the updated value |
| 336 | +fake_object.attr1="spam" |
| 337 | +assertfake_object.asdict()== {"attr1":"spam","alist": [1,2,3]} |