async def test_list_stack(db_config, database): connection.Connection.connect(**db_config) class ListDoc(BaseDocument): nums = fields.ListField(fields.ListField(fields.IntField())) l = ListDoc() l.nums.append([]) l.nums[0].append(5) l.nums[0].append(6) l.nums.append([]) l.nums[1].append(10) await l.save() coll = database['ListDoc'] docs = coll.find_one() assert docs['nums'][0][0] == 5 assert docs['nums'][0][1] == 6 assert docs['nums'][1][0] == 10 l.nums[1][0] = Inc(1) await l.save() docs = coll.find_one() assert l.nums[1][0] == 11 assert docs['nums'][1][0] == 11
async def test_inc_twice(db_config, database): connection.Connection.connect(**db_config) coll = database['Document'] class Document(BaseDocument): num = fields.IntField() doc = Document(num=0) await doc.save() doc.num = Inc(5) doc.num = Inc(5) assert doc.num == 10 await doc.save() result = coll.find_one() assert result['num'] == 10
async def test_list_update_multiple(db_config, database): connection.Connection.connect(**db_config) class ListDoc(BaseDocument): nums = fields.ListField(fields.IntField()) l = ListDoc() l.nums.append(5) await l.save() assert l.nums[0] == 5 l.nums[0] = Inc(5) l.nums[0] = Inc(5) await l.save() assert l.nums[0] == 15 coll = database['ListDoc'] docs = coll.find_one() assert docs['nums'][0] == 15
async def test_map_defaults(db_config, database): connection.Connection.connect(**db_config) coll = database['MapDoc'] class MapDoc(BaseDocument): mapping = fields.MapField(fields.IntField()) m = MapDoc(mapping={'x': 5}) await m.save() m.mapping['x'] = Inc(5) await m.save() doc = coll.find_one() assert doc['mapping']['x'] == 10
async def test_multiple(db_config, database): connection.Connection.connect(**db_config) coll = database['Document'] class Document(BaseDocument): num = fields.IntField() doc = Document(num=10) await doc.save() doc.num = Min(15) assert doc.num == 10 doc.num = Inc(5) assert doc.num == 15 doc.num = Dec(2) assert doc.num == 13 await doc.save()
async def test_list_inc(db_config, database): connection.Connection.connect(**db_config) class ListDoc(BaseDocument): nums = fields.ListField(fields.IntField()) l = ListDoc() l.nums.append(5) l.nums.append(6) l.nums.append(7) coll = database['ListDoc'] await l.save() docs = coll.find_one() assert docs['nums'] == [5, 6, 7] l.nums[0] = Inc(5) await l.save() docs = coll.find_one() assert docs['nums'][0] == 10
async def test_map_defaults(db_config, database): connection.Connection.connect(**db_config) class MapDoc(BaseDocument): mapping = fields.MapField(fields.IntField(required=True), default={'x': 5}) m = MapDoc() await m.save() coll = database['MapDoc'] doc = coll.find_one() assert doc['mapping'] == {'x': 5} m.mapping['x'] = Inc(3) await m.save() m2 = MapDoc() await m2.save() doc = coll.find_one({'_id': m2.id}) assert doc['mapping'] == {'x': 5}