Skip to content

Commit 12d3376

Browse files
committed
Test coverage first. Issue #21
1 parent 4dc4402 commit 12d3376

File tree

2 files changed

+149
-21
lines changed

2 files changed

+149
-21
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ env:
1616
- TOXENV=py35
1717
- TOXENV=pypy
1818
- TOXENV=pypy3
19+
notifications:
20+
slack:
21+
secure: QHOypFu1TxCmkbCeNSqY39Bdj+hxOnA7H0zpTZrxDouLFdD+Ah5cKHFOFjFULruCdRo+JI8vPN0fmgRviUcyFVjKbs/3sEEUNU//JJpBPDQB9Epdc8jVp1w1GDF4fN/ujg3XW19U6N1dORRQBFi5EuJn/seYErogIs6F/4G8nEstyTLWqZ7DgnVBMM5aUVuwDae+iUNTutWTvVwJVqS84JC2rFzsRmXc9RJou6/PljBGogE5t+RYHWWAzSodwf3fMIdC7nKk+U6p5IwaDd021vgqk6Dduhu5qum89KsV80hP97qTIiJO/bz5gX6S/DG4cjjXFo2ELZARcRdQ2G7PJBooomiMPW3OTm4uTIbjxDKSIlZm/miHjUVv/+Am5TYgYPnOqU9RlYxAGFbKmkOXDwgJK3nyK8s4hK19cGss3hbRPC7VWRXAaGmO6Yk+UH7wUAbAPSzXvLT/GXc8VlY276KIFVoBX0dLlqSLt/jHrVvKZL1djXKIEa1cDUA9rDqybn+/Oue0QF9ask34lNM2Uu/5IdMAUz8V/U7UHtlfoYFMOJIbgzjUHRLAcIx9+k908rHAOzkMnPns9rDHkJFgJaj3n6Q5HJpQUMwq89Lhb1NHry0nNRE5awtXio6cRcri/ApVxFA+juuyRS64y1Adi8Su/ENAw4CkVeURJuMOqPk=

tests/test_models.py

Lines changed: 146 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,25 @@
1212
from domain_models import errors
1313

1414

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+
1528
class BaseModelsTests(unittest.TestCase):
1629
"""Basic model tests."""
1730

1831
def test_set_and_get_attrs(self):
1932
"""Test setting and getting of domain model attributes."""
33+
2034
class User(models.DomainModel):
2135
"""Test user domain model."""
2236

@@ -116,6 +130,7 @@ class Model(models.DomainModel):
116130

117131
def test_field_could_not_be_rebound_in_different_model(self):
118132
"""Test that field could not be rebound."""
133+
119134
class Model1(models.DomainModel):
120135
"""Test model."""
121136

@@ -127,6 +142,21 @@ class Model2(models.DomainModel):
127142

128143
field = Model1.field
129144

145+
146+
class ModelSetterGetterTests(unittest.TestCase):
147+
"""Tests for getter and setter methods of model."""
148+
data = {
149+
'id': 1,
150+
'name': 'John',
151+
'main_photo': {'id': 1,
152+
'storage_path': 'some/dir/where/photos/live/1.jpg'},
153+
'photos': [
154+
{'id': 1, 'storage_path': 'some/dir/where/photos/live/1.jpg'},
155+
{'id': 2, 'storage_path': 'some/dir/where/photos/live/2.jpg'}
156+
],
157+
'birth_date': datetime.date(year=1986, month=4, day=26)
158+
}
159+
130160
def test_get_method_on_undefined(self):
131161
"""Test method get of Model."""
132162

@@ -284,45 +314,119 @@ def test_get_method_on_collection(self):
284314
self.skipTest("Test is not implemented yet")
285315

