Пример #1
0
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())
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()))
Пример #3
0
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())