import pkg_resources # create the api prov_api = ProvApi(adapter=SimpleInMemoryAdapter, auth_info=None) # create the prov document from examples prov_document_buffer = pkg_resources.resource_stream( "examples", "file_buffer_example_primer.json") # Save document document_id = prov_api.create_document(prov_document_buffer) # This is similar to: # prov_api.create_document_from_json(prov_document_buffer) # get document print(prov_api.get_document_as_provn(document_id)) # Output: # document # prefix # foaf < http: // xmlns.com / foaf / 0.1 / > # prefix # dcterms < http: // purl.org / dc / terms / > # prefix # ex < http: // example / > # # specializationOf(ex:articleV2, ex:article) # specializationOf(ex:articleV1, ex:article) # wasDerivedFrom(ex:blogEntry, ex:article, -, -, -, [prov:type = 'prov:Quotation']) # alternateOf(ex:articleV2, ex:articleV1)
class ProvApiTests(unittest.TestCase): maxDiff = None def setUp(self): self.test_resources = { 'xml': { 'package': 'provdbconnector', 'file': '/tests/resources/primer.provx' }, 'json': { 'package': 'provdbconnector', 'file': '/tests/resources/primer.json' }, 'provn': { 'package': 'provdbconnector', 'file': '/tests/resources/primer.provn' } } self.test_prov_files = dict( (key, pkg_resources.resource_stream(val['package'], val['file'])) for key, val in self.test_resources.items()) self.auth_info = { "user_name": NEO4J_USER, "user_password": NEO4J_PASS, "host": NEO4J_HOST + ":" + NEO4J_BOLT_PORT } self.provapi = ProvApi(api_id=1, adapter=Neo4jAdapter, auth_info=self.auth_info) def tearDown(self): [self.test_prov_files[k].close() for k in self.test_prov_files.keys()] # Test create instnace def test_provapi_instance(self): self.assertRaises(NoDataBaseAdapterException, lambda: ProvApi()) self.assertRaises(InvalidOptionsException, lambda: ProvApi(api_id=1, adapter=Neo4jAdapter)) obj = ProvApi(api_id=1, adapter=Neo4jAdapter, auth_info=self.auth_info) self.assertIsInstance(obj, ProvApi) self.assertEqual(obj.api_id, 1) obj = ProvApi(adapter=Neo4jAdapter, auth_info=self.auth_info) self.assertIsInstance(obj.api_id, UUID) # Methods that automatically convert to ProvDocument def test_create_document_from_json(self): json_buffer = self.test_prov_files["json"] self.provapi.create_document_from_json(json_buffer) def test_get_document_as_json(self): example = examples.primer_example() document_id = self.provapi.create_document_from_prov(example) prov_str = self.provapi.get_document_as_json(document_id) self.assertIsNotNone(prov_str) self.assertIsInstance(prov_str, str) prov_document_reverse = ProvDocument.deserialize(content=prov_str, format="json") self.assertEqual(prov_document_reverse, example) def test_create_document_from_xml(self): json_buffer = self.test_prov_files["xml"] self.provapi.create_document_from_json(json_buffer) def test_get_document_as_xml(self): example = examples.primer_example() document_id = self.provapi.create_document_from_prov(example) prov_str = self.provapi.get_document_as_xml(document_id) self.assertIsNotNone(prov_str) self.assertIsInstance(prov_str, str) prov_document_reverse = ProvDocument.deserialize(content=prov_str, format="xml") self.assertEqual(prov_document_reverse, example) def test_create_document_from_provn(self): json_buffer = self.test_prov_files["provn"] with self.assertRaises(NotImplementedError): self.provapi.create_document_from_provn(json_buffer) def test_get_document_as_provn(self): example = examples.primer_example() document_id = self.provapi.create_document_from_prov(example) prov_str = self.provapi.get_document_as_provn(document_id) self.assertIsNotNone(prov_str) self.assertIsInstance(prov_str, str) # This check throws NotImplementedError, so skip it # prov_document_reverse = ProvDocument.deserialize(content=prov_str,format="provn") # self.assertEqual(prov_document_reverse, example) # Methods with ProvDocument input / output def test_create_document(self): # test prov document input example = examples.primer_example() document_id = self.provapi.create_document_from_prov(example) self.assertIsNotNone(document_id) self.assertIsInstance(document_id, str) # test invalid options input with self.assertRaises(InvalidArgumentTypeException): self.provapi.create_document(1) def test_create_document_from_prov(self): example = examples.primer_example() document_id = self.provapi.create_document_from_prov(example) self.assertIsNotNone(document_id) self.assertIsInstance(document_id, str) def test_create_document_from_prov_alternate(self): example = examples.primer_example_alternate() document_id = self.provapi.create_document_from_prov(example) self.assertIsNotNone(document_id) self.assertIsInstance(document_id, str) def test_create_document_from_prov_bundles(self): example = examples.bundles1() document_id = self.provapi.create_document_from_prov(example) self.assertIsNotNone(document_id) self.assertIsInstance(document_id, str) def test_create_document_from_prov_bundles2(self): example = examples.bundles2() document_id = self.provapi.create_document_from_prov(example) self.assertIsNotNone(document_id) self.assertIsInstance(document_id, str) def test_create_document_from_prov_invalid_arguments(self): with self.assertRaises(InvalidArgumentTypeException): self.provapi.create_document_from_prov(None) def test_get_document_as_prov(self): example = examples.bundles2() document_id = self.provapi.create_document_from_prov(example) prov_document = self.provapi.get_document_as_prov(document_id) self.assertIsNotNone(prov_document) self.assertIsInstance(prov_document, ProvDocument) self.assertEqual(prov_document, example) def test_get_document_as_prov_invalid_arguments(self): with self.assertRaises(InvalidArgumentTypeException): self.provapi.get_document_as_prov() def test_create_bundle_invalid_arguments(self): with self.assertRaises(InvalidArgumentTypeException): self.provapi._create_bundle("xxxx", None) def test_get_metadata_and_attributes_for_record_invalid_arguments(self): with self.assertRaises(InvalidArgumentTypeException): self.provapi._get_metadata_and_attributes_for_record(None) def test_get_metadata_and_attributes_for_record(self): example = examples.prov_api_record_example() result = self.provapi._get_metadata_and_attributes_for_record( example.prov_record) metadata_result = result.metadata attributes_result = result.attributes self.assertIsNotNone(result.attributes) self.assertIsNotNone(result.metadata) self.assertIsInstance(result.attributes, dict) self.assertIsInstance(result.metadata, dict) self.assertIsNotNone(metadata_result[METADATA_KEY_PROV_TYPE]) self.assertIsNotNone(metadata_result[METADATA_KEY_IDENTIFIER]) self.assertIsNotNone(metadata_result[METADATA_KEY_NAMESPACES]) self.assertIsNotNone(metadata_result[METADATA_KEY_TYPE_MAP]) # check metadata self.assertEqual(example.metadata[METADATA_KEY_PROV_TYPE], metadata_result[METADATA_KEY_PROV_TYPE]) self.assertEqual(example.metadata[METADATA_KEY_IDENTIFIER], metadata_result[METADATA_KEY_IDENTIFIER]) self.assertEqual(example.metadata[METADATA_KEY_NAMESPACES], metadata_result[METADATA_KEY_NAMESPACES]) self.assertEqual(example.metadata[METADATA_KEY_TYPE_MAP], metadata_result[METADATA_KEY_TYPE_MAP]) # check attributes self.assertEqual(example.expected_attributes, attributes_result)