Repository: opus-42/superset-api-client (language: python)

Issue:
Add integration test suite
Since the sole purpose of this client is to interface with the Superset API, testing should be focused on running against an actual Superset API instead of mocking Superset API endpoints.

An integration test suite can build on top of the docker-compose config already present.


Discussion / hints:

Fix patch:
diff --git a/.env b/.env
new file mode 100644
index 0000000..8d73393
--- /dev/null
+++ b/.env
@@ -0,0 +1,2 @@
+SUPERSET_VERSION=1.3.1
+LOAD_EXAMPLES=0
diff --git a/.gitignore b/.gitignore
index c3c516b..7cbdaf3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -109,7 +109,6 @@ celerybeat.pid
 *.sage.py
 
 # Environments
-.env
 .venv
 env/
 venv/
@@ -142,4 +141,4 @@ dmypy.json
 # profiling data
 .prof
 
-# End of https://www.toptal.com/developers/gitignore/api/python
\ No newline at end of file
+# End of https://www.toptal.com/developers/gitignore/api/python
diff --git a/docker-compose.yml b/docker-compose.yml
index a99a410..15cf35e 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,15 +1,16 @@
 version: "3.7"
 services:
   superset:
-    image: apache/superset:1.3.1
+    image: "apache/superset:${SUPERSET_VERSION}"
     container_name: superset_app
     command: ["bash", "/app/superset_home/entrypoint.sh"]
     tty: true
     restart: unless-stopped
     ports:
       - "127.0.0.1:8080:8080"
-    # environment:
+    environment:
+      - LOAD_EXAMPLES="${LOAD_EXAMPLES}"
       # - SUPERSET_CONFIG_PATH=/app/superset_home/test_config.py
     volumes:
       - ./scripts/entrypoint.sh:/app/superset_home/entrypoint.sh
-      # - ./tests/superset_test_config.py:/app/superset_home/test_config.py
\ No newline at end of file
+      # - ./tests/superset_test_config.py:/app/superset_home/test_config.py
diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh
index 9a32688..afd256b 100644
--- a/scripts/entrypoint.sh
+++ b/scripts/entrypoint.sh
@@ -3,5 +3,7 @@
 superset db upgrade
 superset init
 superset fab create-admin --username admin --firstname admin --lastname admin --email admin --password admin
-superset load_examples
-superset run -h 0.0.0.0 -p 8080
\ No newline at end of file
+if [ "${LOAD_EXAMPLES}" -eq 1 ]; then
+  superset load_examples
+fi
+superset run -h 0.0.0.0 -p 8080
diff --git a/setup.cfg b/setup.cfg
index c9e6fe2..da77217 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -20,5 +20,5 @@ install_requires =
 python_requires = >=3.6
 
 [options.extras_require]
-dev = flake8; pytest; requests-mock
+dev = flake8; pytest; requests-mock; pytest-docker
 build = build


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
index 61db9a6..f7a11bf 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,8 +1,11 @@
 """Fixture and superset app for testing."""
 import json
+import os
 from pathlib import Path
 
 import pytest
+import requests
+import requests.exceptions
 import requests_mock  # noqa
 
 from supersetapiclient.client import SupersetClient
@@ -50,3 +53,31 @@ def client(permanent_requests):
         "test"
     )
     yield client
+
+
+def is_responsive(url):
+    try:
+        return requests.get(url).status_code == 200
+    except requests.exceptions.ConnectionError:
+        return False
+
+
+@pytest.fixture(scope="session")
+def superset_api(docker_ip, docker_services):
+    """Ensure that Superset API is up and responsive."""
+
+    # `port_for` takes a container port and returns the corresponding host port
+    schema = "http"
+    port = docker_services.port_for("superset", 8080)
+    url = f"{schema}://{docker_ip}:{port}"
+    if schema == "http":
+        os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
+    docker_services.wait_until_responsive(
+        timeout=600, pause=1.0, check=lambda: is_responsive(url)
+    )
+    yield SupersetClient(url, "admin", "admin")
+
+
+@pytest.fixture(scope="session")
+def docker_compose_file(pytestconfig):
+    return str(Path(__file__).parent.parent / "docker-compose.yml")
diff --git a/tests/integration/test_basic.py b/tests/integration/test_basic.py
new file mode 100644
index 0000000..997548a
--- /dev/null
+++ b/tests/integration/test_basic.py
@@ -0,0 +1,2 @@
+def test_connection(superset_api):
+    assert superset_api.databases.find() == []