def setUpClass(cls): registry = ModelRegistry() t = TestModel() conn = ElasticOSMConnection().connect(settings) registry.register_model(t) t = TestModel()
def test_uniques(self): registry = ModelRegistry() t = TestModelWithUniqueField() conn = ElasticOSMConnection().connect(settings) registry.register_model(t) t.string_field_not_unique = 'not unique' t.string_field_unique = 'should be unique' t.save() t2 = TestModelWithUniqueField() t2.string_field_not_unique = 'still not unique' t2.string_field_unique = 'should be unique' t3 = TestModelWithUniqueField() t3.string_field_not_unique = 'not unique' t3.string_field_unique = 'should be unique but different this time' t3.save() with self.assertRaises(UniqueFieldExistsException): self.save_model(t2)
def test_save_model(self): registry = ModelRegistry() t = TestModel() conn = ElasticOSMConnection().connect(settings) registry.register_model(t) t = TestModel() assign_test_values_to_fields(t) t.save()
def test_deletes(self): registry = ModelRegistry() t = TestModel() conn = ElasticOSMConnection().connect(settings) registry.register_model(t) t = TestModel() t.string_field = 'delete me' t.save() sleep(1) t.delete() sleep(1) test_deletes_rs = TestModel.filter(string_field='delete me') self.assertEqual(test_deletes_rs.count(), 0)
def test_query(self): registry = ModelRegistry() t = TestModel() conn = ElasticOSMConnection().connect(settings) registry.register_model(t) assign_test_values_to_fields(t) t.save() # give it a second to add to the index sleep(1) queried_models = TestModel.filter(string_field=string_field_test_value) models_found = queried_models.count() self.assertGreater(models_found, 0, "Only %s TestModels returned from query, should have found at least 1" % (models_found)) for m in queried_models: self.assertEqual(m.string_field, string_field_test_value)
def test_get(self): registry = ModelRegistry() t = TestModel() conn = ElasticOSMConnection().connect(settings) registry.register_model(t) t = TestModel() assign_test_values_to_fields(t) t.save() # give it a second to add to the index sleep(1) queried_model = TestModel.get(id=t.id) self.assertEqual(t.id, queried_model.id)
def connect(self, settings): self.default_database = settings.default_database self.default_database_uniques = "%s__uniques" % self.default_database self.servers = settings.servers self.connection = pyes.ES(self.servers) # create the db/index self.connection.create_index_if_missing(self.default_database) # create the uniques db/index self.connection.create_index_if_missing(self.default_database_uniques, __uniques_index_settings__) self.is_initialized = True try: self.encryption_key = settings.TOKEN_ENCRYPTION_KEY except: pass from elasticosm.models.registry import ModelRegistry register = ModelRegistry() apps = settings.ELASTIC_APPS for app in apps: register.register_models_for_app_name(app) #self.es_conn = __get_es_connection(database) #self.database = get_db() return self