예제 #1
0
def assert_db(db_dict):
    def assert_model(model, model_dict):
        def raise_assertion_error(key, actual_value, expected_value):
            raise AssertionError(
                f'Value of `{key}` is `{actual_value}` but should be `{expected_value}`',
            )

        for key, expected_value in model_dict.items():
            actual_value = getattr(model, key)

            if isinstance(expected_value, datetime.datetime):
                if abs(actual_value - expected_value) > TIME_TOLERANCE:
                    raise_assertion_error(key, actual_value, expected_value)
            elif hasattr(expected_value,
                         'match'):  # Regular expression pattern.
                if expected_value.match(actual_value) is None:
                    raise AssertionError(
                        f'Value of `{key}` is `{actual_value}` and doesn\'t match'
                        f' `{expected_value}`', )
            elif actual_value != expected_value:
                raise_assertion_error(key, actual_value, expected_value)

    for model_name, models_dicts in db_dict.items():
        if model_name == 'strangers':
            for stranger_dict in models_dicts:
                stranger_instance = Stranger.get(id=stranger_dict['id'])
                assert_model(stranger_instance, stranger_dict)
        elif model_name == 'talks':
            for talk_dict, talk_instance in zip(models_dicts, Talk.select()):
                assert_model(talk_instance, talk_dict)
        else:
            raise AssertionError(f'Unknown model name: `{model_name}`')
예제 #2
0
 def test_delete_old__0(self):
     Talk.delete_old(datetime.datetime(2010, 1, 2, 12))
     self.assertEqual(
         list(Talk.select().order_by(Talk.begin)),
         [
             self.talk_0,
             self.talk_1,
             self.talk_4,
             ],
         )
예제 #3
0
 def test_delete_old__0(self):
     Talk.delete_old(datetime.datetime(2010, 1, 2, 12))
     self.assertEqual(
         list(Talk.select().order_by(Talk.begin)),
         [
             self.talk_0,
             self.talk_1,
             self.talk_4,
         ],
     )