286316
def test_get_data_method(self):
317+
"""Test get_data method."""
318+
photo1 = Photo(id=1, storage_path='some/dir/where/photos/live/1.jpg')
319+
photo2 = Photo(id=2, storage_path='some/dir/where/photos/live/2.jpg')
320+
profile = Profile(id=1, name='John', main_photo=photo1,
321+
photos=[photo1, photo2],
322+
birth_date=datetime.date(year=1986, month=4,
323+
day=26))
324+
325+
self.assertDictEqual(profile.get_data(), self.data)
326+
327+
def test_set_data_method(self):
328+
"""Test set_data method."""
329+
profile = Profile()
330+
profile.set_data(self.data)
331+
332+
self.assertEqual(profile.id, 1)
333+
self.assertEqual(profile.name, 'John')
334+
335+
self.assertIsInstance(profile.main_photo, Photo)
336+
self.assertEqual(profile.main_photo.id, 1)
337+
self.assertEqual(profile.main_photo.storage_path,
338+
'some/dir/where/photos/live/1.jpg')
339+
340+
self.assertIsInstance(profile.photos, Photo.Collection)
341+
self.assertEqual(profile.photos[0].id, 1)
342+
self.assertEqual(profile.photos[0].storage_path,
343+
'some/dir/where/photos/live/1.jpg')
344+
self.assertEqual(profile.photos[1].id, 2)
345+
self.assertEqual(profile.photos[1].storage_path,
346+
'some/dir/where/photos/live/2.jpg')
347+
348+
self.assertEqual(profile.birth_date,
349+
datetime.date(year=1986, month=4, day=26))
350+
351+
def test_set_data_via_constructor(self):
352+
"""Test set data via model."""
353+
profile = Profile(**self.data)
354+
355+
self.assertEqual(profile.id, 1)
356+
self.assertEqual(profile.name, 'John')
357+
358+
self.assertIsInstance(profile.main_photo, Photo)
359+
self.assertEqual(profile.main_photo.id, 1)
360+
self.assertEqual(profile.main_photo.storage_path,
361+
'some/dir/where/photos/live/1.jpg')
362+
363+
self.assertIsInstance(profile.photos, Photo.Collection)
364+
self.assertEqual(profile.photos[0].id, 1)
365+
self.assertEqual(profile.photos[0].storage_path,
366+
'some/dir/where/photos/live/1.jpg')
367+
self.assertEqual(profile.photos[1].id, 2)
368+
self.assertEqual(profile.photos[1].storage_path,
369+
'some/dir/where/photos/live/2.jpg')
370+
371+
self.assertEqual(profile.birth_date,
372+
datetime.date(year=1986, month=4, day=26))
373+
374+
def test_set_data_method_defaults(self):
287375
class Photo(models.DomainModel):
288376
id = fields.Int()
289-
url = fields.String()
377+
storage_path = fields.String(
378+
default='some/dir/where/photos/live/default.jpg')
379+
380+
default_photo = Photo()
381+
382+
class Profile(models.DomainModel):
383+
id = fields.Int()
384+
name = fields.String()
385+
main_photo = fields.Model(Photo, default=default_photo)
386+
photos = fields.Collection(Photo)
387+
birth_date = fields.Date()
388+
something = fields.String(default='def-val')
389+
390+
profile = Profile()
391+
profile.set_data({'id': 1, 'name': 'John'})
392+
393+
self.assertEqual(profile.id, 1)
394+
self.assertEqual(profile.name, 'John')
395+
396+
self.assertIsInstance(profile.main_photo, Photo)
397+
self.assertEqual(profile.main_photo.storage_path,
398+
'some/dir/where/photos/live/default.jpg')
399+
400+
self.assertIsNone(profile.main_photo.id)
401+
self.assertIsNone(profile.photos)
402+
self.assertIsNone(profile.birth_date)
403+
404+
self.assertEqual(profile.something, 'def-val')
405+
406+
def test_set_data_method_requirements(self):
407+
class Photo(models.DomainModel):
408+
id = fields.Int(required=True)
409+
storage_path = fields.String(required=True)
290410

291411
class Profile(models.DomainModel):
292412
id = fields.Int()
293413
name = fields.String()
294414
main_photo = fields.Model(Photo)
295415
photos = fields.Collection(Photo)
296416
birth_date = fields.Date()
297-
sequence = fields.Collection(fields.Int)
298417

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))
418+
profile = Profile()
306419

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-
})
420+
with self.assertRaises(AttributeError):
421+
profile.set_data({'main_photo': {'id': 1}})
319422

320423

321424
class ModelReprTests(unittest.TestCase):
322425
"""Tests for model Pythonic representation."""
323426

324427
def test_repr(self):
325428
"""Test model __repr__()."""
429+
326430
class User(models.DomainModel):
327431
"""Test user domain model."""
328432

@@ -357,6 +461,7 @@ class ModelStrTests(unittest.TestCase):
357461

358462
def test_str_with_single_view_key(self):
359463
"""Test model __str__()."""
464+
360465
class User(models.DomainModel):
361466
"""Test user domain model."""
362467

@@ -390,6 +495,7 @@ class User(models.DomainModel):
390495

391496
def test_str_with_multiple_view_keys(self):
392497
"""Test model __str__()."""
498+
393499
class User(models.DomainModel):
394500
"""Test user domain model."""
395501

@@ -423,6 +529,7 @@ class User(models.DomainModel):
423529

424530
def test_str_without_view_key(self):
425531
"""Test model __str__()."""
532+
426533
class User(models.DomainModel):
427534
"""Test user domain model."""
428535

@@ -449,6 +556,7 @@ class ModelSlotsOptimizationTests(unittest.TestCase):
449556

450557
def test_model_slots(self):
451558
"""Test model slots optimization."""
559+
452560
class Model(models.DomainModel):
453561
"""Test model."""
454562

@@ -463,6 +571,7 @@ class Model(models.DomainModel):
463571

