def field_value(): return AnyField()
def test_any_field_default_value(self): field = AnyField(default=1) self.assertEqual(1, field.default)
def test_any_field_default_callable(self): field = AnyField(default=lambda: 1) self.assertEqual(1, field.default)
def test_any_field_dumps_enforce_type(self): field = AnyField(enforce_type=True) for value in [1, 1.2, True, "foo", object(), {}, []]: self.assertEqual(value, field.dumps(value))
def test_any_field_default_default_None(self): field = AnyField() self.assertIsNone(field.default)
def get_field(self, field_name: str) -> BaseField: field_name = self._datastore_mappings.get(field_name, field_name) return self._mappings.get(field_name, AnyField())
def test_any_field_loads(self): field = AnyField() for value in [1, 1.2, True, "foo", object(), {}, []]: self.assertEqual(value, field.loads(value))
def test_enforce_variable_incorrect_comparison_raises_value_error(mocker): mocker.patch('datastorm.fields.AnyField.check_type', return_value=False) field = AnyField(enforce_type=True) with pytest.raises(ValueError): field == "test"
def test_callable_default(): default_value = "test" field = AnyField(default=lambda: default_value) assert field.default == default_value
def test_no_enforce_variable_no_checks_variable_type(mocker): mock_check_type = mocker.patch('datastorm.fields.AnyField.check_type') field = AnyField(enforce_type=False) field.dumps("test") assert not mock_check_type.called
def test_enforce_variable_checks_variable_type(mocker): mock_check_type = mocker.patch('datastorm.fields.AnyField.check_type', return_value=True) field = AnyField(enforce_type=True) field.dumps("test") assert mock_check_type.called
def test_value_default(): default_value = "test" field = AnyField(default=default_value) assert field.default == default_value
def test_projected_query_has_same_order(qbuilder): orders = ["a", AnyField("b")] qbuilder.order(orders[0], False) qbuilder.order(orders[1], True) assert qbuilder.only("test")._order == qbuilder._order
def test_filter_adds_filters(qbuilder): new_filters = [1, 2, 3] qbuilder.filter(*new_filters) assert qbuilder._filters == new_filters def test_filter_empty_no_add(qbuilder): qbuilder.filter() assert qbuilder._filters == [] @pytest.mark.parametrize("field, inverted, expected", [("test", False, "test"), ("test", True, "-test"), (AnyField(field_name="test"), False, "test"), (AnyField(field_name="test"), True, "-test"), ]) def test_order(qbuilder, field, inverted, expected): qbuilder.order(field, inverted) assert qbuilder._order == [expected] def test_only_returns_projected_query(qbuilder): assert isinstance(qbuilder.only("test"), ProjectedQueryBuilder) def test_projected_query_has_same_instance(qbuilder): assert qbuilder.only("test")._entity_class == qbuilder._entity_class