Пример #1
0
class CacheTests(unittest.TestCase):

    def setUp(self):
        mapper = ClassMapper([Dog, Cat, Person])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True)
        self.db.update_designs()

    def tearDown(self):
        self.db.session.close()

    def test_delete_cat_refs(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        key = cat.key
        cat2 = Cat(name="puss", owner_id=paul.key, legs=2)
        cat2.save(self.db)
        revivedcat1 = self.db.get(key)

        self.assertTrue(revivedcat1 is not None)

        paul.delete(self.db)
        revivedcat2 = self.db.get(key)
        print("revivedcat2:" , revivedcat2)
        self.assertTrue(revivedcat2 is None, "revivedcat2: %s" % revivedcat2)
Пример #2
0
class CacheTests(unittest.TestCase):
    def setUp(self):
        mapper = ClassMapper([Dog, Cat, Person])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True)
        self.db.update_designs()

    def tearDown(self):
        self.db.session.close()

    def test_delete_cat_refs(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        key = cat.key
        cat2 = Cat(name="puss", owner_id=paul.key, legs=2)
        cat2.save(self.db)
        revivedcat1 = self.db.get(key)

        self.assertTrue(revivedcat1 is not None)

        paul.delete(self.db)
        revivedcat2 = self.db.get(key)
        print("revivedcat2:", revivedcat2)
        self.assertTrue(revivedcat2 is None, "revivedcat2: %s" % revivedcat2)
Пример #3
0
class TestDB(unittest.TestCase):


    def setUp(self):

        mapper = ClassMapper([Dog, Cat, Person, JackRussell, Monkey])
        self.firestore = FirestoreWrapper(mapper, CREDS, namespace=NAMESPACE)
        self.couchdb = CouchDBWrapper(mapper, "http://localhost:5984", db_name="test", reset=True)
        self.couchdb.update_designs()
        self.clear_db()

    def tearDown(self):
        self.couchdb.session.close()

    def clear_db(self):
        self.firestore.delete_all("dog")
        self.firestore.delete_all("cat")
        self.firestore.delete_all("person")
        self.firestore.delete_all("jackrussell")
        self.firestore.delete_all("monkey")

    def test_app(self):
        self.assertNotEqual(self.firestore, None)


    def test_query_generator(self):

        paul = Person.create(self.firestore, name="paul")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")
        q = dogs_ref.where(u"owner_id", u"==", paul.key)

        dogs = self.firestore.query_items(q, batch_size=1)
        dogs_list = list(dogs)
        self.assertTrue(isinstance(dogs_list[0], Dog))

        self.assertEqual(len(dogs_list), 3)


    def test_sync_down(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")

        syncer = FirestoreSyncer(self.couchdb, self.firestore)
        syncer.add_query(dogs_ref.where("owner_id", "==", paul.key))

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        syncer.sync_down()

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 3)


    def test_sync_down_since(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")

        syncer = FirestoreSyncer(self.couchdb, self.firestore)
        syncer.add_query(dogs_ref.where("owner_id", "==", paul.key))

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        changed = syncer.sync_down()
        self.assertEqual(len(changed), 3)

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 3)

        dog3.update({"name":"jelly"})

        changed = syncer.sync_down()
        print(changed)
        self.assertEqual(len(changed), 1)


        updated = self.couchdb.get(dog3.key)

        self.assertEqual(updated.name, "jelly")


    def test_sync_down_since_in_db(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")

        syncer = FirestoreSyncer(self.couchdb, self.firestore, since_in_db=True)
        syncer.add_query(dogs_ref.where("owner_id", "==", paul.key))

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        changed = syncer.sync_down()
        self.assertEqual(len(changed), 3)

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 3)

        dog3.update({"name":"jelly"})

        changed = syncer.sync_down()
        print(changed)
        self.assertEqual(len(changed), 1)


        updated = self.couchdb.get(dog3.key)

        self.assertEqual(updated.name, "jelly")



    def test_sync_down_single(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)

        syncer = FirestoreSyncer(self.couchdb, self.firestore)

        dog1_ref = self.firestore.db.collection("dog").document(dog1.key)

        syncer.add_doc_ref(dog1_ref)

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        syncer.sync_down()
        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 1)



    def test_sync_up(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")

        syncer = FirestoreSyncer(self.couchdb, self.firestore)
        syncer.add_query(dogs_ref.where("owner_id", "==", paul.key))

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        syncer.sync_down()
        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 3)

        dog4 = Dog.create(self.couchdb, name="di", owner_id=paul.key)
        dog5 = Dog.create(self.couchdb, name="stevie", owner_id=paul.key)

        syncer.sync_up()
        dogs = list(paul.dogs)
        self.assertEqual(len(dogs), 5)