464572
def test_model_slots_disabling(self):
465573
"""Test disabling of model slots optimization."""
574+
466575
class Model(models.DomainModel):
467576
"""Test model."""
468577

@@ -482,6 +591,7 @@ class ModelsEqualityComparationsTests(unittest.TestCase):
482591

483592
def test_models_equal_single_key(self):
484593
"""Test models equality comparator based on unique key."""
594+
485595
class Model(models.DomainModel):
486596
"""Test domain model with single unique key."""
487597

@@ -499,6 +609,7 @@ class Model(models.DomainModel):
499609

500610
def test_models_not_equal_single_key(self):
501611
"""Test that models are not equal."""
612+
502613
class Model(models.DomainModel):
503614
"""Test domain model with single unique key."""
504615

@@ -516,6 +627,7 @@ class Model(models.DomainModel):
516627

517628
def test_models_equal_multiple_keys(self):
518629
"""Test models equality comparator based on unique key."""
630+
519631
class Model(models.DomainModel):
520632
"""Test domain model with multiple unique key."""
521633

@@ -536,6 +648,7 @@ class Model(models.DomainModel):
536648

537649
def test_models_not_equal_multiple_keys(self):
538650
"""Test that models are not equal."""
651+
539652
class Model(models.DomainModel):
540653
"""Test domain model with multiple unique key."""
541654

@@ -556,6 +669,7 @@ class Model(models.DomainModel):
556669

557670
def test_models_not_equal_multiple_keys_first_equal(self):
558671
"""Test that models are not equal."""
672+
559673
class Model(models.DomainModel):
560674
"""Test domain model with multiple unique key."""
561675

@@ -576,6 +690,7 @@ class Model(models.DomainModel):
576690

577691
def test_models_not_equal_different_classes(self):
578692
"""Test that models are not equal."""
693+
579694
class Model1(models.DomainModel):
580695
"""Test domain model with single unique key."""
581696

@@ -599,6 +714,7 @@ class Model2(models.DomainModel):
599714

600715
def test_models_not_equal_scalar_value(self):
601716
"""Test that model and scalar value are not equal."""
717+
602718
class Model(models.DomainModel):
603719
"""Test domain model with single unique key."""
604720

@@ -613,6 +729,7 @@ class Model(models.DomainModel):
613729

614730
def test_models_not_equal_unknown_unique_key(self):
615731
"""Test that models are not equal."""
732+
616733
class Model(models.DomainModel):
617734
"""Test domain model without unique key."""
618735

@@ -629,6 +746,7 @@ class Model(models.DomainModel):
629746

630747
def test_same_models_equal_unknown_unique_key(self):
631748
"""Test that models are not equal."""
749+
632750
class Model(models.DomainModel):
633751
"""Test domain model without unique key."""
634752

@@ -642,6 +760,7 @@ class Model(models.DomainModel):
642760

643761
def test_non_equal_models_in_set_single_key(self):
644762
"""Test that non-equal models work properly with sets."""
763+
645764
class Model(models.DomainModel):
646765
"""Test domain model with single unique key."""
647766

@@ -664,6 +783,7 @@ class Model(models.DomainModel):
664783

665784
def test_equal_models_in_set_single_key(self):
666785
"""Test that equal models work properly with sets."""
786+
667787
class Model(models.DomainModel):
668788
"""Test domain model with single unique key."""
669789

@@ -686,6 +806,7 @@ class Model(models.DomainModel):
686806

687807
def test_non_equal_models_in_set_multiple_keys(self):
688808
"""Test that non-equal models work properly with sets."""
809+
689810
class Model(models.DomainModel):
690811
"""Test domain model with multiple unique key."""
691812

@@ -712,6 +833,7 @@ class Model(models.DomainModel):
712833

713834
def test_equal_models_in_set_multiple_keys(self):
714835
"""Test that equal models work properly with sets."""
836+
715837
class Model(models.DomainModel):
716838
"""Test domain model with multiple unique key."""
717839

@@ -738,6 +860,7 @@ class Model(models.DomainModel):
738860

739861
def test_non_equal_models_in_set_without_unique_key(self):
740862
"""Test that non-equal models work properly with sets."""
863+
741864
class Model(models.DomainModel):
742865
"""Test domain model without unique key."""
743866

@@ -759,6 +882,7 @@ class Model(models.DomainModel):
759882

760883
def test_equal_models_in_set_without_unique_key(self):
761884
"""Test that equal models work properly with sets."""
885+
762886
class Model(models.DomainModel):
763887
"""Test domain model without unique key."""
764888

@@ -778,6 +902,7 @@ class Model(models.DomainModel):
778902

779903
def test_models_collection_extending(self):
780904
"""Test model's collection extending."""
905+
781906
class Credit(models.DomainModel):
782907
"""Test credit domain model."""
783908

0 commit comments

Comments
 (0)