|
12 | 12 | from domain_models import errors
|
13 | 13 |
|
14 | 14 |
|
| 15 | +class Photo(models.DomainModel): |
| 16 | + id = fields.Int() |
| 17 | + storage_path = fields.String() |
| 18 | + |
| 19 | + |
| 20 | +class Profile(models.DomainModel): |
| 21 | + id = fields.Int() |
| 22 | + name = fields.String() |
| 23 | + main_photo = fields.Model(Photo) |
| 24 | + photos = fields.Collection(Photo) |
| 25 | + birth_date = fields.Date() |
| 26 | + |
| 27 | + |
15 | 28 | class BaseModelsTests(unittest.TestCase):
|
16 | 29 | """Basic model tests."""
|
17 | 30 |
|
@@ -127,9 +140,23 @@ class Model2(models.DomainModel):
|
127 | 140 |
|
128 | 141 | field = Model1.field
|
129 | 142 |
|
| 143 | + |
| 144 | +class ModelSetterGetterTests(unittest.TestCase): |
| 145 | + """Tests for getter and setter methods of model.""" |
| 146 | + data = { |
| 147 | + 'id': 1, |
| 148 | + 'name': 'John', |
| 149 | + 'main_photo': {'id': 1, |
| 150 | + 'storage_path': 'some/dir/where/photos/live/1.jpg'}, |
| 151 | + 'photos': [ |
| 152 | + {'id': 1, 'storage_path': 'some/dir/where/photos/live/1.jpg'}, |
| 153 | + {'id': 2, 'storage_path': 'some/dir/where/photos/live/2.jpg'} |
| 154 | + ], |
| 155 | + 'birth_date': datetime.date(year=1986, month=4, day=26) |
| 156 | + } |
| 157 | + |
130 | 158 | def test_get_method_on_undefined(self):
|
131 | 159 | """Test method get of Model."""
|
132 |
| - |
133 | 160 | class Model(models.DomainModel):
|
134 | 161 | """Test model."""
|
135 | 162 | field = fields.Int()
|
@@ -183,7 +210,6 @@ class Model(models.DomainModel):
|
183 | 210 |
|
184 | 211 | def test_get_method_on_bool(self):
|
185 | 212 | """Test method get on Bool of Model."""
|
186 |
| - |
187 | 213 | class Model(models.DomainModel):
|
188 | 214 | """Test model."""
|
189 | 215 | field = fields.Bool()
|
@@ -284,38 +310,111 @@ def test_get_method_on_collection(self):
|
284 | 310 | self.skipTest("Test is not implemented yet")
|
285 | 311 |
|
286 | 312 | def test_get_data_method(self):
|
| 313 | + """Test get_data method.""" |
| 314 | + photo1 = Photo(id=1, storage_path='some/dir/where/photos/live/1.jpg') |
| 315 | + photo2 = Photo(id=2, storage_path='some/dir/where/photos/live/2.jpg') |
| 316 | + profile = Profile(id=1, name='John', main_photo=photo1, |
| 317 | + photos=[photo1, photo2], |
| 318 | + birth_date=datetime.date(year=1986, month=4, |
| 319 | + day=26)) |
| 320 | + |
| 321 | + self.assertDictEqual(profile.get_data(), self.data) |
| 322 | + |
| 323 | + def test_set_data_method(self): |
| 324 | + """Test set_data method.""" |
| 325 | + profile = Profile() |
| 326 | + profile.set_data(self.data) |
| 327 | + |
| 328 | + self.assertEqual(profile.id, 1) |
| 329 | + self.assertEqual(profile.name, 'John') |
| 330 | + |
| 331 | + self.assertIsInstance(profile.main_photo, Photo) |
| 332 | + self.assertEqual(profile.main_photo.id, 1) |
| 333 | + self.assertEqual(profile.main_photo.storage_path, |
| 334 | + 'some/dir/where/photos/live/1.jpg') |
| 335 | + |
| 336 | + self.assertIsInstance(profile.photos, Photo.Collection) |
| 337 | + self.assertEqual(profile.photos[0].id, 1) |
| 338 | + self.assertEqual(profile.photos[0].storage_path, |
| 339 | + 'some/dir/where/photos/live/1.jpg') |
| 340 | + self.assertEqual(profile.photos[1].id, 2) |
| 341 | + self.assertEqual(profile.photos[1].storage_path, |
| 342 | + 'some/dir/where/photos/live/2.jpg') |
| 343 | + |
| 344 | + self.assertEqual(profile.birth_date, |
| 345 | + datetime.date(year=1986, month=4, day=26)) |
| 346 | + |
| 347 | + def test_set_data_via_constructor(self): |
| 348 | + """Test set data via model.""" |
| 349 | + profile = Profile(**self.data) |
| 350 | + |
| 351 | + self.assertEqual(profile.id, 1) |
| 352 | + self.assertEqual(profile.name, 'John') |
| 353 | + |
| 354 | + self.assertIsInstance(profile.main_photo, Photo) |
| 355 | + self.assertEqual(profile.main_photo.id, 1) |
| 356 | + self.assertEqual(profile.main_photo.storage_path, |
| 357 | + 'some/dir/where/photos/live/1.jpg') |
| 358 | + |
| 359 | + self.assertIsInstance(profile.photos, Photo.Collection) |
| 360 | + self.assertEqual(profile.photos[0].id, 1) |
| 361 | + self.assertEqual(profile.photos[0].storage_path, |
| 362 | + 'some/dir/where/photos/live/1.jpg') |
| 363 | + self.assertEqual(profile.photos[1].id, 2) |
| 364 | + self.assertEqual(profile.photos[1].storage_path, |
| 365 | + 'some/dir/where/photos/live/2.jpg') |
| 366 | + |
| 367 | + self.assertEqual(profile.birth_date, |
| 368 | + datetime.date(year=1986, month=4, day=26)) |
| 369 | + |
| 370 | + def test_set_data_method_defaults(self): |
287 | 371 | class Photo(models.DomainModel):
|
288 | 372 | id = fields.Int()
|
289 |
| - url = fields.String() |
| 373 | + storage_path = fields.String( |
| 374 | + default='some/dir/where/photos/live/default.jpg') |
| 375 | + |
| 376 | + default_photo = Photo() |
| 377 | + |
| 378 | + class Profile(models.DomainModel): |
| 379 | + id = fields.Int() |
| 380 | + name = fields.String() |
| 381 | + main_photo = fields.Model(Photo, default=default_photo) |
| 382 | + photos = fields.Collection(Photo) |
| 383 | + birth_date = fields.Date() |
| 384 | + something = fields.String(default='def-val') |
| 385 | + |
| 386 | + profile = Profile() |
| 387 | + profile.set_data({'id': 1, 'name': 'John'}) |
| 388 | + |
| 389 | + self.assertEqual(profile.id, 1) |
| 390 | + self.assertEqual(profile.name, 'John') |
| 391 | + |
| 392 | + self.assertIsInstance(profile.main_photo, Photo) |
| 393 | + self.assertEqual(profile.main_photo.storage_path, |
| 394 | + 'some/dir/where/photos/live/default.jpg') |
| 395 | + |
| 396 | + self.assertIsNone(profile.main_photo.id) |
| 397 | + self.assertIsNone(profile.photos) |
| 398 | + self.assertIsNone(profile.birth_date) |
| 399 | + |
| 400 | + self.assertEqual(profile.something, 'def-val') |
| 401 | + |
| 402 | + def test_set_data_method_requirements(self): |
| 403 | + class Photo(models.DomainModel): |
| 404 | + id = fields.Int(required=True) |
| 405 | + storage_path = fields.String(required=True) |
290 | 406 |
|
291 | 407 | class Profile(models.DomainModel):
|
292 | 408 | id = fields.Int()
|
293 | 409 | name = fields.String()
|
294 | 410 | main_photo = fields.Model(Photo)
|
295 | 411 | photos = fields.Collection(Photo)
|
296 | 412 | birth_date = fields.Date()
|
297 |
| - sequence = fields.Collection(fields.Int) |
298 | 413 |
|
299 |
| - photo1 = Photo(id=1, url='http://boonya.info/wat.jpg?1') |
300 |
| - photo2 = Photo(id=2, url='http://boonya.info/wat.jpg?2') |
301 |
| - profile = Profile(id=1, name='John', main_photo=photo1, |
302 |
| - photos=[photo1, photo2], |
303 |
| - sequence=[1, 1, 2, 3, 5, 8, 13], |
304 |
| - birth_date=datetime.date(year=1986, month=4, |
305 |
| - day=26)) |
| 414 | + profile = Profile() |
306 | 415 |
|
307 |
| - self.assertDictEqual(profile.get_data(), { |
308 |
| - 'id': 1, |
309 |
| - 'name': 'John', |
310 |
| - 'main_photo': {'id': 1, |
311 |
| - 'url': 'http://boonya.info/wat.jpg?1'}, |
312 |
| - 'photos': [ |
313 |
| - {'id': 1, 'url': 'http://boonya.info/wat.jpg?1'}, |
314 |
| - {'id': 2, 'url': 'http://boonya.info/wat.jpg?2'} |
315 |
| - ], |
316 |
| - 'sequence': [1, 1, 2, 3, 5, 8, 13], |
317 |
| - 'birth_date': datetime.date(year=1986, month=4, day=26) |
318 |
| - }) |
| 416 | + with self.assertRaises(AttributeError): |
| 417 | + profile.set_data({'main_photo': {'id': 1}}) |
319 | 418 |
|
320 | 419 |
|
321 | 420 | class ModelReprTests(unittest.TestCase):
|
|
0 commit comments