def setUp(self): self.app = app self.server = os.environ.get('COLLECTIONS_API_TEST_DB') if not self.server: raise EnvironmentError self.app.db = LDPDataBase(self.server) requests.post(self.app.db.marmotta.sparql.update, data=reset_marmotta, headers={"Content-Type":"application/sparql-update; charset=utf-8"}) #self.dir = TemporaryDirectory(dir='test/data') # self.dir = mkdtemp(dir='test/data') #self.app.db = FilesystemDB(self.dir.name) self.mock = RandomGenerator()
def setUp(self): self.server = os.environ.get('COLLECTIONS_API_TEST_DB') if not self.server: raise EnvironmentError self.db = LDPDataBase(self.server) res = requests.post(self.db.marmotta.sparql.update, data=reset_marmotta, headers={ "Content-Type": "application/sparql-update; charset=utf-8" }) self.mock = RandomGenerator()
class JSONTest(TestCase): def setUp(self): self.mock = RandomGenerator() def test_decode(self): decoder = RDAJSONDecoder() json = self.mock.json_collection() obj = decoder.decode(json) self.assertIsInstance(obj, Model) self.assertIsInstance(obj.capabilities, Model) self.assertIsInstance(obj.properties, Model) def test_encode(self): encoder = RDAJSONEncoder() obj = self.mock.collection() dct = encoder.default(obj) self.assertIsInstance(dct, dict) self.assertIsInstance(dct.get('capabilities'), dict) self.assertIsInstance(dct.get('properties'), dict) self.assertDictEqual(dct, obj.dict())
class SPARQLTest(TestCase): def setUp(self): self.mock = RandomGenerator() self.sparql = SPARQLTools( Marmotta(os.environ.get('COLLECTIONS_API_TEST_DB')).sparql) def test_ldp_result_to_dataset(self): result = self.mock.collection_result() b = result.bindings ds = self.sparql.result_to_dataset(result) g = Variable('g') s = Variable('s') p = Variable('p') o = Variable('o') self.assertEqual(len(b), len(ds)) for d in result.bindings: self.assertIn((d[s], d[p], d[o], d[g]), ds)
def setUp(self): self.mock = RandomGenerator() self.sparql = SPARQLTools( Marmotta(os.environ.get('COLLECTIONS_API_TEST_DB')).sparql)
class CollectionTest(TestCase): @classmethod def setUpClass(cls): print("\n>> Test Collections Routes >>>>>>>>>>>>>") def setUp(self): self.app = app self.server = os.environ.get('COLLECTIONS_API_TEST_DB') if not self.server: raise EnvironmentError self.app.db = LDPDataBase(self.server) requests.post(self.app.db.marmotta.sparql.update, data=reset_marmotta, headers={"Content-Type":"application/sparql-update; charset=utf-8"}) #self.dir = TemporaryDirectory(dir='test/data') # self.dir = mkdtemp(dir='test/data') #self.app.db = FilesystemDB(self.dir.name) self.mock = RandomGenerator() def tearDown(self): #self.dir.cleanup() requests.post(self.app.db.marmotta.sparql.update, data=reset_marmotta, headers={"Content-Type":"application/sparql-update; charset=utf-8"}) def to_dict(self, c_obj): if isinstance(c_obj, CollectionObject): c_dict = c_obj.__dict__ c_dict['capabilities'] = c_dict['capabilities'].__dict__ c_dict['properties'] = c_dict['properties'].__dict__ return c_dict def get(self, path): with self.app.test_client() as client: return client.get(path, follow_redirects=True) def test_collection_get(self): with self.app.app_context(): c_objs = [self.mock.collection(description={'something':'abcdefghi123ö'}) for i in range(5)] for c in c_objs: self.app.db.set_collection(c) response = self.get("/collections") loaded = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertListEqual([json.dumps(c) for c in sorted(loaded['contents'], key=lambda x: x.id)], [json.dumps(c) for c in sorted(c_objs, key=lambda x: x.id)]) def test_collection_get_id(self): with self.app.app_context(): c_objs = [self.mock.collection(description={'something':'abcdefghi123ö'}) for i in range(5)] for c in c_objs: self.app.db.set_collection(c) response = [self.get("/collections/"+urllib.parse.quote_plus(c.id)) for c in c_objs] loaded = [json.loads(r.data) for r in response] for r in response: self.assertEqual(r.status_code, 200) self.assertListEqual([json.dumps(c) for c in sorted(loaded, key=lambda x: x.id)], [json.dumps(c) for c in sorted(c_objs, key=lambda x: x.id)]) def test_collection_get_unknown_id(self): response = self.get("/collections/ubifdnoi3hrfiu") self.assertEqual(response.status_code, 404) def test_collection_post(self): with self.app.app_context(): c_dicts = [self.mock.collection(description={'something':'abcdefghi123ö'}).dict() for i in range(5)] with self.app.test_client() as c: results = [{'out': c.post("/collections", data=json.dumps([col]), content_type='application/json', follow_redirects=True), 'in': col} for col in c_dicts] for r in results: self.assertEqual(r['out'].status_code, 201) r_dict = json.loads(r['out'].data).pop().dict() self.assertEqual(json.dumps(r_dict), json.dumps(r['in'])) def test_collection_delete_id(self): with self.app.app_context(): c_objs = [self.mock.collection(description={'something':'abcdefghi123ö'}) for i in range(5)] for c in c_objs: self.app.db.set_collection(c) with self.app.test_client() as c: results = [c.delete("/collections/"+urllib.parse.quote_plus(col.id), follow_redirects=True) for col in c_objs] for r in results: self.assertEqual(r.status_code, 200) for c in c_objs: with self.assertRaises(NotFoundError): self.app.db.get_collection(c.id) def test_collection_delete_unknown_id(self): with self.app.test_client() as c: response = c.delete("/collections/tgvfrde", follow_redirects=True) self.assertEqual(response.status_code, 404) def test_collection_put_id(self): with self.app.app_context(): c_objs = [self.mock.collection(description={'something':'abcdefghi123ö'}) for i in range(5)] for c in c_objs: self.app.db.set_collection(c) c.description.update({"added_description":"changed"}) with self.app.test_client() as c: for col in c_objs: response = c.put("/collections/"+urllib.parse.quote_plus(col.id), data=json.dumps(col), content_type="application/json", follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertEqual(json.dumps(json.loads(response.data)), json.dumps([col])) response = self.get("/collections/"+urllib.parse.quote_plus(col.id)) self.assertEqual(response.status_code, 200) self.assertEqual(json.dumps(json.loads(response.data)), json.dumps(col)) def test_collection_put_unknown_id(self): with self.app.app_context(): c_obj = self.mock.collection(description={'something':'abcdefghi123ö'}) with self.app.test_client() as c: response = c.put("/collections/"+urllib.parse.quote_plus(c_obj.id), data=json.dumps(c_obj), content_type="application/json", follow_redirects=True) self.assertEqual(response.status_code, 404)
def setUp(self): self.dir = TemporaryDirectory( dir=os.environ.get('COLLECTIONS_API_TEST_DIR', 'test/data')) #self.dir = mkdtemp(dir='./test/data') self.db = FilesystemDB(self.dir.name) self.mock = RandomGenerator()
class DbTests(TestCase): @classmethod def setUpClass(cls): print("\n// Test Filesystem DB ------------------") def setUp(self): self.dir = TemporaryDirectory( dir=os.environ.get('COLLECTIONS_API_TEST_DIR', 'test/data')) #self.dir = mkdtemp(dir='./test/data') self.db = FilesystemDB(self.dir.name) self.mock = RandomGenerator() def tearDown(self): # a = 'b' self.dir.cleanup() def test_db_create_collection_on_filesystem(self): with app.app_context(): c_obj = self.mock.collection() self.db.set_collection(c_obj) self.assertTrue( os.path.isfile( os.path.join(self.db.d_data, c_obj.id.replace('/', '∕'), self.db.d_collection))) def test_db_access_created_collection(self): with app.app_context(): c_obj = self.mock.collection() self.db.set_collection(c_obj) self.assertEqual( json.dumps(c_obj), json.dumps(self.db.get_collection(c_obj.id).pop())) def test_db_access_multiple_collections(self): with app.app_context(): c_objs = [self.mock.collection() for _ in range(randint(2, 5))] for c in c_objs: self.db.set_collection(c) self.assertSetEqual( set([json.dumps(c) for c in c_objs]), set([json.dumps(c) for c in self.db.get_collection()])) def test_db_delete_created_collection(self): with app.app_context(): c_obj = self.mock.collection() self.db.set_collection(c_obj) self.db.del_collection(c_obj.id) self.assertFalse( os.path.isfile( os.path.join(self.db.d_data, c_obj.id, self.db.d_collection))) def test_db_overwrite_collection(self): with app.app_context(): c_obj = self.mock.collection() d_obj = self.mock.collection() d_obj.id = c_obj.id d_obj.capabilities.isOrdered = not c_obj.capabilities.isOrdered self.db.set_collection(c_obj) self.assertNotEqual( self.db.get_collection(c_obj.id)[0].capabilities.isOrdered, d_obj.capabilities.isOrdered) self.db.set_collection(d_obj) self.assertEqual( self.db.get_collection(c_obj.id)[0].capabilities.isOrdered, d_obj.capabilities.isOrdered) def test_db_create_member_on_filesystem(self): with app.app_context(): c_obj = self.mock.collection() m_obj = self.mock.member() self.db.set_collection(c_obj) self.db.set_member(c_obj.id, m_obj) self.assertTrue( os.path.isfile( os.path.join(self.db.d_data, c_obj.id.replace('/', '∕'), m_obj.id.replace('/', '∕') + '.json'))) def test_db_access_created_member(self): with app.app_context(): c_obj = self.mock.collection() m_obj = self.mock.member() self.db.set_collection(c_obj) self.db.set_member(c_obj.id, m_obj) self.assertEqual( json.dumps(m_obj), json.dumps(self.db.get_member(c_obj.id, m_obj.id).pop())) def test_db_access_multiple_members(self): with app.app_context(): c_obj = self.mock.collection() self.db.set_collection(c_obj) m_objs = [self.mock.member() for _ in range(randint(2, 5))] for m_obj in m_objs: self.db.set_member(c_obj.id, m_obj) self.assertSetEqual( set([json.dumps(m) for m in m_objs]), set([json.dumps(m) for m in self.db.get_member(c_obj.id)])) def test_db_delete_created_member(self): with app.app_context(): c_obj = self.mock.collection() m_obj = self.mock.member() self.db.set_collection(c_obj) self.db.set_member(c_obj.id, m_obj) self.assertTrue(len(self.db.get_member(c_obj.id, m_obj.id)) == 1) self.db.del_member(c_obj.id, m_obj.id) with self.assertRaises(NotFoundError) as context: self.db.get_member(c_obj.id, m_obj.id) def test_db_overwrite_member(self): with app.app_context(): c_obj = self.mock.collection() m_obj = self.mock.member() n_obj = self.mock.member() n_obj.id = m_obj.id n_obj.location = m_obj.location + "x" self.db.set_collection(c_obj) self.db.set_member(c_obj.id, m_obj) self.db.set_member(c_obj.id, n_obj) self.assertEqual( self.db.get_member(c_obj.id, m_obj.id)[0].location, n_obj.location) def test_db_create_service_on_filesystem(self): with app.app_context(): s_obj = self.mock.service() self.db.set_service(s_obj) self.assertTrue( os.path.isfile(os.path.join(self.db.d_data, self.db.d_service))) def test_db_access_created_service(self): with app.app_context(): s_obj = self.mock.service() self.db.set_service(s_obj) t_obj = self.db.get_service() self.assertEqual(json.dumps(s_obj), json.dumps(t_obj)) def test_db_overwrite_service(self): with app.app_context(): s_obj = self.mock.service() t_obj = self.mock.service() t_obj.providesCollectionPids = not s_obj.providesCollectionPids self.db.set_service(s_obj) self.db.set_service(t_obj) self.assertNotEqual(json.dumps(s_obj), json.dumps(self.db.get_service()))
from src.utils.data.ldp_db import * from test.mock import RandomGenerator mocks = RandomGenerator() c_obj = mocks.collection() ldp = LDPDataBase("http://localhost:32768/marmotta")
class DbTests(TestCase): @classmethod def setUpClass(cls): print("\n// Test LDP DB ------------------") def setUp(self): self.server = os.environ.get('COLLECTIONS_API_TEST_DB') if not self.server: raise EnvironmentError self.db = LDPDataBase(self.server) res = requests.post(self.db.marmotta.sparql.update, data=reset_marmotta, headers={ "Content-Type": "application/sparql-update; charset=utf-8" }) self.mock = RandomGenerator() #def test_ldp_b64encode(self): # self.assertEqual(self.db.b64encode(self.server),"aHR0cDovL2xvY2FsaG9zdDozMjc2OC9tYXJtb3R0YQ==") def tearDown(self): requests.post(self.db.marmotta.sparql.update, data=reset_marmotta, headers={ "Content-Type": "application/sparql-update; charset=utf-8" }) def test_ldp_create_collection(self): with app.app_context(): c_obj = self.mock.collection() id = self.db.marmotta.ldp(encoder.encode(c_obj.id)) self.db.set_collection(c_obj) response = self.db.sparql.select( id) # todo: figure out if using db.sparql or sparql #print(response.json()) r_obj = self.db.RDA.graph_to_object( response.toDataset().graph(id)).pop() self.assertDictEqual(c_obj.dict(), r_obj.dict()) def test_ldp_access_created_collection(self): with app.app_context(): # todo: post collection to sparql, retrieve via LDP and compare c_obj = self.mock.collection() self.db.set_collection(c_obj) r_obj = self.db.get_collection(c_obj.id).pop() self.assertDictEqual(c_obj.dict(), r_obj.dict()) def test_ldp_access_multiple_collections(self): with app.app_context(): # todo: post collections to sparql, retrieve via LDP and compare requests.post(self.db.marmotta.sparql.update, data=reset_marmotta) c_objs = [self.mock.collection() for _ in range(randint(2, 5))] self.db.set_collection(c_objs) set1 = set([json.dumps(c) for c in c_objs]) set2 = set([json.dumps(c) for c in self.db.get_collection()]) self.assertSetEqual(set1, set2) def test_ldp_access_with_ldp(self): with app.app_context(): # todo: post collection to sparql, retrieve via LDP and compare c_obj = self.mock.collection() self.db.set_collection(c_obj) g = Dataset().parse(self.db.marmotta.ldp(encoder.encode(c_obj.id)), format="n3") r_obj = self.db.RDA.graph_to_object(g).pop() self.assertDictEqual(c_obj.dict(), r_obj.dict()) def test_ldp_delete_created_collection(self): with app.app_context(): c_obj = self.mock.collection() self.db.set_collection(c_obj) self.db.del_collection(c_obj.id) with self.assertRaises(NotFoundError) as context: self.db.get_collection(c_obj.id) def test_ldp_overwrite_collection(self): with app.app_context(): c_obj = self.mock.collection() d_obj = self.mock.collection() d_obj.id = c_obj.id d_obj.capabilities.isOrdered = not c_obj.capabilities.isOrdered self.db.set_collection(c_obj) self.assertNotEqual( self.db.get_collection(c_obj.id).pop().capabilities.isOrdered, d_obj.capabilities.isOrdered) self.db.upd_collection(d_obj) self.assertEqual( self.db.get_collection(c_obj.id).pop().capabilities.isOrdered, d_obj.capabilities.isOrdered) def test_db_create_member(self): with app.app_context(): c_obj = self.mock.collection() m_obj = self.mock.member() id = self.db.marmotta.ldp( encoder.encode(c_obj.id) + "/member/" + encoder.encode(m_obj.id)) self.db.set_collection(c_obj) self.db.set_member(c_obj.id, m_obj) response = self.db.sparql.select(id) r_obj = self.db.RDA.graph_to_object( response.toDataset().graph(id)).pop() self.assertDictEqual(m_obj.dict(), r_obj.dict()) def test_db_access_created_member(self): with app.app_context(): c_obj = self.mock.collection() m_obj = self.mock.member() self.db.set_collection(c_obj) self.db.set_member(c_obj.id, m_obj) self.assertEqual( json.dumps(m_obj), json.dumps(self.db.get_member(c_obj.id, m_obj.id).pop())) def test_db_access_multiple_members(self): with app.app_context(): c_obj = self.mock.collection() self.db.set_collection(c_obj) m_objs = [self.mock.member() for _ in range(randint(2, 5))] for m_obj in m_objs: self.db.set_member(c_obj.id, m_obj) self.assertSetEqual( set([json.dumps(m) for m in m_objs]), set([json.dumps(m) for m in self.db.get_member(c_obj.id)])) def test_db_delete_created_member(self): with app.app_context(): c_obj = self.mock.collection() m_obj = self.mock.member() self.db.set_collection(c_obj) self.db.set_member(c_obj.id, m_obj) self.assertTrue(len(self.db.get_member(c_obj.id, m_obj.id)) == 1) self.db.del_member(c_obj.id, m_obj.id) with self.assertRaises(NotFoundError) as context: self.db.get_member(c_obj.id, m_obj.id) def test_db_overwrite_member(self): with app.app_context(): c_obj = self.mock.collection() m_obj = self.mock.member() n_obj = self.mock.member() n_obj.id = m_obj.id n_obj.location = m_obj.location + "x" self.db.set_collection(c_obj) self.db.set_member(c_obj.id, m_obj) self.db.upd_member(c_obj.id, n_obj) self.assertEqual( self.db.get_member(c_obj.id, m_obj.id)[0].location, n_obj.location) def test_db_create_service(self): with app.app_context(): s_obj = self.mock.service() self.db.set_service(s_obj) self.assertDictEqual(self.db.get_service().dict(), s_obj.dict())
def setUp(self): self.mock = RandomGenerator()
def setUp(self): #self.maxDiff = None self.mock = RandomGenerator() self.marmotta = Marmotta("http://localhost:8080/marmotta") self.rda = MappingTool(RDA, RDATools) self.maxDiff = None
class RDATestCase(unittest.TestCase): def setUp(self): #self.maxDiff = None self.mock = RandomGenerator() self.marmotta = Marmotta("http://localhost:8080/marmotta") self.rda = MappingTool(RDA, RDATools) self.maxDiff = None def test_rda_graph_to_dict(self): obj = self.mock.collection() g = self.mock.graph_collection(self.marmotta.ldp(""), obj) g.remove((None, RDF.type, self.rda.ns.Collection)) dct = self.rda.graph_to_dict(g, g.identifier, self.rda.dictionary.get(type(obj)).map) self.assertDictEqual(obj.dict(), dct) def test_rda_dict_to_graph(self): obj = self.mock.collection() dct = obj.dict() g1 = self.mock.graph_collection(self.marmotta.ldp(""), obj) g1.remove((None, RDF.type, self.rda.ns.Collection)) g2 = self.rda.dict_to_graph(dct, subject=g1.identifier, map=self.rda.dictionary.get( type(obj)).inverted) self.assertEqual(len(g1), len(g2)) self.assertEqual(len(g1 - g2), 0) self.assertEqual(len(g2 - g1), 0) def test_rda_collection_to_graph(self): obj = self.mock.collection() g1 = self.mock.graph_collection(self.marmotta.ldp(""), obj) g2 = self.rda.object_to_graph(g1.identifier, obj) self.assertEqual(len(g1), len(g2)) self.assertEqual(len(g1 - g2), 0) self.assertEqual(len(g2 - g1), 0) def test_rda_graph_to_collection(self): obj1 = self.mock.collection() g = self.mock.graph_collection(self.marmotta.ldp(""), obj1) obj2 = self.rda.graph_to_object(g).pop() self.assertDictEqual(obj1.dict(), obj2.dict()) def test_rda_member_to_graph(self): cid = self.mock.collection().id obj = self.mock.member() g1 = self.mock.graph_member(self.marmotta.ldp(""), cid, obj) g2 = self.rda.object_to_graph(g1.identifier, obj) self.assertEqual(len(g1), len(g2)) self.assertEqual(len(g1 - g2), 0) self.assertEqual(len(g2 - g1), 0) def test_rda_graph_to_member(self): cid = self.mock.collection().id obj1 = self.mock.member() g = self.mock.graph_member(self.marmotta.ldp(""), cid, obj1) obj2 = self.rda.graph_to_object(g).pop() self.assertDictEqual(obj1.dict(), obj2.dict()) def test_rda_service_to_graph(self): obj = self.mock.service() g1 = self.mock.graph_service(self.marmotta.ldp(""), obj) g2 = self.rda.object_to_graph(g1.identifier, obj) self.assertEqual(len(g1), len(g2)) self.assertEqual(len(g1 - g2), 0) self.assertEqual(len(g2 - g1), 0) def test_rda_graph_to_service(self): obj1 = self.mock.service() g = self.mock.graph_service(self.marmotta.ldp(""), obj1) obj2 = self.rda.graph_to_object(g).pop() self.assertDictEqual(obj1.dict(), obj2.dict())
class ModelTests(TestCase): @classmethod def setUpClass(cls): print("\n// Test Models -------------------------") def setUp(self): self.mock = RandomGenerator() def test_models_create_collection_with_kwargs(self): c_obj = CollectionObject("", self.mock.collection().capabilities, self.mock.collection().properties, "") self.assertEqual(type(c_obj), CollectionObject) def test_models_create_collection_with_dict(self): c_obj = CollectionObject(**{ 'id': "", 'capabilities': self.mock.collection().capabilities, 'properties': self.mock.collection().properties, 'description': "" }) self.assertEqual(type(c_obj), CollectionObject) def test_models_create_collection_with_json(self): with app.app_context(): c_obj = json.loads(self.mock.json_collection()) self.assertEqual(type(c_obj), CollectionObject) self.assertEqual(type(c_obj.capabilities), CollectionCapabilities) self.assertEqual(type(c_obj.properties), CollectionProperties) # def test_models_deserialize_collection_to_json(self): # with app.app_context(): # c_str = self.mock.json_collection() # c_obj = json.loads(c_str) # self.assertListEqual(sorted(re.sub("\s+","",c_str)), sorted(re.sub("\s+","",json.dumps(c_obj)))) def test_models_collection_fails_with_superfluous_args(self): with self.assertRaises(TypeError) as context: c_obj = CollectionObject(**{ 'id': "", 'capabilities': self.mock.collection().capabilities, 'properties': self.mock.collection().properties, 'description': "", 'superfluousArg': "" }) def test_models_create_member_with_kwargs(self): c_obj = CollectionObject("", self.mock.collection().capabilities, self.mock.collection().properties, "") self.assertEqual(type(c_obj), CollectionObject) def test_models_create_member_with_dict(self): c_obj = CollectionObject(**{ 'id': "", 'capabilities': self.mock.collection().capabilities, 'properties': self.mock.collection().properties, 'description': "" }) self.assertEqual(type(c_obj), CollectionObject) def test_models_create_member_with_json(self): with app.app_context(): m_obj = json.loads(self.mock.json_member()) self.assertEqual(type(m_obj), MemberItem) # def test_models_deserialize_member_to_json(self): # with app.app_context(): # m_str = self.mock.json_member() # m_obj = json.loads(m_str) # self.assertEqual(m_str, json.dumps(m_obj)) def test_models_member_fails_with_superfluous_args(self): with self.assertRaises(TypeError) as context: MemberItem(**{ 'id': 'http://example.org/12/mem/23', 'location': 'http://example.org/12/mem/23', 'superfluousArg': '' }) def test_models_create_service_with_kwargs(self): c_obj = CollectionObject("", self.mock.collection().capabilities, self.mock.collection().properties, "") self.assertEqual(type(c_obj), CollectionObject) def test_models_create_service_with_dict(self): c_obj = CollectionObject(**{ 'id': "", 'capabilities': self.mock.collection().capabilities, 'properties': self.mock.collection().properties, 'description': "" }) self.assertEqual(type(c_obj), CollectionObject) def test_models_create_service_with_json(self): with app.app_context(): s_str = self.mock.json_service() s_obj = json.loads(s_str) self.assertEqual(type(s_obj), Service) # def test_models_deserialize_service_to_json(self): # with app.app_context(): # s_str = self.mock.json_service() # s_obj = json.loads(s_str) # self.assertEqual(s_str, json.dumps(s_obj)) def test_models_service_fails_with_superfluous_args(self): with self.assertRaises(TypeError) as context: Service(**{ "providesCollectionPids": True, "collectionPidProviderType": True, "enforcesAccess": True, "supportsPagination": True, "asynchronousActions": True, "ruleBasedGeneration": True, "maxExpansionDepth": True, "providesVersioning": True, "supportedCollectionsOperations": ["findMatch","flatten"], "supportedModelTypes": [], "superfluousArg": "" })