async def test_update_all(preset_documents): await Sample.update_all(Set({Sample.integer: 100})) result = await Sample.find_all().to_list() for sample in result: assert sample.integer == 100 await Sample.find_all().update(Set({Sample.integer: 101})) result = await Sample.find_all().to_list() for sample in result: assert sample.integer == 101
async def test_update_one(preset_documents): await Sample.find_one(Sample.integer == 1 ).update(Set({Sample.integer: 100})) result = await Sample.find_many(Sample.integer == 100).to_list() assert len(result) == 1 assert result[0].integer == 100 await Sample.find_one(Sample.integer == 1 ).update_one(Set({Sample.integer: 101})) result = await Sample.find_many(Sample.integer == 101).to_list() assert len(result) == 1 assert result[0].integer == 101
async def test_update_pymongo_kwargs(preset_documents): with pytest.raises(TypeError): await Sample.find_many(Sample.increment > 4 ).update(Set({Sample.increment: 100}), wrong="integer_1") await Sample.find_many(Sample.increment > 4 ).update(Set({Sample.increment: 100}), hint="integer_1") await Sample.find_one(Sample.increment > 4 ).update(Set({Sample.increment: 100}), hint="integer_1")
def set(self, expression: Dict[Union[ExpressionField, str], Any], session: Optional[ClientSession] = None, **kwargs): """ Set values Example: ```python class Sample(Document): one: int await Document.find(Sample.one == 1).set({Sample.one: 100}) ``` Uses [Set operator](https://roman-right.github.io/beanie/api/operators/update/#set) :param expression: Dict[Union[ExpressionField, str], Any] - keys and values to set :param session: Optional[ClientSession] - pymongo session :return: self """ return self.update(Set(expression), session=session, **kwargs)
async def test_update_self(preset_documents): sample = await Sample.find_one(Sample.integer == 1) await sample.update(Set({Sample.integer: 100})) assert sample.integer == 100 result = await Sample.find_many(Sample.integer == 100).to_list() assert len(result) == 1 assert result[0].integer == 100
async def test_update_query(): q = (Sample.find_many(Sample.integer == 1).update(Set({Sample.integer: 10})).update_query) assert q == {"$set": {"integer": 10}} q = (Sample.find_many(Sample.integer == 1).update( Max({Sample.integer: 10}), Set({Sample.optional: None})).update_query) assert q == {"$max": {"integer": 10}, "$set": {"optional": None}} q = (Sample.find_many(Sample.integer == 1).update( Set({Sample.integer: 10}), Set({Sample.optional: None})).update_query) assert q == {"$set": {"optional": None}} q = (Sample.find_many(Sample.integer == 1).update(Max( {Sample.integer: 10})).update(Set({Sample.optional: None})).update_query) assert q == {"$max": {"integer": 10}, "$set": {"optional": None}} q = (Sample.find_many(Sample.integer == 1).update(Set( {Sample.integer: 10})).update(Set({Sample.optional: None})).update_query) assert q == {"$set": {"optional": None}} with pytest.raises(TypeError): Sample.find_many(Sample.integer == 1).update(40).update_query
async def test_update_many(preset_documents): await Sample.find_many(Sample.increment > 4 ).find_many(Sample.nested.optional == None ).update(Set({Sample.increment: 100})) # noqa result = await Sample.find_many(Sample.increment == 100).to_list() assert len(result) == 3 for sample in result: assert sample.increment == 100
async def test_update_one_upsert_without_insert(preset_documents, sample_doc_not_saved): await Sample.find_one(Sample.integer > 1 ).upsert(Set({Sample.integer: 100}), on_insert=sample_doc_not_saved) await asyncio.sleep(2) new_docs = await Sample.find_many( Sample.string == sample_doc_not_saved.string).to_list() assert len(new_docs) == 0
async def test_update_many_with_session(preset_documents, session): q = (Sample.find_many( Sample.increment > 4).find_many(Sample.nested.optional == None).update( Set({Sample.increment: 100})).set_session(session=session)) assert q.session == session q = (Sample.find_many(Sample.increment > 4).find_many( Sample.nested.optional == None).update(Set({Sample.increment: 100}), session=session)) assert q.session == session q = (Sample.find_many(Sample.increment > 4).find_many( Sample.nested.optional == None, session=session).update(Set({Sample.increment: 100}))) assert q.session == session await q # noqa result = await Sample.find_many(Sample.increment == 100).to_list() assert len(result) == 3 for sample in result: assert sample.increment == 100
async def test_update(documents, document_not_inserted): await documents(5) doc = await DocumentTestModel.find_one(DocumentTestModel.test_int == 0) doc.test_int = 100 async with BulkWriter() as bulk_writer: await doc.save_changes(bulk_writer=bulk_writer) await DocumentTestModel.find_one( DocumentTestModel.test_int == 1 ).update( Set({DocumentTestModel.test_int: 1000}), bulk_writer=bulk_writer ) await DocumentTestModel.find(DocumentTestModel.test_int < 100).update( Set({DocumentTestModel.test_int: 2000}), bulk_writer=bulk_writer ) assert len(await DocumentTestModel.find_all().to_list()) == 5 assert ( len( await DocumentTestModel.find( DocumentTestModel.test_int == 100 ).to_list() ) == 1 ) assert ( len( await DocumentTestModel.find( DocumentTestModel.test_int == 1000 ).to_list() ) == 1 ) assert ( len( await DocumentTestModel.find( DocumentTestModel.test_int == 2000 ).to_list() ) == 3 )
def test_set(): q = Set({Sample.integer: 2}) assert q == {"$set": {"integer": 2}}