def test_exception_state_without_valid_location(self, state):
        s = {
            'a': 'b',
            'c': 1,
            'd': ['e', 'f', 'g'],
            'h': {
                'i': 'j',
                'k': True
            }
        }

        with pytest.raises(DataError) as e:
            StateDescriptor("", "")
        assert str(
            e.value
        ) == """{"instance_key": ["Value must not be a empty string"]}"""

        instance = StateDescriptor("test", "this")
        # set an invalid file_location for this test
        instance.file_location = ""
        assert state.persistent_state.get_state(instance) is None

        with pytest.raises(StateNotPersistedException) as e:
            state.persistent_state.set_state(instance, s)
        if platform.system() == "Windows":
            assert str(
                e.value
            ) == """[Error 3] The system cannot find the path specified: ''"""
        else:
            assert str(
                e.value) == """[Errno 2] No such file or directory: ''"""
 def test_exception_unsupported_data_type_state(self, state):
     instance = StateDescriptor("state.with.unsupported.data", ".")
     with pytest.raises(ValueError) as e:
         state.persistent_state.set_state(instance, 123)
     if PY3:
         assert str(e.value) == "Got unexpected <class 'int'> for argument state, expected dictionary " \
                                "or schematics.Model"
     else:
         assert str(e.value) == "Got unexpected <type 'int'> for argument state, expected dictionary " \
                                "or schematics.Model"
 def test_state_flushing(self, state):
     s = {
         'a': 'b',
         'c': 1,
         'd': ['e', 'f', 'g'],
         'h': {
             'i': 'j',
             'k': True
         }
     }
     instance = StateDescriptor("on.disk.state", ".")
     state.assert_state(instance, s)
    def test_state_copy_no_modification_state(self, state):
        s = TestStorageSchema({'offset': 10})
        instance = StateDescriptor("rollback.state.schema", ".")
        s = state.assert_state(instance,
                               s,
                               TestStorageSchema,
                               with_clear=False)

        # update the state in memory
        s.offset = 30

        # assert the state remains unchanged, state should have offset as 10
        state.assert_state(instance, s, TestStorageSchema)
 def test_clear_without_flushing_state(self, state):
     s = {
         'a': 'b',
         'c': 1,
         'd': ['e', 'f', 'g'],
         'h': {
             'i': 'j',
             'k': True
         }
     }
     instance = StateDescriptor("state.with.unsupported.data", ".")
     state.persistent_state.set_state(instance, s, False)
     assert state.persistent_state.clear(instance) is None
    def test_exception_corrupted_state(self, state):
        instance = StateDescriptor("state.with.corrupted.data", ".")
        # write "corrupted" data
        with open(instance.file_location, 'w') as f:
            f.write("{'a': 'b', 'c': 1, 'd':....")

        with pytest.raises(StateCorruptedException) as e:
            state.persistent_state.get_state(instance)
        if PY3:
            assert str(
                e.value
            ) == """Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"""
        else:
            assert str(
                e.value
            ) == """Expecting property name: line 1 column 2 (char 1)"""

        os.remove(instance.file_location)
 def test_state_flushing_with_schema(self, state):
     s = TestStorageSchema({'offset': 10})
     instance = StateDescriptor("on.disk.state.schema", ".")
     rs = state.assert_state(instance, s, TestStorageSchema)
     assert rs.offset == s.offset