def test_duplicate_field_name(self, mock_reg): from ramses import models schema = self._test_schema() schema['properties']['_public_fields'] = {'type': 'interval'} mock_reg.mget.return_value = {'foo': 'bar'} models.generate_model_cls( schema=schema, model_name='Story', raml_resource=1) assert not models.engine.IntervalField.called
def test_unknown_field_type(self, mock_reg): from ramses import models schema = self._test_schema() schema['properties']['progress'] = {'type': 'foobar'} mock_reg.mget.return_value = {'foo': 'bar'} with pytest.raises(ValueError) as ex: models.generate_model_cls( schema=schema, model_name='Story', raml_resource=None) assert str(ex.value) == 'Unknown type: foobar'
def test_relationship_field(self, mock_prep, mock_reg): from ramses import models schema = self._test_schema() schema['properties']['progress'] = { 'type': 'relationship', 'args': {'document': 'FooBar'} } mock_reg.mget.return_value = {'foo': 'bar'} models.generate_model_cls( schema=schema, model_name='Story', raml_resource=1) mock_prep.assert_called_once_with('progress', 'FooBar', 1)
def test_duplicate_field_name( self, mock_reg, mock_subscribers, mock_proc): from ramses import models config = config_mock() schema = self._test_schema() schema['properties']['_public_fields'] = { '_db_settings': {'type': 'interval'}} mock_reg.mget.return_value = {'foo': 'bar'} models.generate_model_cls( config, schema=schema, model_name='Story', raml_resource=1) assert not models.engine.IntervalField.called
def test_unknown_field_type( self, mock_reg, mock_subscribers, mock_proc): from ramses import models config = config_mock() schema = self._test_schema() schema['properties']['progress'] = { '_db_settings': {'type': 'foobar'}} mock_reg.mget.return_value = {'foo': 'bar'} with pytest.raises(ValueError) as ex: models.generate_model_cls( config, schema=schema, model_name='Story', raml_resource=None) assert str(ex.value) == 'Unknown type: foobar'
def test_list_field(self, mock_reg): from ramses import models schema = self._test_schema() schema['properties']['progress'] = { "type": "list", "args": { "item_type": "integer" } } mock_reg.mget.return_value = {'foo': 'bar'} models.generate_model_cls( schema=schema, model_name='Story', raml_resource=1) models.engine.ListField.assert_called_once_with( required=False, item_type=models.engine.IntegerField, before_validation=[], after_validation=[])
def test_foreignkey_field(self, mock_reg): from ramses import models schema = self._test_schema() schema['properties']['progress'] = { "type": "foreign_key", "args": { "ref_column_type": "string" } } mock_reg.mget.return_value = {'foo': 'bar'} models.generate_model_cls( schema=schema, model_name='Story', raml_resource=1) models.engine.ForeignKeyField.assert_called_once_with( required=False, ref_column_type=models.engine.StringField, before_validation=[], after_validation=[])
def test_list_field(self, mock_reg, mock_subscribers, mock_proc): from ramses import models config = config_mock() schema = self._test_schema() schema['properties']['progress'] = { "_db_settings": { "type": "list", "item_type": "integer" } } mock_reg.mget.return_value = {'foo': 'bar'} models.generate_model_cls( config, schema=schema, model_name='Story', raml_resource=1) models.engine.ListField.assert_called_once_with( required=False, item_type=models.engine.IntegerField)
def test_simple_case(self, mock_reg): from ramses import models schema = self._test_schema() schema['properties']['progress'] = { "required": True, "type": "float", "args": { "default": 0, "before_validation": ["zoo"], "after_validation": ["foo"] } } mock_reg.get.return_value = 1 mock_reg.mget.return_value = {'foo': 'bar'} model_cls, auth_model = models.generate_model_cls( schema=schema, model_name='Story', raml_resource=None) assert not auth_model assert model_cls.__name__ == 'Story' assert hasattr(model_cls, 'progress') assert model_cls.__tablename__ == 'story' assert model_cls._public_fields == ['public_field1'] assert model_cls._auth_fields == ['auth_field1'] assert model_cls._nested_relationships == ['nested_field1'] assert model_cls.foo == 'bar' assert issubclass(model_cls, models.engine.ESBaseDocument) assert not issubclass(model_cls, models.AuthModelDefaultMixin) models.engine.FloatField.assert_called_once_with( default=0, required=True, before_validation=[1], after_validation=[1]) mock_reg.get.assert_has_calls([call('zoo'), call('foo')]) mock_reg.mget.assert_called_once_with('Story')
def test_foreignkey_field( self, mock_reg, mock_subscribers, mock_proc): from ramses import models config = config_mock() schema = self._test_schema() schema['properties']['progress'] = { "_db_settings": { "type": "foreign_key", "ref_column_type": "string" } } mock_reg.mget.return_value = {'foo': 'bar'} models.generate_model_cls( config, schema=schema, model_name='Story', raml_resource=1) models.engine.ForeignKeyField.assert_called_once_with( required=False, ref_column_type=models.engine.StringField)
def test_relationship_field( self, mock_prep, mock_reg, mock_subscribers, mock_proc): from ramses import models config = Mock() schema = self._test_schema() schema['properties']['progress'] = { '_db_settings': { 'type': 'relationship', 'document': 'FooBar', } } mock_reg.mget.return_value = {'foo': 'bar'} config = config_mock() models.generate_model_cls( config, schema=schema, model_name='Story', raml_resource=1) mock_prep.assert_called_once_with( config, 'FooBar', 1)
def test_no_db_settings(self, mock_reg, mock_subscribers, mock_proc): from ramses import models config = config_mock() schema = self._test_schema() schema['properties']['progress'] = {'type': 'pickle'} mock_reg.mget.return_value = {'foo': 'bar'} model_cls, auth_model = models.generate_model_cls( config, schema=schema, model_name='Story', raml_resource=None, es_based=False) assert not models.engine.PickleField.called
def test_auth_model(self, mock_reg): from ramses import models schema = self._test_schema() schema['properties']['progress'] = {} schema['auth_model'] = True mock_reg.mget.return_value = {'foo': 'bar'} model_cls, auth_model = models.generate_model_cls( schema=schema, model_name='Story', raml_resource=None) assert auth_model assert issubclass(model_cls, models.AuthModelDefaultMixin)
def test_database_acls_option( self, mock_reg, mock_subscribers, mock_proc, guards_engine_mock): from ramses import models schema = self._test_schema() schema['properties']['progress'] = {'_db_settings': {}} schema['_auth_model'] = True mock_reg.mget.return_value = {'foo': 'bar'} config = config_mock() config.registry.database_acls = False model_cls, auth_model = models.generate_model_cls( config, schema=schema, model_name='Story1', raml_resource=None) assert not issubclass(model_cls, guards_engine_mock.DocumentACLMixin) config.registry.database_acls = True model_cls, auth_model = models.generate_model_cls( config, schema=schema, model_name='Story2', raml_resource=None) assert issubclass(model_cls, guards_engine_mock.DocumentACLMixin)
def test_db_based_model(self, mock_reg): from ramses import models schema = self._test_schema() schema['properties']['progress'] = {} mock_reg.mget.return_value = {'foo': 'bar'} model_cls, auth_model = models.generate_model_cls( schema=schema, model_name='Story', raml_resource=None, es_based=False) assert issubclass(model_cls, models.engine.BaseDocument) assert not issubclass(model_cls, models.engine.ESBaseDocument) assert not issubclass(model_cls, models.AuthModelDefaultMixin)
def test_db_based_model(self, mock_reg, mock_subscribers, mock_proc): from nefertari.authentication.models import AuthModelMethodsMixin from ramses import models config = config_mock() schema = self._test_schema() schema['properties']['progress'] = {'_db_settings': {}} mock_reg.mget.return_value = {'foo': 'bar'} model_cls, auth_model = models.generate_model_cls( config, schema=schema, model_name='Story', raml_resource=None, es_based=False) assert issubclass(model_cls, models.engine.BaseDocument) assert not issubclass(model_cls, models.engine.ESBaseDocument) assert not issubclass(model_cls, AuthModelMethodsMixin)
def test_callable_default(self, mock_res, mock_reg): from ramses import models models.engine.FloatField.reset_mock() schema = self._test_schema() schema['properties']['progress'] = { "type": "float", "args": {"default": "{{foobar}}"} } mock_res.return_value = 1 model_cls, auth_model = models.generate_model_cls( schema=schema, model_name='Story', raml_resource=None) models.engine.FloatField.assert_called_with( default=1, after_validation=[], required=False, before_validation=[]) mock_res.assert_called_once_with('{{foobar}}')
def test_callable_default( self, mock_res, mock_reg, mock_subscribers, mock_proc): from ramses import models config = config_mock() models.engine.FloatField.reset_mock() schema = self._test_schema() schema['properties']['progress'] = { "_db_settings": { "type": "float", "default": "{{foobar}}", } } mock_res.return_value = 1 model_cls, auth_model = models.generate_model_cls( config, schema=schema, model_name='Story', raml_resource=None) models.engine.FloatField.assert_called_with( default=1, required=False) mock_res.assert_called_once_with('{{foobar}}')
def test_simple_case( self, mock_res, mock_reg, mock_subscribers, mock_proc): from nefertari.authentication.models import AuthModelMethodsMixin from ramses import models config = config_mock() models.engine.FloatField.reset_mock() schema = self._test_schema() schema['properties']['progress'] = { "_db_settings": { "type": "float", "required": True, "default": 0, } } mock_reg.mget.return_value = {'foo': 'bar'} model_cls, auth_model = models.generate_model_cls( config, schema=schema, model_name='Story', raml_resource=None) assert not auth_model assert model_cls.__name__ == 'Story' assert hasattr(model_cls, 'progress') assert model_cls.__tablename__ == 'story' assert model_cls._public_fields == ['public_field1'] assert model_cls._nesting_depth == 3 assert model_cls._auth_fields == ['auth_field1'] assert model_cls._hidden_fields == ['hidden_field1'] assert model_cls._nested_relationships == ['nested_field1'] assert model_cls.foo == 'bar' assert issubclass(model_cls, models.engine.ESBaseDocument) assert not issubclass(model_cls, AuthModelMethodsMixin) models.engine.FloatField.assert_called_once_with( default=0, required=True) mock_reg.mget.assert_called_once_with('Story') mock_subscribers.assert_called_once_with( config, model_cls, schema) mock_proc.assert_called_once_with( config, model_cls, schema)
def test_simple_case(self, mock_res, mock_reg, mock_subscribers, mock_proc): from nefertari.authentication.models import AuthModelMethodsMixin from ramses import models config = config_mock() models.engine.FloatField.reset_mock() schema = self._test_schema() schema['properties']['progress'] = { "_db_settings": { "type": "float", "required": True, "default": 0, } } mock_reg.mget.return_value = {'foo': 'bar'} model_cls, auth_model = models.generate_model_cls(config, schema=schema, model_name='Story', raml_resource=None) assert not auth_model assert model_cls.__name__ == 'Story' assert hasattr(model_cls, 'progress') assert model_cls.__tablename__ == 'story' assert model_cls._public_fields == ['public_field1'] assert model_cls._nesting_depth == 3 assert model_cls._auth_fields == ['auth_field1'] assert model_cls._hidden_fields == ['hidden_field1'] assert model_cls._nested_relationships == ['nested_field1'] assert model_cls.foo == 'bar' assert issubclass(model_cls, models.engine.ESBaseDocument) assert not issubclass(model_cls, AuthModelMethodsMixin) models.engine.FloatField.assert_called_once_with(default=0, required=True) mock_reg.mget.assert_called_once_with('Story') mock_subscribers.assert_called_once_with(config, model_cls, schema) mock_proc.assert_called_once_with(config, model_cls, schema)