Ensure you can handle circular references with more than one level
(self)
| 422 | assert "[<Person: Mother>, <Person: Daughter>]" == "%s" % Person.objects() |
| 423 | |
| 424 | def test_circular_tree_reference(self): |
| 425 | """Ensure you can handle circular references with more than one level""" |
| 426 | |
| 427 | class Other(EmbeddedDocument): |
| 428 | name = StringField() |
| 429 | friends = ListField(ReferenceField("Person")) |
| 430 | |
| 431 | class Person(Document): |
| 432 | name = StringField() |
| 433 | other = EmbeddedDocumentField(Other, default=lambda: Other()) |
| 434 | |
| 435 | def __repr__(self): |
| 436 | return "<Person: %s>" % self.name |
| 437 | |
| 438 | Person.drop_collection() |
| 439 | paul = Person(name="Paul").save() |
| 440 | maria = Person(name="Maria").save() |
| 441 | julia = Person(name="Julia").save() |
| 442 | anna = Person(name="Anna").save() |
| 443 | |
| 444 | paul.other.friends = [maria, julia, anna] |
| 445 | paul.other.name = "Paul's friends" |
| 446 | paul.save() |
| 447 | |
| 448 | maria.other.friends = [paul, julia, anna] |
| 449 | maria.other.name = "Maria's friends" |
| 450 | maria.save() |
| 451 | |
| 452 | julia.other.friends = [paul, maria, anna] |
| 453 | julia.other.name = "Julia's friends" |
| 454 | julia.save() |
| 455 | |
| 456 | anna.other.friends = [paul, maria, julia] |
| 457 | anna.other.name = "Anna's friends" |
| 458 | anna.save() |
| 459 | |
| 460 | assert ( |
| 461 | "[<Person: Paul>, <Person: Maria>, <Person: Julia>, <Person: Anna>]" |
| 462 | == "%s" % Person.objects() |
| 463 | ) |
| 464 | |
| 465 | def test_generic_reference(self): |
| 466 | class UserA(Document): |
nothing calls this directly
no test coverage detected