Пример #4
0
class TestDB(unittest.TestCase):
    def setUp(self):

        mapper = ClassMapper([Dog, Cat, Person, JackRussell, Monkey])
        self.firestore = FirestoreWrapper(mapper, CREDS, namespace=NAMESPACE)
        self.couchdb = CouchDBWrapper(mapper,
                                      "http://localhost:5984",
                                      db_name="test",
                                      reset=True)
        self.couchdb.update_designs()
        self.clear_db()

    def tearDown(self):
        self.couchdb.session.close()

    def clear_db(self):
        self.firestore.delete_all("dog")
        self.firestore.delete_all("cat")
        self.firestore.delete_all("person")
        self.firestore.delete_all("jackrussell")
        self.firestore.delete_all("monkey")

    def test_app(self):
        self.assertNotEqual(self.firestore, None)

    def test_query_generator(self):

        paul = Person.create(self.firestore, name="paul")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")
        q = dogs_ref.where(u"owner_id", u"==", paul.key)

        dogs = self.firestore.query_items(q, batch_size=1)
        dogs_list = list(dogs)
        self.assertTrue(isinstance(dogs_list[0], Dog))

        self.assertEqual(len(dogs_list), 3)

    def test_sync_down(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")

        syncer = FirestoreSyncer(self.couchdb, self.firestore)
        syncer.add_query(dogs_ref.where("owner_id", "==", paul.key))

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        syncer.sync_down()

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 3)

    def test_sync_down_since(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")

        syncer = FirestoreSyncer(self.couchdb, self.firestore)
        syncer.add_query(dogs_ref.where("owner_id", "==", paul.key))

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        changed = syncer.sync_down()
        self.assertEqual(len(changed), 3)

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 3)

        dog3.update({"name": "jelly"})

        changed = syncer.sync_down()
        print(changed)
        self.assertEqual(len(changed), 1)

        updated = self.couchdb.get(dog3.key)

        self.assertEqual(updated.name, "jelly")

    def test_sync_down_since_in_db(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")

        syncer = FirestoreSyncer(self.couchdb,
                                 self.firestore,
                                 since_in_db=True)
        syncer.add_query(dogs_ref.where("owner_id", "==", paul.key))

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        changed = syncer.sync_down()
        self.assertEqual(len(changed), 3)

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 3)

        dog3.update({"name": "jelly"})

        changed = syncer.sync_down()
        print(changed)
        self.assertEqual(len(changed), 1)

        updated = self.couchdb.get(dog3.key)

        self.assertEqual(updated.name, "jelly")

    def test_sync_down_single(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)

        syncer = FirestoreSyncer(self.couchdb, self.firestore)

        dog1_ref = self.firestore.db.collection("dog").document(dog1.key)

        syncer.add_doc_ref(dog1_ref)

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        syncer.sync_down()
        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 1)

    def test_sync_up(self):

        paul = Person.create(self.firestore, name="paul")
        sol = Person.create(self.firestore, name="sol")
        dog1 = Dog.create(self.firestore, name="woofer", owner=paul)
        dog2 = Dog.create(self.firestore, name="tiny", owner=paul)
        dog3 = Dog.create(self.firestore, name="fly", owner=paul)

        dogs_ref = self.firestore.db.collection("dog")

        syncer = FirestoreSyncer(self.couchdb, self.firestore)
        syncer.add_query(dogs_ref.where("owner_id", "==", paul.key))

        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 0)

        syncer.sync_down()
        dogs = Dog.all(self.couchdb)
        dogs_list = list(dogs)
        self.assertEqual(len(dogs_list), 3)

        dog4 = Dog.create(self.couchdb, name="di", owner_id=paul.key)
        dog5 = Dog.create(self.couchdb, name="stevie", owner_id=paul.key)

        syncer.sync_up()
        dogs = list(paul.dogs)
        self.assertEqual(len(dogs), 5)
