person_collection.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import datetime
  2. from yamily import Person, PersonCollection
  3. def test_add_person_again():
  4. collection = PersonCollection()
  5. alice1 = Person("alice")
  6. alice1.name = "Alice"
  7. collection.add_person(alice1)
  8. alice2 = Person("alice")
  9. alice2.birth_date = datetime.date(2019, 12, 23)
  10. collection.add_person(alice2)
  11. assert len(list(collection)) == 1
  12. assert collection["alice"].name == "Alice"
  13. assert collection["alice"].birth_date == datetime.date(2019, 12, 23)
  14. def test_add_person_unknown_parents():
  15. collection = PersonCollection()
  16. alice = Person("alice")
  17. alice.name = "Alice"
  18. alice.birth_date = datetime.date(2019, 12, 23)
  19. alice.mother = Person("mother")
  20. alice.father = Person("father")
  21. collection.add_person(alice)
  22. assert collection["alice"].birth_date == datetime.date(2019, 12, 23)
  23. assert collection["alice"] is alice
  24. assert collection["mother"] is alice.mother
  25. assert collection["father"] is alice.father
  26. def test_add_person_known_parents():
  27. collection = PersonCollection()
  28. mother = Person("mother")
  29. mother.name = "Mum"
  30. collection.add_person(mother)
  31. collection.add_person(Person("father"))
  32. alice = Person("alice")
  33. alice.name = "Alice"
  34. alice.birth_date = datetime.date(2019, 12, 23)
  35. alice.mother = Person("mother")
  36. alice.father = Person("father")
  37. collection.add_person(alice)
  38. assert collection["alice"].birth_date == datetime.date(2019, 12, 23)
  39. assert collection["alice"] is alice
  40. assert collection["mother"] is mother
  41. assert collection["mother"] is alice.mother
  42. assert collection["alice"].mother.name == "Mum"
  43. assert collection["father"] is alice.father
  44. def test_add_person_later_parents():
  45. collection = PersonCollection()
  46. alice = Person("alice")
  47. alice.name = "Alice"
  48. alice.birth_date = datetime.date(2019, 12, 23)
  49. alice.mother = Person("mother")
  50. alice.father = Person("father")
  51. collection.add_person(alice)
  52. assert collection["mother"].name is None
  53. assert collection["father"].name is None
  54. mother = Person("mother")
  55. mother.name = "Mum"
  56. stored_mother = collection.add_person(mother)
  57. father = Person("father")
  58. father.name = "Dad"
  59. stored_father = collection.add_person(father)
  60. assert collection["alice"].birth_date == datetime.date(2019, 12, 23)
  61. assert collection["alice"] is alice
  62. assert collection["mother"] is alice.mother
  63. assert collection["mother"] is stored_mother
  64. assert collection["alice"].mother.name == "Mum"
  65. assert collection["father"] is alice.father
  66. assert collection["father"] is stored_father
  67. assert collection["alice"].father.name == "Dad"