import logging
import unittest
from wqflask import app
from elasticsearch import Elasticsearch, TransportError

class ParametrizedTest(unittest.TestCase):

    def __init__(self, methodName='runTest', gn2_url="http://localhost:5003", es_url="localhost:9200"):
        super(ParametrizedTest, self).__init__(methodName=methodName)
        self.gn2_url = gn2_url
        self.es_url = es_url

    def setUp(self):
        self.es = Elasticsearch([self.es_url])
        self.es_cleanup = []

        es_logger = logging.getLogger("elasticsearch")
        es_logger.setLevel(app.config.get("LOG_LEVEL"))
        es_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationInfo.log"))
        es_trace_logger = logging.getLogger("elasticsearch.trace")
        es_trace_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationTrace.log"))

    def tearDown(self):
        self.es.delete_by_query(
            index="users"
            , doc_type="local"
            , body={"query":{"match":{"email_address":"test@user.com"}}})

import logging
import unittest
from wqflask import app
from utility.elasticsearch_tools import get_elasticsearch_connection, get_user_by_unique_column
from elasticsearch import Elasticsearch, TransportError

class ParametrizedTest(unittest.TestCase):

    def __init__(self, methodName='runTest', gn2_url="http://localhost:5003", es_url="localhost:9200"):
        super(ParametrizedTest, self).__init__(methodName=methodName)
        self.gn2_url = gn2_url
        self.es_url = es_url

    def setUp(self):
        self.es = get_elasticsearch_connection()
        self.es_cleanup = []

        es_logger = logging.getLogger("elasticsearch")
        es_logger.setLevel(app.config.get("LOG_LEVEL"))
        es_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationInfo.log"))
        es_trace_logger = logging.getLogger("elasticsearch.trace")
        es_trace_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationTrace.log"))

    def tearDown(self):
        from time import sleep
        self.es.delete_by_query(
            index="users"
            , doc_type="local"
            , body={"query":{"match":{"email_address":"test@user.com"}}})
        sleep(1)

Use existing code. Delay after delete.
Use existing code. Delay after delete.

* Use existing code to get the elasticsearch connection. This should
  prevent tests from failing in case the way connections to
  elasticsearch are made change.
* Delay a while after deleting to allow elasticsearch to re-index the
  data, thus preventing subtle bugs in the test.

  import logging
  import unittest
  from wqflask import app
+ from utility.elasticsearch_tools import get_elasticsearch_connection, get_user_by_unique_column
  from elasticsearch import Elasticsearch, TransportError
  
  class ParametrizedTest(unittest.TestCase):
  
      def __init__(self, methodName='runTest', gn2_url="http://localhost:5003", es_url="localhost:9200"):
          super(ParametrizedTest, self).__init__(methodName=methodName)
          self.gn2_url = gn2_url
          self.es_url = es_url
  
      def setUp(self):
-         self.es = Elasticsearch([self.es_url])
+         self.es = get_elasticsearch_connection()
          self.es_cleanup = []
  
          es_logger = logging.getLogger("elasticsearch")
          es_logger.setLevel(app.config.get("LOG_LEVEL"))
          es_logger.addHandler(
              logging.FileHandler("/tmp/es_TestRegistrationInfo.log"))
          es_trace_logger = logging.getLogger("elasticsearch.trace")
          es_trace_logger.addHandler(
              logging.FileHandler("/tmp/es_TestRegistrationTrace.log"))
  
      def tearDown(self):
+         from time import sleep
          self.es.delete_by_query(
              index="users"
              , doc_type="local"
              , body={"query":{"match":{"email_address":"test@user.com"}}})
+         sleep(1)
  
Use existing code. Delay after delete.
## Code Before:
import logging
import unittest
from wqflask import app
from elasticsearch import Elasticsearch, TransportError

class ParametrizedTest(unittest.TestCase):

    def __init__(self, methodName='runTest', gn2_url="http://localhost:5003", es_url="localhost:9200"):
        super(ParametrizedTest, self).__init__(methodName=methodName)
        self.gn2_url = gn2_url
        self.es_url = es_url

    def setUp(self):
        self.es = Elasticsearch([self.es_url])
        self.es_cleanup = []

        es_logger = logging.getLogger("elasticsearch")
        es_logger.setLevel(app.config.get("LOG_LEVEL"))
        es_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationInfo.log"))
        es_trace_logger = logging.getLogger("elasticsearch.trace")
        es_trace_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationTrace.log"))

    def tearDown(self):
        self.es.delete_by_query(
            index="users"
            , doc_type="local"
            , body={"query":{"match":{"email_address":"test@user.com"}}})

## Instruction:
Use existing code. Delay after delete.
## Code After:
import logging
import unittest
from wqflask import app
from utility.elasticsearch_tools import get_elasticsearch_connection, get_user_by_unique_column
from elasticsearch import Elasticsearch, TransportError

class ParametrizedTest(unittest.TestCase):

    def __init__(self, methodName='runTest', gn2_url="http://localhost:5003", es_url="localhost:9200"):
        super(ParametrizedTest, self).__init__(methodName=methodName)
        self.gn2_url = gn2_url
        self.es_url = es_url

    def setUp(self):
        self.es = get_elasticsearch_connection()
        self.es_cleanup = []

        es_logger = logging.getLogger("elasticsearch")
        es_logger.setLevel(app.config.get("LOG_LEVEL"))
        es_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationInfo.log"))
        es_trace_logger = logging.getLogger("elasticsearch.trace")
        es_trace_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationTrace.log"))

    def tearDown(self):
        from time import sleep
        self.es.delete_by_query(
            index="users"
            , doc_type="local"
            , body={"query":{"match":{"email_address":"test@user.com"}}})
        sleep(1)
