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_iterate_dogs(self): me = Person(name="paul") self.db.put(me) for i in range(500): dog = Dog(name="dog_%s" % i, owner=me) self.db.put(dog) counter = 0 for dog in me.dogs: counter += 1 self.assertEqual(counter, 500)
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)
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)
class IndexTests(unittest.TestCase): db = None def setUp(self): filepath = os.path.join(THIS_DIR, "animal_views.js") mapper = ClassMapper([Dog, Cat, Person, JackRussell], designs=[filepath]) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True) self.db.update_designs() def tearDown(self): self.db.session.close() def test_create_index(self): filepath = os.path.join(THIS_DIR, "animal_views.js") as_dict = self.db.mapper._js_design_as_doc(filepath) expected = { "_id": "_design/animal_views", "views": { "cat_legs": { "map": "function(doc) {\n if (doc.type == \"cat\") {\n emit(doc.legs, doc);\n }\n}" } } } self.assertEqual(as_dict, expected) def test_query_view(self): paul = Person(name="Paul") self.db.put(paul) cat1 = Cat(owner=paul, legs=4) self.db.put(cat1) cat2 = Cat(owner=paul, legs=3) self.db.put(cat2) three_legged_cats = Cat.all_with_n_legs(self.db, 3) self.assertEqual(len(three_legged_cats), 1) self.assertEqual(three_legged_cats[0].key, cat2.key) def test_long_polling(self): paul = Person(name="Paul") self.db.put(paul) cat1 = Cat(owner=paul, legs=4) self.db.put(cat1) cat2 = Cat(owner=paul, legs=3) self.db.put(cat2) three_legged_cats = self.db.view("animal_views/cat_legs", key=3) self.assertEqual(len(three_legged_cats), 1) self.assertEqual(three_legged_cats[0].key, cat2.key)
class CacheTests(unittest.TestCase): def setUp(self): self.mapper = ClassMapper([Dog, Cat, Person]) self.db = CouchDBWrapper(self.mapper, COUCHDB_URL, COUCHDB_NAME, reset=True) self.db.update_designs() def tearDown(self): self.db.session.close() def test_make_views(self): views = FamWriteBufferViews(self.mapper) paul = Person(name="paul") dog = Dog(name="woofer", owner=paul) views.index_obj(dog) self.assertTrue( views.indexes.get("glowinthedark_co_uk_test_person_dogs") != None) self.assertTrue(views.indexes["glowinthedark_co_uk_test_person_dogs"][ paul.key][dog.key] == dog) def test_query_views(self): views = FamWriteBufferViews(self.mapper) paul = Person(name="paul") dog = Dog(name="woofer", owner=paul) views.index_obj(dog) obj = views.query_view("glowinthedark.co.uk/test/person_dogs", key=paul.key) self.assertEqual(obj, [dog]) def test_views_keys(self): views = FamWriteBufferViews(self.mapper) paul = Person(name="paul") dog = Dog(name="woofer", owner=paul) views.index_obj(dog) print("keys: ", views.indexes.keys()) self.assertEqual( set(views.indexes.keys()), { 'glowinthedark_co_uk_test_person_animals', 'glowinthedark_co_uk_test_person_dogs', 'raw_all', 'glowinthedark_co_uk_test_dog_kennel_club_membership' })
class IndexTests(unittest.TestCase): db = None def setUp(self): filepath = os.path.join(THIS_DIR, "animal_views.js") mapper = ClassMapper([Dog, Cat, Person, JackRussell], designs=[filepath]) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True) self.db.update_designs() def tearDown(self): self.db.session.close() def test_uniqueness(self): paul = Person(name="paul") self.db.put(paul) dog1 = Dog.create(self.db, name="rufus", owner_id=paul.key, kennel_club_membership="123456") dog2 = Dog.create(self.db, name="fly", owner_id=paul.key) # raises if setting one value self.assertRaises(FamUniqueError, dog2.update, {"kennel_club_membership": "123456"}) self.assertIsNone(dog2.kennel_club_membership) # raises if creating a new one self.assertRaises(FamUniqueError, Dog.create, self.db, name="steve", owner_id=paul.key, kennel_club_membership="123456") def test_uniqueness_delete(self): paul = Person(name="paul") self.db.put(paul) dog1 = Dog.create(self.db, name="rufus", owner_id=paul.key, kennel_club_membership="123456") dog1.delete(self.db) dog2 = Dog.create(self.db, name="another", owner_id=paul.key, kennel_club_membership="123456") def test_get_unique(self): paul = Person(name="paul") self.db.put(paul) dog1 = Dog.create(self.db, name="rufus", owner_id=paul.key, kennel_club_membership="123456") dog2 = Dog.get_unique_instance(self.db, "kennel_club_membership", "123456") self.assertIsNotNone(dog2) self.assertTrue(dog2.kennel_club_membership == "123456")
class CacheTests(unittest.TestCase): def setUp(self): self.mapper = ClassMapper([Dog, Cat, Person]) self.db = CouchDBWrapper(self.mapper, COUCHDB_URL, COUCHDB_NAME, reset=True) self.db.update_designs() def tearDown(self): self.db.session.close() def test_make_views(self): views = FamWriteBufferViews(self.mapper) paul = Person(name="paul") dog = Dog(name="woofer", owner=paul) views.index_obj(dog) self.assertTrue(views.indexes.get("glowinthedark_co_uk_test_person_dogs") != None) self.assertTrue(views.indexes["glowinthedark_co_uk_test_person_dogs"][paul.key][dog.key] == dog) def test_query_views(self): views = FamWriteBufferViews(self.mapper) paul = Person(name="paul") dog = Dog(name="woofer", owner=paul) views.index_obj(dog) obj = views.query_view("glowinthedark.co.uk/test/person_dogs", key=paul.key) self.assertEqual(obj, [dog]) def test_views_keys(self): views = FamWriteBufferViews(self.mapper) paul = Person(name="paul") dog = Dog(name="woofer", owner=paul) views.index_obj(dog) print("keys: ", views.indexes.keys()) self.assertEqual(set(views.indexes.keys()), {'glowinthedark_co_uk_test_person_animals', 'glowinthedark_co_uk_test_person_dogs', 'raw_all', 'glowinthedark_co_uk_test_dog_kennel_club_membership'})
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)
class TestDatabase(unittest.TestCase): def setUp(self): mapper = ClassMapper([Fish]) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True) self.db.update_designs() def test_fish(self): loc = LatLong(51.2345, -1.4533) birthday = datetime.datetime(1964, 12, 5, tzinfo=pytz.utc) image_path = os.path.join(DATA_DIR, "goldfish.jpg") with open(image_path, "rb") as f: image_data = f.read() if sys.version_info < (3, 0): image_data = bytearray(image_data) fish = Fish.create(self.db, name="Nemo", location=loc, born=birthday, length=Decimal("45.7"), edible_fraction=Fraction(1, 3), image=image_data ) fish2 = Fish.get(self.db, fish.key) self.assertTrue(type(fish2.location) == LatLong) self.assertEqual(fish2.location.latitude, 51.2345) self.assertTrue(isinstance(fish2.born, datetime.datetime)) self.assertEqual(fish2.born, birthday) self.assertTrue(isinstance(fish2.length, Decimal)) self.assertEqual(fish2.length, Decimal("45.7")) self.assertTrue(isinstance(fish2.edible_fraction, Fraction)) self.assertEqual(fish2.edible_fraction, Fraction(1, 3)) self.assertTrue(isinstance(fish2.image, bytes)) self.assertEqual(fish2.image, image_data) ## test for view query fishes = list(Fish.all(self.db)) fish3 = fishes[0] self.assertTrue(type(fish3.location) == LatLong) self.assertEqual(fish3.location.latitude, 51.2345) self.assertTrue(isinstance(fish3.born, datetime.datetime)) self.assertEqual(fish3.born, birthday) self.assertTrue(isinstance(fish3.length, Decimal)) self.assertEqual(fish3.length, Decimal("45.7")) self.assertTrue(isinstance(fish3.edible_fraction, Fraction)) self.assertEqual(fish3.edible_fraction, Fraction(1, 3)) self.assertTrue(isinstance(fish3.image, bytes)) self.assertEqual(fish3.image, image_data)
class MapperValidationTests(unittest.TestCase): def test_make_a_validator(self): mapper = ClassMapper([Dog, Cat, Person, JackRussell]) validator = ModelValidator(classes=[Dog, Cat, Person, JackRussell]) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator) self.db.update_designs() paul = Person(name="paul") paul.save(self.db) cat = Cat(name="whiskers", owner_id=paul.key, legs=4) cat.save(self.db) self.assertEqual(cat.owner, paul) self.assertEqual(cat.owner.name, "paul") cat = Cat(name="puss", owner_id=paul.key) self.assertRaises(FamValidationError, cat.save, self.db) self.db.session.close() def test_make_a_validator_from_modules(self): mapper = ClassMapper([], modules=[test01]) validator = ModelValidator(classes=[Dog, Cat, Person, JackRussell]) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator) self.db.update_designs() paul = Person(name="paul") paul.save(self.db) cat = Cat(name="whiskers", owner_id=paul.key, legs=4) cat.save(self.db) self.assertEqual(cat.owner, paul) self.assertEqual(cat.owner.name, "paul") #missing legs cat = Cat(name="puss", owner_id=paul.key) self.assertRaises(FamValidationError, cat.save, self.db) #additional properties def failing_cat(): cat = Cat(name="puss", owner_id=paul.key, legs=2, collar="green") self.assertRaises(FamValidationError, failing_cat) dog = Dog(name="fly") self.db.put(dog) dog.tail = "long" self.db.put(dog) self.db.session.close() # print dog.as_json() def test_included_refs_from_in_validator(self): mapper = ClassMapper([], modules=[test01]) validator = ModelValidator(modules=[test01]) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator) self.db.update_designs() paul = Person(name="paul") paul.save(self.db) paul_id = paul.key cat = Cat(name="whiskers", owner_id=paul.key, legs=4) cat.save(self.db) self.assertEqual(cat.owner, paul) self.assertEqual(cat.owner.name, "paul") paul = Person.get(self.db, paul_id) paul.save(self.db) self.db.session.close() def test_string_format(self): mapper = ClassMapper([], modules=[test01]) validator = ModelValidator(modules=[test01]) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator) self.db.update_designs() paul = Person(name="paul") paul.save(self.db) cat = Cat(name="whiskers", owner_id=paul.key, legs=4) cat.save(self.db) self.assertEqual(cat.owner, paul) self.assertEqual(cat.owner.name, "paul") cat = Cat(name="puss", owner_id=paul.key, legs=3, email="*****@*****.**") cat.save(self.db) cat.email = "paulglowinthedark.co.uk" self.assertRaises(FamValidationError, self.db.put, cat) self.db.session.close()
class MapperValidationTests(unittest.TestCase): def test_make_a_validator(self): mapper = ClassMapper([Dog, Cat, Person, JackRussell]) validator = ModelValidator(None, classes=[Dog, Cat, Person, JackRussell]) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator) self.db.update_designs() paul = Person(name="paul") paul.save(self.db) cat = Cat(name="whiskers", owner_id=paul.key, legs=4) cat.save(self.db) self.assertEqual(cat.owner, paul) self.assertEqual(cat.owner.name, "paul") cat = Cat(name="puss", owner_id=paul.key) self.assertRaises(FamValidationError, cat.save, self.db) self.db.session.close() def test_make_a_validator_from_modules(self): mapper = ClassMapper([], modules=[test01]) validator = ModelValidator(mapper) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator) self.db.update_designs() paul = Person(name="paul") paul.save(self.db) cat = Cat(name="whiskers", owner_id=paul.key, legs=4) cat.save(self.db) self.assertEqual(cat.owner, paul) self.assertEqual(cat.owner.name, "paul") #missing legs cat = Cat(name="puss", owner_id=paul.key) self.assertRaises(FamValidationError, cat.save, self.db) #additional properties def failing_cat(): cat = Cat(name="puss", owner_id=paul.key, legs=2, collar="green") self.assertRaises(FamValidationError, failing_cat) dog = Dog(name="fly") self.db.put(dog) dog.tail = "long" self.db.put(dog) self.db.session.close() # print dog.as_json() def test_included_refs_from_in_validator(self): mapper = ClassMapper([], modules=[test01]) validator = ModelValidator(mapper) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator) self.db.update_designs() paul = Person(name="paul") paul.save(self.db) paul_id = paul.key cat = Cat(name="whiskers", owner_id=paul.key, legs=4) cat.save(self.db) self.assertEqual(cat.owner, paul) self.assertEqual(cat.owner.name, "paul") paul = Person.get(self.db, paul_id) paul.save(self.db) self.db.session.close() def test_string_format(self): mapper = ClassMapper([], modules=[test01]) validator = ModelValidator(mapper) self.db = CouchDBWrapper(mapper, COUCHDB_URL, COUCHDB_NAME, reset=True, validator=validator) self.db.update_designs() paul = Person(name="paul") paul.save(self.db) cat = Cat(name="whiskers", owner_id=paul.key, legs=4) cat.save(self.db) self.assertEqual(cat.owner, paul) self.assertEqual(cat.owner.name, "paul") cat = Cat(name="puss", owner_id=paul.key, legs=3, email="*****@*****.**") cat.save(self.db) cat.email = "paulglowinthedark.co.uk" self.assertRaises(FamValidationError, self.db.put, cat) self.db.session.close()
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)
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])