|
41 | 41 | TEST_IMAGE_PATH = os.path.join(os.path.dirname(__file__), "../fields/mongoengine.png") |
42 | 42 |
|
43 | 43 |
|
44 | | -class TestInstance(MongoDBTestCase): |
| 44 | +class TestDocumentInstance(MongoDBTestCase): |
45 | 45 | def setUp(self): |
46 | 46 | class Job(EmbeddedDocument): |
47 | 47 | name = StringField() |
@@ -3617,6 +3617,51 @@ class A(Document): |
3617 | 3617 | assert b._instance == a |
3618 | 3618 | assert idx == 2 |
3619 | 3619 |
|
| 3620 | + def test_updating_listfield_manipulate_list(self): |
| 3621 | + class Company(Document): |
| 3622 | + name = StringField() |
| 3623 | + employees = ListField(field=DictField()) |
| 3624 | + |
| 3625 | + Company.drop_collection() |
| 3626 | + |
| 3627 | + comp = Company(name="BigBank", employees=[{"name": "John"}]) |
| 3628 | + comp.save() |
| 3629 | + comp.employees.append({"name": "Bill"}) |
| 3630 | + comp.save() |
| 3631 | + |
| 3632 | + stored_comp = get_as_pymongo(comp) |
| 3633 | + self.assertEqual( |
| 3634 | + stored_comp, |
| 3635 | + { |
| 3636 | + "_id": comp.id, |
| 3637 | + "employees": [{"name": "John"}, {"name": "Bill"}], |
| 3638 | + "name": "BigBank", |
| 3639 | + }, |
| 3640 | + ) |
| 3641 | + |
| 3642 | + comp = comp.reload() |
| 3643 | + comp.employees[0]["color"] = "red" |
| 3644 | + comp.employees[-1]["color"] = "blue" |
| 3645 | + comp.employees[-1].update({"size": "xl"}) |
| 3646 | + comp.save() |
| 3647 | + |
| 3648 | + assert len(comp.employees) == 2 |
| 3649 | + assert comp.employees[0] == {"name": "John", "color": "red"} |
| 3650 | + assert comp.employees[1] == {"name": "Bill", "size": "xl", "color": "blue"} |
| 3651 | + |
| 3652 | + stored_comp = get_as_pymongo(comp) |
| 3653 | + self.assertEqual( |
| 3654 | + stored_comp, |
| 3655 | + { |
| 3656 | + "_id": comp.id, |
| 3657 | + "employees": [ |
| 3658 | + {"name": "John", "color": "red"}, |
| 3659 | + {"size": "xl", "color": "blue", "name": "Bill"}, |
| 3660 | + ], |
| 3661 | + "name": "BigBank", |
| 3662 | + }, |
| 3663 | + ) |
| 3664 | + |
3620 | 3665 | def test_falsey_pk(self): |
3621 | 3666 | """Ensure that we can create and update a document with Falsey PK.""" |
3622 | 3667 |
|
|
0 commit comments