def test_default_connection_is_returned_by_default(): c = connections.Connections() con, con2 = object(), object() c.add_connection("default", con) c.add_connection("not-default", con2) assert c.get_connection() is con
def test_get_connection_created_connection_if_needed(): c = connections.Connections() c.configure(default={"hosts": ["es.com"]}, local={"hosts": ["localhost"]}) default = c.get_connection() local = c.get_connection("local") assert isinstance(default, Elasticsearch) assert isinstance(local, Elasticsearch) assert [{"host": "es.com"}] == default.transport.hosts assert [{"host": "localhost"}] == local.transport.hosts
def test_get_connection_created_connection_if_needed(): c = connections.Connections() c.configure(default={'hosts': ['es.com']}, local={'hosts': ['localhost']}) default = c.get_connection() local = c.get_connection('local') assert isinstance(default, Elasticsearch) assert isinstance(local, Elasticsearch) assert [{'host': 'es.com'}] == default.transport.hosts assert [{'host': 'localhost'}] == local.transport.hosts
def test_remove_connection_removes_both_conn_and_conf(): c = connections.Connections() c.configure(default={"hosts": ["es.com"]}, local={"hosts": ["localhost"]}) c.add_connection("local2", object()) c.remove_connection("default") c.get_connection("local2") c.remove_connection("local2") with raises(Exception): c.get_connection("local2") c.get_connection("default")
def test_configure_preserves_unchanged_connections(): c = connections.Connections() c.configure(default={"hosts": ["es.com"]}, local={"hosts": ["localhost"]}) default = c.get_connection() local = c.get_connection("local") c.configure(default={"hosts": ["not-es.com"]}, local={"hosts": ["localhost"]}) new_default = c.get_connection() new_local = c.get_connection("local") assert new_local is local assert new_default is not default
def test_remove_connection_removes_both_conn_and_conf(): c = connections.Connections() c.configure(default={'hosts': ['es.com']}, local={'hosts': ['localhost']}) c.add_connection('local2', object()) c.remove_connection('default') c.get_connection('local2') c.remove_connection('local2') with raises(Exception): c.get_connection('local2') c.get_connection('default')
def test_configure_preserves_unchanged_connections(): c = connections.Connections() c.configure(default={'hosts': ['es.com']}, local={'hosts': ['localhost']}) default = c.get_connection() local = c.get_connection('local') c.configure(default={'hosts': ['not-es.com']}, local={'hosts': ['localhost']}) new_default = c.get_connection() new_local = c.get_connection('local') assert new_local is local assert new_default is not default
def test_create_connection_adds_our_serializer(): c = connections.Connections() c.create_connection("testing", hosts=["es.com"]) assert c.get_connection("testing").transport.serializer is serializer.serializer
def test_create_connection_constructs_client(): c = connections.Connections() c.create_connection("testing", hosts=["es.com"]) con = c.get_connection("testing") assert [{"host": "es.com"}] == con.transport.hosts
class Connection: """Class to store json data in elastic""" __elastic: Elasticsearch __connection: connections.Connections __duplicate_dict: dict __host: str __port: int __index: str # The connection is a static class variable, reused every time __connection = connections.Connections() def __init__(self, host: str = None, port: int = None): self.__duplicate_dict = {} self.host = host if host else 'localhost' self.port = port if port else 9200 self.index = 'catalog' @property def host(self) -> str: return self.__host @property def port(self) -> int: return self.__port @property def index(self) -> str: if not self.get().indices.exists(self.__index): self.create_index(self.__index) return self.__index @property def connection_name(self) -> str: return self.__index + self.host + str(self.port) @host.setter def host(self, host: str): self.__host = host @port.setter def port(self, port: int): self.__port = port @index.setter def index(self, i: str): self.__index = i def get(self) -> Elasticsearch: """The connection is reused. On first try there will be no 'elastic' connection. Create it. Will create different connections for each host:port combination, by using that in the name.""" try: self.__elastic = type(self).__connection.get_connection( self.connection_name) except KeyError: type(self).__connection.configure( **{ self.connection_name: { 'hosts': [self.__host + ':' + str(self.__port)] } }) return self.get() return self.__elastic def close(self) -> None: """Force closing connection to elastic on given host and port. Only use if you know what you are doing""" try: type(self).__connection.get_connection(self.connection_name) except KeyError: return type(self).__connection.remove_connection(self.connection_name) print("closed connection to " + self.host + ':' + str(self.port)) def create_index(self, index): self.__elastic.indices.create(index=index, body=Constants.index)
def test_create_connection_adds_our_serializer(): c = connections.Connections() c.create_connection('testing', hosts=['es.com']) assert c.get_connection( 'testing').transport.serializer is serializer.serializer
def test_create_connection_constructs_client(): c = connections.Connections() c.create_connection('testing', hosts=['es.com']) con = c.get_connection('testing') assert [{'host': 'es.com'}] == con.transport.hosts