Пример #5
0
class CacheTests(unittest.TestCase):

    def setUp(self):
        mapper = ClassMapper([Dog, Cat, Person])
        self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True)
        self.db.update_designs()

    def tearDown(self):
        pass


    def test_cache_saves(self):

        with buffered_db(self.db) as dbc:
            dog = Dog(name="woofer")
            dbc.put(dog)

        got = self.db.get(dog.key)

        self.assertTrue(got is not None)


    def test_cache_doesnt_save(self):
        # doesnt save until were done

        with buffered_db(self.db) as dbc:
            dog = Dog(name="woofer")
            dbc.put(dog)
            got = self.db.get(dog.key)
            self.assertTrue(got is None)

        got = self.db.get(dog.key)
        self.assertTrue(got is not None)

    def test_cache_gets(self):
        # doesnt save until were done

        with buffered_db(self.db) as dbc:
            dog = Dog(name="woofer")
            dbc.put(dog)
            fetched = dbc.get(dog.key)
            self.assertTrue(fetched is not None)
            self.assertEqual(id(dog), id(fetched))


    def test_cache_gets_from_db(self):

        dog = Dog(name="woofer")
        self.db.put(dog)

        with buffered_db(self.db) as dbc:
            fetched = dbc.get(dog.key)
            self.assertTrue(fetched is not None)
            self.assertNotEqual(id(dog), id(fetched))
            fetched_again = dbc.get(dog.key)

            self.assertEqual(id(fetched), id(fetched_again))

    def test_cache_gets_change_from_db(self):

        dog = Dog(name="woofer")
        self.db.put(dog)

        with buffered_db(self.db) as dbc:
            fetched = dbc.get(dog.key)
            self.assertTrue(fetched is not None)
            self.assertNotEqual(id(dog), id(fetched))
            fetched_again = dbc.get(dog.key)
            self.assertTrue(fetched_again is not None)

            self.assertEqual(id(fetched), id(fetched_again))
            dog.name = "fly"
            self.db.put(dog)

            fetched_yet_again = dbc.get(dog.key)
            self.assertTrue(fetched_yet_again is not None)

            self.assertEqual(id(fetched), id(fetched_yet_again))
            self.assertEqual(fetched_yet_again.name, 'fly')
            fetched_yet_again.name = "bluebottle"
            db_fetched = self.db.get(dog.key)
            self.assertEqual(db_fetched.name, 'fly')

        db_fetched = self.db.get(dog.key)
        self.assertEqual(db_fetched.name, 'fly')


    def test_saves_putted(self):

        dog = Dog(name="woofer")
        self.db.put(dog)

        with buffered_db(self.db) as dbc:
            fetched = dbc.get(dog.key)
            fetched.name = "bluebottle"
            dbc.put(fetched)

        db_fetched = self.db.get(dog.key)
        self.assertEqual(db_fetched.name, 'bluebottle')



    def test_refs_from(self):

        with buffered_db(self.db) as dbc:

            person = Person(name="paul")
            dbc.put(person)

            dog = Dog(name="woofer", owner=person)
            dbc.put(dog)

            self.assertEqual(person.dogs, [dog])