def test_raises(self): with smart_assert.raises(Exception) as context: raise Exception('This should not show up') with self.assertRaises(ValueError) as _context: with smart_assert.raises(Exception) as context: raise ValueError('This should show up')
def test_choices_property(self): prop = venom.Properties.ChoicesProperty(choices=[1, 2, 3, '4']) with smart_assert.raises(venom.Properties.PropertyValidationFailed) as context: prop.validate(None, 4) prop.validate(None, None) with smart_assert.raises() as context: prop.validate(None, 1) prop.validate(None, 2) prop.validate(None, 3) prop.validate(None, '4')
def test_choices_property(self): prop = venom.Properties.ChoicesProperty(choices=[1, 2, 3, '4']) with smart_assert.raises( venom.Properties.PropertyValidationFailed) as context: prop.validate(None, 4) prop.validate(None, None) with smart_assert.raises() as context: prop.validate(None, 1) prop.validate(None, 2) prop.validate(None, 3) prop.validate(None, '4')
def test_base_validatation(self): class TestProp(venom.Properties.Property): pass prop = TestProp(required=True) with smart_assert.raises() as context: prop.validate(None, 123) with smart_assert.raises(venom.Properties.PropertyValidationFailed) as context: prop.validate(None, None) prop = TestProp(required=False) with smart_assert.raises() as context: prop.validate(None, 123) prop.validate(None, None)
def test_base_validatation(self): class TestProp(venom.Properties.Property): pass prop = TestProp(required=True) with smart_assert.raises() as context: prop.validate(None, 123) with smart_assert.raises( venom.Properties.PropertyValidationFailed) as context: prop.validate(None, None) prop = TestProp(required=False) with smart_assert.raises() as context: prop.validate(None, 123) prop.validate(None, None)
def test_check_base_class_fails(self): foo = PropComparisonTestProp() foo._connect(name='foo') bar = PropComparisonTestProp() bar._connect(name='bar') operator = venom.QueryLogicalOperator(foo == 123, bar == 456) with smart_assert.raises(ValueError) as context: operator.to_search_query([])
def __test_string_property(self, prop_cls): prop = prop_cls(min=2, max=8, choices=['foo', 'bar', 'baz'], required=True, characters='fobar') assert prop.min == 2 assert prop.max == 8 assert prop.choices == ['foo', 'bar', 'baz'] assert prop.required == True assert prop.characters == 'fobar' with smart_assert.raises(venom.Properties.PropertyValidationFailed) as context: prop.validate(None, 'a') prop.validate(None, 'foobar') prop.validate(None, 'baz') prop.validate(None, 'aaaaaaaaa') with smart_assert.raises() as context: prop.validate(None, 'foo') prop.validate(None, u'bar') assert isinstance(prop._from_storage(unicode('bar')), str)
def test_integer_property(self): prop = venom.Properties.Integer(min=2, max=8, choices=[1, 3, 4, 9], required=True) assert prop.min == 2 assert prop.max == 8 assert prop.choices == [1, 3, 4, 9] assert prop.required == True with smart_assert.raises(venom.Properties.PropertyValidationFailed) as context: prop.validate(None, 1) prop.validate(None, 9) with smart_assert.raises() as context: prop.validate(None, 3) prop.validate(None, 4) assert prop._from_storage(4.000001) == 4 assert prop.to_search_field() == search.NumberField assert prop.to_datastore_property() == ndb.IntegerProperty
def test_args_only(self): param = venom.QueryParameter() args = ['foo', 'bar', 'baz'] assert param.get_value(args) == 'foo' assert param.get_value(args) == 'bar' assert param.get_value(args) == 'baz' assert args == [] with smart_assert.raises(IndexError) as context: param.get_value(args)
def test_setting_properties(self): test = TestDynamic() with smart_assert.raises(AttributeError) as context: test.foo test.set('foo', 123, ndb.IntegerProperty) assert test.foo == 123 test.foo = 456 assert test.foo == 456 assert 'foo' in test._properties assert test._properties['foo']._get_value(test) == 456 test.set('bar', 789, ndb.IntegerProperty(indexed=True)) assert test.bar == 789
def test_validate_on_set(self): class ModelStub(venom.Model): prop = venom.Properties.Property(required=True) with smart_assert.raises(venom.Properties.PropertyValidationFailed) as context: model = ModelStub() model.prop = None class ModelStub(venom.Model): prop = venom.Properties.Property(required=False) model = ModelStub() model.prop = None
def test_integer_property(self): prop = venom.Properties.Integer(min=2, max=8, choices=[1, 3, 4, 9], required=True) assert prop.min == 2 assert prop.max == 8 assert prop.choices == [1, 3, 4, 9] assert prop.required == True with smart_assert.raises( venom.Properties.PropertyValidationFailed) as context: prop.validate(None, 1) prop.validate(None, 9) with smart_assert.raises() as context: prop.validate(None, 3) prop.validate(None, 4) assert prop._from_storage(4.000001) == 4 assert prop.to_search_field() == search.NumberField assert prop.to_datastore_property() == ndb.IntegerProperty
def test_validate_on_set(self): class ModelStub(venom.Model): prop = venom.Properties.Property(required=True) with smart_assert.raises( venom.Properties.PropertyValidationFailed) as context: model = ModelStub() model.prop = None class ModelStub(venom.Model): prop = venom.Properties.Property(required=False) model = ModelStub() model.prop = None
def __test_string_property(self, prop_cls): prop = prop_cls(min=2, max=8, choices=['foo', 'bar', 'baz'], required=True, characters='fobar') assert prop.min == 2 assert prop.max == 8 assert prop.choices == ['foo', 'bar', 'baz'] assert prop.required == True assert prop.characters == 'fobar' with smart_assert.raises( venom.Properties.PropertyValidationFailed) as context: prop.validate(None, 'a') prop.validate(None, 'foobar') prop.validate(None, 'baz') prop.validate(None, 'aaaaaaaaa') with smart_assert.raises() as context: prop.validate(None, 'foo') prop.validate(None, u'bar') assert isinstance(prop._from_storage(unicode('bar')), str)
def test_queries(self): entity = TestHybrid() entity.set('foo', 123, ndb.IntegerProperty) entity.set('foo', 123, search.NumberField) entity.set('bar', 'baz', ndb.StringProperty) entity.set('baz', 456, search.NumberField) entity.put() # query by search on mutual property entities = TestHybrid.query_by_search('foo = 123') assert len(entities) == 1 entity = entities[0] assert entity.datastore_entity.get_entity().foo == 123 assert entity.datastore_entity.get_entity().bar == 'baz' # query by search on ndb only property entities = TestHybrid.query_by_search('bar = baz') assert len(entities) == 0 # query by ndb on ndb only property entities = TestHybrid.query_by_datastore( ndb.GenericProperty('bar') == 'baz') assert len(entities) == 1 entity = entities[0] assert entity.datastore_entity.get_entity().foo == 123 assert entity.datastore_entity.get_entity().bar == 'baz' # query by search on search only property entities = TestHybrid.query_by_search('baz = 456') assert len(entities) == 1 entity = entities[0] assert entity.datastore_entity.get_entity().foo == 123 assert entity.datastore_entity.get_entity().bar == 'baz' # since baz exists only on search documents # it cannot be found on a returned entity from the ndb with smart_assert.raises(AttributeError) as context: entity.datastore_entity.get_entity().baz # query by ndb on search only property entities = TestHybrid.query_by_datastore( ndb.GenericProperty('baz') == 456) assert len(entities) == 0
def test_queries(self): entity = TestHybrid() entity.set('foo', 123, ndb.IntegerProperty) entity.set('foo', 123, search.NumberField) entity.set('bar', 'baz', ndb.StringProperty) entity.set('baz', 456, search.NumberField) entity.put() # query by search on mutual property entities = TestHybrid.query_by_search('foo = 123') assert len(entities) == 1 entity = entities[0] assert entity.datastore_entity.get_entity().foo == 123 assert entity.datastore_entity.get_entity().bar == 'baz' # query by search on ndb only property entities = TestHybrid.query_by_search('bar = baz') assert len(entities) == 0 # query by ndb on ndb only property entities = TestHybrid.query_by_datastore(ndb.GenericProperty('bar') == 'baz') assert len(entities) == 1 entity = entities[0] assert entity.datastore_entity.get_entity().foo == 123 assert entity.datastore_entity.get_entity().bar == 'baz' # query by search on search only property entities = TestHybrid.query_by_search('baz = 456') assert len(entities) == 1 entity = entities[0] assert entity.datastore_entity.get_entity().foo == 123 assert entity.datastore_entity.get_entity().bar == 'baz' # since baz exists only on search documents # it cannot be found on a returned entity from the ndb with smart_assert.raises(AttributeError) as context: entity.datastore_entity.get_entity().baz # query by ndb on search only property entities = TestHybrid.query_by_datastore(ndb.GenericProperty('baz') == 456) assert len(entities) == 0
def test_invalid_comparison(self): prop = venom.Properties.Property() with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop == 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop < 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop <= 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop > 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop >= 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop != 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop.contains(1) class TestProp(venom.Properties.Property): allowed_operators = frozenset([ venom.Properties.PropertyComparison.EQ, venom.Properties.PropertyComparison.NE ]) def query_uses_datastore(self, operator, value): return operator == venom.Properties.PropertyComparison.EQ def to_search_field(self, operator, value): return 'search' def to_datastore_property(self, operator, value): return 'datastore' prop = TestProp() with smart_assert.raises() as context: prop == 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop < 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop <= 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop > 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop >= 1 with smart_assert.raises() as context: prop != 1 with smart_assert.raises(venom.Properties.InvalidPropertyComparison) as context: prop.contains(1)
def test_invalid_comparison(self): prop = venom.Properties.Property() with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop == 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop < 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop <= 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop > 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop >= 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop != 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop.contains(1) class TestProp(venom.Properties.Property): allowed_operators = frozenset([ venom.Properties.PropertyComparison.EQ, venom.Properties.PropertyComparison.NE ]) def query_uses_datastore(self, operator, value): return operator == venom.Properties.PropertyComparison.EQ def to_search_field(self, operator, value): return 'search' def to_datastore_property(self, operator, value): return 'datastore' prop = TestProp() with smart_assert.raises() as context: prop == 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop < 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop <= 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop > 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop >= 1 with smart_assert.raises() as context: prop != 1 with smart_assert.raises( venom.Properties.InvalidPropertyComparison) as context: prop.contains(1)