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 setUp(self): if os.path.exists(SCHEMATA_DIR): shutil.rmtree(SCHEMATA_DIR) mapper = ClassMapper([Dog, Cat, JackRussell, Person, Monkey]) self.mutator = FamMutator(mapper, DATA_PATH) self.db = FirestoreWrapper(mapper, CREDS, validator=self.mutator, namespace="http://glowinthedark.co.uk/test") if self.db.db.project != "orcast-test": raise Exception("wrong db: " % self.db.db.project)
class TestFireStoreAuth(unittest.TestCase): def setUp(self): mapper = ClassMapper([Dog, Cat, Person, JackRussell, Monkey, Monarch]) # self.db = FirestoreWrapper(mapper, creds_path) cred = credentials.Certificate(CREDS) app = firebase_admin.initialize_app(cred) uid = 'some-uid' additional_claims = { 'premiumAccount': True } custom_token = auth.create_custom_token(uid, additional_claims) firebase_admin.delete_app(app) self.db = FirestoreWrapper(mapper, None, project_id=CREDS["project_id"], custom_token=custom_token.decode("utf-8"), api_key=API_KEY, namespace=NAMESPACE ) def tearDown(self): firebase_admin.delete_app(self.db.app) def test_make_an_object_and_refresh(self): dog = Dog.create(self.db, name="woofer") dog2 = Dog.get(self.db, dog.key) self.assertIsNotNone(dog2) time.sleep(1) self.db.refresh() dog3 = Dog.create(self.db, name="another") dog4 = Dog.get(self.db, dog3.key) self.assertIsNotNone(dog4)
def setUp(self): mapper = ClassMapper([Dog, Cat, Person, JackRussell, Monkey, Monarch]) # self.db = FirestoreWrapper(mapper, creds_path) cred = credentials.Certificate(CREDS) app = firebase_admin.initialize_app(cred) uid = 'some-uid' additional_claims = { 'premiumAccount': True } custom_token = auth.create_custom_token(uid, additional_claims) firebase_admin.delete_app(app) self.db = FirestoreWrapper(mapper, None, project_id=CREDS["project_id"], custom_token=custom_token.decode("utf-8"), api_key=API_KEY, namespace=NAMESPACE )
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)
def setUpClass(cls): mapper = ClassMapper([Fish]) cls.db = FirestoreWrapper(mapper, None, namespace=fish_namespace) if cls.db.db.project != "localtest": raise Exception("wrong db: %s" % cls.db.db.project)
def setUpClass(cls): mapper = ClassMapper([Dog, Cat, Person, JackRussell, Monkey, Monarch]) cls.db = FirestoreWrapper(mapper, None, namespace=NAMESPACE) if cls.db.db.project != "localtest": raise Exception("wrong db: %s" % cls.db.db.project)
class MutationTests(unittest.TestCase): def clear_db(self): self.db.delete_all("dog") self.db.delete_all("cat") self.db.delete_all("jackrussell") self.db.delete_all("person") self.db.delete_all("monkey") def setUp(self): if os.path.exists(SCHEMATA_DIR): shutil.rmtree(SCHEMATA_DIR) mapper = ClassMapper([Dog, Cat, JackRussell, Person, Monkey]) self.mutator = FamMutator(mapper, DATA_PATH) self.db = FirestoreWrapper(mapper, CREDS, validator=self.mutator, namespace="http://glowinthedark.co.uk/test") if self.db.db.project != "orcast-test": raise Exception("wrong db: " % self.db.db.project) def _update_mutation(self, dst, schema_id): src_file = os.path.join(DATA_PATH, "dog_mutation.py") with open(src_file, "r") as f: txt = f.read() txt = txt.replace("TEMPLATE_SCHEMA_ID", schema_id) os.remove(dst) with open(dst, "w") as f: f.write(txt) def _add_changes(self): from fam.tests.test_mutation.models.test02 import Dog, Cat, JackRussell, Person, Monkey self.mapper = ClassMapper([Dog, Cat, JackRussell, Person, Monkey]) self.db.mapper = self.mapper self.mutator = FamMutator(self.mapper, DATA_PATH) self.mutator.db = self.db def test_creates_schemata(self): self.mutator.update_ref_schemata() self.assertEqual(len(self.mutator.changes), 5) for namespace, type_name, timestamp in self.mutator.changes: schema_dir = self.mutator._schema_path(namespace, type_name, timestamp) schema_filepath = os.path.join(schema_dir, "schema.json") self.assertTrue(os.path.exists(schema_filepath)) def test_creates_mutations(self): self.mutator.update_ref_schemata() self.assertEqual(len(self.mutator.changes), 5) self._add_changes() self.mutator.update_ref_schemata() self.assertEqual(len(self.mutator.changes), 2) for namespace, type_name, timestamp in self.mutator.changes: schema_dir = self.mutator._schema_path(namespace, type_name, timestamp) mutation_filepath = os.path.join(schema_dir, "mutation.py") self.assertTrue(os.path.exists(mutation_filepath)) def test_index_mutations(self): self.mutator.update_ref_schemata() self.assertEqual(len(self.mutator.changes), 5) self._add_changes() self.mutator.update_ref_schemata() self.assertEqual(len(self.mutator.changes), 2) self.mutator._index_all_mutations() self.assertEqual(len(self.mutator.mutation_histories), 5) self.assertEqual( len(self.mutator.mutation_histories[( "http://glowinthedark.co.uk/test", "dog")]), 1) self.assertEqual( len(self.mutator.mutation_histories[( "http://glowinthedark.co.uk/test", "cat")]), 0) def test_find_not_implemented(self): self.mutator.update_ref_schemata() self._add_changes() self.mutator.update_ref_schemata() self.assertRaises(NotImplementedError, self.mutator.check_not_implemented_mutations) def test_update_mutations(self): self.mutator.update_ref_schemata() self._add_changes() self.mutator.update_ref_schemata() self.assertRaises(NotImplementedError, self.mutator.check_not_implemented_mutations) for namespace, type_name, timestamp in self.mutator.changes: schema_dir = self.mutator._schema_path(namespace, type_name, timestamp) mutation_filepath = os.path.join(schema_dir, "mutation.py") schema_id = self.mutator.ref_schemas[(namespace, type_name)]["id"] self._update_mutation(mutation_filepath, schema_id) self.mutator.all_mutations = {} self.mutator.check_not_implemented_mutations() def test_mutate(self): self.clear_db() self.mutator.update_ref_schemata() namespace = "http://glowinthedark.co.uk/test" first_dog_schema = self.mutator.ref_schemas[(namespace, "dog")]["id"] dog = Dog(name="fly") self.db.put(dog) got_dog = Dog.get(self.db, dog.key) self.assertEqual(first_dog_schema, got_dog.schema) self._add_changes() self.mutator.update_ref_schemata() self.assertRaises(NotImplementedError, self.mutator.check_not_implemented_mutations) # fix the mutations for namespace, type_name, timestamp in self.mutator.changes: schema_dir = self.mutator._schema_path(namespace, type_name, timestamp) mutation_filepath = os.path.join(schema_dir, "mutation.py") schema_id = self.mutator.ref_schemas[(namespace, type_name)]["id"] self._update_mutation(mutation_filepath, schema_id) self.mutator.all_mutations = {} self.mutator.check_not_implemented_mutations() second_dog_schema = self.mutator.ref_schemas[(namespace, "dog")]["id"] self.assertNotEqual(first_dog_schema, second_dog_schema) dog_key = dog.key time.sleep(1) gotDog = Dog.get(self.db, dog_key) self.assertEqual(first_dog_schema, gotDog.schema) self.mutator.mutate() gotDog = Dog.get(self.db, dog_key) self.assertEqual(second_dog_schema, gotDog.schema) self.assertEqual("red", gotDog.colour) # remove the schema to check lazy load del self.mutator.reference_store[second_dog_schema] gotDog.colour = "blue" self.db.put(gotDog) # some tests of the indexing namespace, type_name, timestamp = self.mutator._namespace_typename_timestamp_from_schema_id( second_dog_schema) dogs = list( self.mutator._iter_of_of_date_obj(namespace, type_name, timestamp)) self.assertTrue(len(dogs) == 0) cats = list( self.mutator._iter_of_of_date_obj(namespace, "cats", timestamp)) self.assertTrue(len(cats) == 0) zebra = list( self.mutator._iter_of_of_date_obj(namespace, "zebra", timestamp)) self.assertTrue(len(zebra) == 0) # move timestamp later dt = self.mutator._datetime_from_timestamp(timestamp) later_dt = dt + datetime.timedelta(seconds=1) later_timestamp = self.mutator._timestamp_from_datetime(later_dt) dogs = list( self.mutator._iter_of_of_date_obj(namespace, type_name, later_timestamp)) self.assertTrue(len(dogs) == 1) # move timestamp earlier dt = self.mutator._datetime_from_timestamp(timestamp) earlier_dt = dt + datetime.timedelta(seconds=-1) earlier_timestamp = self.mutator._timestamp_from_datetime(earlier_dt) dogs = list( self.mutator._iter_of_of_date_obj(namespace, type_name, earlier_timestamp)) self.assertTrue(len(dogs) == 0) def test_mutate_typed(self): self.clear_db() self.mutator.update_ref_schemata() namespace = "http://glowinthedark.co.uk/test" first_dog_schema = self.mutator.ref_schemas[(namespace, "dog")]["id"] dog = Dog(name="fly") self.db.put(dog) got_dog = Dog.get(self.db, dog.key) self.assertEqual(first_dog_schema, got_dog.schema) self._add_changes() self.mutator.update_ref_schemata() self.assertRaises(NotImplementedError, self.mutator.check_not_implemented_mutations) # fix the mutations for namespace, type_name, timestamp in self.mutator.changes: schema_dir = self.mutator._schema_path(namespace, type_name, timestamp) mutation_filepath = os.path.join(schema_dir, "mutation.py") schema_id = self.mutator.ref_schemas[(namespace, type_name)]["id"] self._update_mutation(mutation_filepath, schema_id) self.mutator.all_mutations = {} self.mutator.check_not_implemented_mutations() second_dog_schema = self.mutator.ref_schemas[(namespace, "dog")]["id"] self.assertNotEqual(first_dog_schema, second_dog_schema) dog_key = dog.key time.sleep(1) gotDog = Dog.get(self.db, dog_key) self.assertEqual(first_dog_schema, gotDog.schema) self.mutator.mutate() gotDog = Dog.get(self.db, dog_key) self.assertEqual(second_dog_schema, gotDog.schema) self.assertEqual("red", gotDog.colour) # remove the schema to check lazy load del self.mutator.reference_store[second_dog_schema] gotDog.colour = "blue" self.db.put(gotDog) # some tests of the indexing namespace, type_name, timestamp = self.mutator._namespace_typename_timestamp_from_schema_id( second_dog_schema) dogs = list( self.mutator._iter_of_of_date_obj(namespace, type_name, timestamp)) self.assertTrue(len(dogs) == 0) cats = list( self.mutator._iter_of_of_date_obj(namespace, "cats", timestamp)) self.assertTrue(len(cats) == 0) zebra = list( self.mutator._iter_of_of_date_obj(namespace, "zebra", timestamp)) self.assertTrue(len(zebra) == 0) # move timestamp later dt = self.mutator._datetime_from_timestamp(timestamp) later_dt = dt + datetime.timedelta(seconds=1) later_timestamp = self.mutator._timestamp_from_datetime(later_dt) dogs = list( self.mutator._iter_of_of_date_obj(namespace, type_name, later_timestamp)) self.assertTrue(len(dogs) == 1) # move timestamp earlier dt = self.mutator._datetime_from_timestamp(timestamp) earlier_dt = dt + datetime.timedelta(seconds=-1) earlier_timestamp = self.mutator._timestamp_from_datetime(earlier_dt) dogs = list( self.mutator._iter_of_of_date_obj(namespace, type_name, earlier_timestamp)) self.assertTrue(len(dogs) == 0)
def setUpClass(cls): mapper = ClassMapper([House, Fence]) cls.db = FirestoreWrapper(mapper, None, namespace=NAMESPACE) if cls.db.db.project != "localtest": raise Exception("wrong db: %s" % cls.db.db.project)
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)