From 0874dc062eab600af47e5e901121d2b52011eea5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Thuret?= <contact@sebastien-thuret.fr>
Date: Sun, 3 Apr 2022 11:07:37 +0200
Subject: [PATCH] reorganization of tests (1 route per file), add some basic
 tests

---
 tests/test_api/__init__.py                    |  0
 tests/test_api/conftest.py                    | 18 +++++++++++++
 tests/test_api/test_api_detect_language.py    | 26 +++++++++++++++++++
 tests/test_api/test_api_frontend_settings.py  |  4 +++
 .../test_api_get_languages.py}                | 21 +++++----------
 tests/test_api/test_api_spec.py               | 10 +++++++
 6 files changed, 64 insertions(+), 15 deletions(-)
 create mode 100644 tests/test_api/__init__.py
 create mode 100644 tests/test_api/conftest.py
 create mode 100644 tests/test_api/test_api_detect_language.py
 create mode 100644 tests/test_api/test_api_frontend_settings.py
 rename tests/{test_api.py => test_api/test_api_get_languages.py} (53%)
 create mode 100644 tests/test_api/test_api_spec.py

diff --git a/tests/test_api/__init__.py b/tests/test_api/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_api/conftest.py b/tests/test_api/conftest.py
new file mode 100644
index 0000000..3fc54aa
--- /dev/null
+++ b/tests/test_api/conftest.py
@@ -0,0 +1,18 @@
+import sys
+import pytest
+
+from app.app import create_app
+from app.main import get_args
+
+
+@pytest.fixture()
+def app():
+    sys.argv = ['']
+    app = create_app(get_args())
+
+    yield app
+
+
+@pytest.fixture()
+def client(app):
+    return app.test_client()
diff --git a/tests/test_api/test_api_detect_language.py b/tests/test_api/test_api_detect_language.py
new file mode 100644
index 0000000..d85600c
--- /dev/null
+++ b/tests/test_api/test_api_detect_language.py
@@ -0,0 +1,26 @@
+import json
+
+
+def test_api_detect_language(client):
+    response = client.post("/detect", data={
+        "q": "Hello"
+    })
+    response_json = json.loads(response.data)
+
+    assert "confidence" in response_json[0] and "language" in response_json[0]
+    assert len(response_json) >= 1
+    assert response.status_code == 200
+
+
+def test_api_detect_language_must_fail_without_parameters(client):
+    response = client.post("/detect")
+    response_json = json.loads(response.data)
+
+    assert "error" in response_json
+    assert response.status_code == 400
+
+
+def test_api_detect_language_must_fail_bad_request_type(client):
+    response = client.get("/detect")
+
+    assert response.status_code == 405
diff --git a/tests/test_api/test_api_frontend_settings.py b/tests/test_api/test_api_frontend_settings.py
new file mode 100644
index 0000000..8c3070a
--- /dev/null
+++ b/tests/test_api/test_api_frontend_settings.py
@@ -0,0 +1,4 @@
+def test_api_get_frontend_settings(client):
+    response = client.get("/frontend/settings")
+
+    assert response.status_code == 200
diff --git a/tests/test_api.py b/tests/test_api/test_api_get_languages.py
similarity index 53%
rename from tests/test_api.py
rename to tests/test_api/test_api_get_languages.py
index 5663e19..75404f1 100644
--- a/tests/test_api.py
+++ b/tests/test_api/test_api_get_languages.py
@@ -1,20 +1,5 @@
-import pytest
-import sys
 import json
-from app.app import create_app
-from app.main import get_args
 
-@pytest.fixture()
-def app():
-    sys.argv=['']
-    app = create_app(get_args())
-
-    yield app
-
-
-@pytest.fixture()
-def client(app):
-    return app.test_client()
 
 def test_api_get_languages(client):
     response = client.get("/languages")
@@ -23,3 +8,9 @@ def test_api_get_languages(client):
     assert "code" in response_json[0] and "name" in response_json[0]
     assert len(response_json) >= 1
     assert response.status_code == 200
+
+
+def test_api_get_languages_must_fail_bad_request_type(client):
+    response = client.post("/spec")
+
+    assert response.status_code == 405
diff --git a/tests/test_api/test_api_spec.py b/tests/test_api/test_api_spec.py
new file mode 100644
index 0000000..3922c44
--- /dev/null
+++ b/tests/test_api/test_api_spec.py
@@ -0,0 +1,10 @@
+def test_api_get_spec(client):
+    response = client.get("/spec")
+
+    assert response.status_code == 200
+
+
+def test_api_get_spec_must_fail_bad_request_type(client):
+    response = client.post("/spec")
+
+    assert response.status_code == 405
-- 
GitLab