def test_delete(self): request = testing.DummyRequest() request.matchdict = { 'name': os.path.basename(self.workspace.working_dir), 'content_type': fqcn(TestPerson), 'uuid': self.person.uuid, } resource = ContentTypeResource(request) with patch.object(request.registry, 'notify') as mocked_notify: object_data = resource.delete() self.assertEqual(TestPerson(object_data), self.person) (event, ) = mocked_notify.call_args[0] self.assertIsInstance(event, ContentTypeObjectUpdated) self.assertEqual(event.repo, self.workspace.repo) self.assertEqual(event.model, self.person) self.assertEqual(event.change_type, 'delete') request = testing.DummyRequest({}) request.matchdict = { 'name': os.path.basename(self.workspace.working_dir), 'content_type': fqcn(TestPerson), 'uuid': self.person.uuid, } resource = ContentTypeResource(request) self.assertRaises(NotFound, resource.get)
def resync(self, working_dir, index_prefix, model_class, mapping=None, recreate_index=False, es={}): workspace = EG.workspace(working_dir, index_prefix=index_prefix, es=es) branch = workspace.sm.repo.active_branch if recreate_index and workspace.im.index_exists(branch.name): self.stdout.writelines( 'Destroying index for %s.\n' % (branch.name,)) workspace.im.destroy_index(branch.name) if not workspace.im.index_exists(branch.name): self.stdout.writelines( 'Creating index for %s.\n' % (branch.name,)) # create the index and wait for it to become ready workspace.im.create_index(branch.name) while not workspace.index_ready(): pass if mapping is not None: self.stdout.writelines( 'Creating mapping for %s.\n' % (fqcn(model_class),)) workspace.setup_custom_mapping(model_class, mapping) updated, removed = workspace.sync(model_class) self.stdout.writelines('%s: %d updated, %d removed.\n' % ( fqcn(model_class), len(updated), len(removed)))
def test_collection(self): request = testing.DummyRequest({}) request.matchdict = { 'name': os.path.basename(self.workspace.working_dir), 'content_type': fqcn(TestPerson), } resource = ContentTypeResource(request) content_type_json = resource.collection_get() self.assertEqual( content_type_json, format_content_type(self.workspace.repo, fqcn(TestPerson)))
def test_get(self): request = testing.DummyRequest({}) request.matchdict = { 'name': os.path.basename(self.workspace.working_dir), 'content_type': fqcn(TestPerson), 'uuid': self.person.uuid, } resource = ContentTypeResource(request) object_json = resource.get() self.assertEqual( object_json, format_content_type_object(self.workspace.repo, fqcn(TestPerson), self.person.uuid))
def sync_data(self, workdir, model_class, verbose=False, clobber=False): self.verbose = verbose workdir = EG.workspace( workdir, index_prefix=os.path.basename(workdir)) self.emit('Syncing data for %s.' % (fqcn(model_class),)) workdir.sync(model_class) self.emit('Data synced.')
def test_iterate(self): with patch.object(self.rsm, 'mk_request') as mock: response = Response() response.encoding = 'utf-8' response._content = json.dumps([{ 'uuid': 'person1', 'age': 1, 'name': 'person1' }, { 'uuid': 'person2', 'age': 2, 'name': 'person2' }]) mock.return_value = response person1, person2 = self.rsm.iterate(TestPerson) self.assertEqual(person1.uuid, 'person1') self.assertEqual(person1.age, 1) self.assertEqual(person1.name, 'person1') self.assertTrue(person1.is_read_only()) self.assertEqual(person2.uuid, 'person2') self.assertEqual(person2.age, 2) self.assertEqual(person2.name, 'person2') self.assertTrue(person2.is_read_only()) mock.assert_called_with( 'GET', 'http://www.example.org/repos/foo/%s.json' % ( fqcn(TestPerson),))
def create_mapping(self, workdir, model_class, mapping, verbose=False): self.verbose = verbose workspace = EG.workspace( workdir, index_prefix=os.path.basename(workdir)) self.emit('Creating mapping for %s.' % (fqcn(model_class),)) workspace.setup_custom_mapping(model_class, mapping) self.emit('Mapping created.')
def test_validate_schema_valid(self): self.request.matchdict = { 'name': os.path.basename(self.workspace.working_dir), 'content_type': fqcn(TestPerson), 'uuid': self.person.uuid, } self.request.body = json.dumps(dict(self.person)) validate_schema(self.request) self.assertEqual(self.request.errors, [])
def test_get_404(self): request = testing.DummyRequest({}) request.matchdict = { 'name': os.path.basename(self.workspace.working_dir), 'content_type': fqcn(TestPerson), 'uuid': 'does not exist', } resource = ContentTypeResource(request) self.assertRaises(NotFound, resource.get)
def test_validate_schema_invalid_uuid(self): self.request.matchdict = { 'name': os.path.basename(self.workspace.working_dir), 'content_type': fqcn(TestPerson), 'uuid': 'foo', } self.request.body = json.dumps(dict(self.person)) validate_schema(self.request) self.assertEqual( self.request.errors, [{ 'location': 'body', 'name': 'uuid', 'description': 'Payload UUID does not match URL UUID.'}])
def load_schema(self, data, field_mapping={}, model_renames={}): loader = self.mk_schema_loader() tmp_file = self.mk_tempfile(json.dumps(data, indent=2)) with open(tmp_file, 'r') as fp: loader.run([fp], field_mappings=[ FieldMapType('%s=%s' % (key, fqcn(value))) for key, value in field_mapping.items() ], model_renames=[ RenameType('%s=%s' % (old, new)) for old, new in model_renames.items() ]) return loader.stdout.getvalue()
def load_schema(self, data, field_mapping={}, model_renames={}): loader = self.mk_schema_loader() tmp_file = self.mk_tempfile(json.dumps(data, indent=2)) with open(tmp_file, 'r') as fp: loader.run( [fp], field_mappings=[ FieldMapType('%s=%s' % (key, fqcn(value))) for key, value in field_mapping.items() ], model_renames=[ RenameType('%s=%s' % (old, new)) for old, new in model_renames.items() ]) return loader.stdout.getvalue()
def test_validate_schema_invalid(self): self.request.matchdict = { 'name': os.path.basename(self.workspace.working_dir), 'content_type': fqcn(TestPerson), 'uuid': self.person.uuid, } self.request.body = json.dumps({'age': 'twenty two'}) validate_schema(self.request) self.assertEqual( self.request.errors, [{ 'location': 'body', 'name': 'schema', 'description': ( 'Data does not match the schema for ' 'elasticgit.tests.base.TestPerson') }])
def test_get(self): with patch.object(self.rsm, 'mk_request') as mock: response = Response() response.encoding = 'utf-8' response._content = json.dumps({ 'uuid': 'person1', 'age': 1, 'name': 'person1' }) mock.return_value = response person1 = self.rsm.get(TestPerson, 'person1') self.assertEqual(person1.uuid, 'person1') self.assertEqual(person1.age, 1) self.assertEqual(person1.name, 'person1') mock.assert_called_with( 'GET', 'http://www.example.org/repos/foo/%s/person1.json' % ( fqcn(TestPerson),))
def test_put(self): request = testing.DummyRequest() request.schema = avro.schema.parse(serialize(TestPerson)).to_json() request.schema_data = dict(self.person) request.matchdict = { 'name': os.path.basename(self.workspace.working_dir), 'content_type': fqcn(TestPerson), 'uuid': self.person.uuid, } request.registry = self.config.registry resource = ContentTypeResource(request) with patch.object(request.registry, 'notify') as mocked_notify: object_data = resource.put() self.assertEqual(TestPerson(object_data), self.person) (event, ) = mocked_notify.call_args[0] self.assertIsInstance(event, ContentTypeObjectUpdated) self.assertEqual(event.repo, self.workspace.repo) self.assertEqual(event.model, self.person) self.assertEqual(event.change_type, 'update')
def test_url(self): class_name = fqcn(TestPerson) self.assertEqual( self.rsm.url(class_name), 'http://www.example.org/repos/foo/%s.json' % (class_name,))
def get(self, model_class, uuid): response = self.mk_request('GET', self.url(fqcn(model_class), uuid)) response.raise_for_status() return model_class(response.json()).set_read_only()
def sync_data(self, workdir, model_class, verbose=False, clobber=False): self.verbose = verbose workdir = EG.workspace(workdir, index_prefix=os.path.basename(workdir)) self.emit('Syncing data for %s.' % (fqcn(model_class), )) workdir.sync(model_class) self.emit('Data synced.')
def iterate(self, model_class): response = self.mk_request('GET', self.url(fqcn(model_class))) response.raise_for_status() return [model_class(obj).set_read_only() for obj in response.json()]