def test_saved_state_needed():
    doc_1 = DocumentWithTurnedOffStateManagement(num_1=1, num_2=2)
    with pytest.raises(StateManagementIsTurnedOff):
        doc_1.is_changed

    doc_2 = DocumentWithTurnedOnStateManagement(num_1=1, num_2=2)
    with pytest.raises(StateNotSaved):
        doc_2.is_changed
def test_parse_object_with_saving_state():
    obj = {
        "num_1": 1,
        "num_2": 2,
        "_id": ObjectId(),
        "internal": InternalDoc(),
    }
    doc = DocumentWithTurnedOnStateManagement._parse_obj_saving_state(obj)
    assert doc.get_saved_state() == obj
async def test_find_many():
    docs = []
    for i in range(10):
        docs.append(DocumentWithTurnedOnStateManagement(num_1=i, num_2=i + 1))
    await DocumentWithTurnedOnStateManagement.insert_many(docs)

    found_docs = await DocumentWithTurnedOnStateManagement.find(
        DocumentWithTurnedOnStateManagement.num_1 > 4).to_list()

    for doc in found_docs:
        assert doc.get_saved_state() is not None
def test_save_state():
    doc = DocumentWithTurnedOnStateManagement(
        num_1=1, num_2=2, internal=InternalDoc(num=1, string="s")
    )
    assert doc.get_saved_state() is None
    doc._save_state()
    assert doc.get_saved_state() == {
        "num_1": 1,
        "num_2": 2,
        "internal": {"num": 1, "string": "s", "lst": [1, 2, 3, 4, 5]},
    }
def test_use_state_management_property():
    assert DocumentWithTurnedOnStateManagement.use_state_management() is True
    assert DocumentWithTurnedOffStateManagement.use_state_management() is False
def doc_default(state):
    return DocumentWithTurnedOnStateManagement._parse_obj_saving_state(state)
async def test_insert(state):
    doc = DocumentWithTurnedOnStateManagement.parse_obj(state)
    assert doc.get_saved_state() is None
    await doc.insert()
    assert doc.get_saved_state() == state
def test_save_state():
    doc = DocumentWithTurnedOnStateManagement(num_1=1, num_2=2)
    assert doc.get_saved_state() is None
    doc._save_state()
    assert doc.get_saved_state() == {"num_1": 1, "num_2": 2}