aboutsummaryrefslogtreecommitdiff
path: root/app/tests
diff options
context:
space:
mode:
Diffstat (limited to 'app/tests')
-rw-r--r--app/tests/test_api.py105
-rw-r--r--app/tests/test_homepage.py6
-rw-r--r--app/tests/utils.py17
3 files changed, 122 insertions, 6 deletions
diff --git a/app/tests/test_api.py b/app/tests/test_api.py
new file mode 100644
index 0000000..8009656
--- /dev/null
+++ b/app/tests/test_api.py
@@ -0,0 +1,105 @@
+import pytest
+from app import app
+from app.default_data import populate_test_data
+from app.models import db, License, Tag, User, UserRank, Package
+from utils import client, recreate_db, parse_json
+from utils import is_str, is_int, is_optional
+
+def validate_package_list(packages, strict=False):
+ valid_keys = {
+ "author", "name", "release",
+ "short_description", "thumbnail",
+ "title", "type"
+ }
+
+ for package in packages:
+ assert set(package.keys()).issubset(valid_keys)
+
+ assert is_str(package.get("author"))
+ assert is_str(package.get("name"))
+ if strict:
+ assert is_int(package.get("release"))
+ else:
+ assert is_optional(int, package.get("release"))
+ assert is_str(package.get("short_description"))
+ assert is_optional(str, package.get("thumbnail"))
+ assert is_str(package.get("title"))
+ assert is_str(package.get("type"))
+
+
+def test_packages_empty(client):
+ """Start with a blank database."""
+
+ rv = client.get("/api/packages/")
+ assert parse_json(rv.data) == []
+
+
+def test_packages_with_contents(client):
+ """Start with a test database."""
+
+
+ populate_test_data(db.session)
+ db.session.commit()
+
+ rv = client.get("/api/packages/")
+
+ packages = parse_json(rv.data)
+
+ assert len(packages) > 0
+ assert len(packages) == Package.query.filter_by(approved=True).count()
+
+ validate_package_list(packages)
+
+
+def test_packages_with_query(client):
+ """Start with a test database."""
+
+ populate_test_data(db.session)
+ db.session.commit()
+
+ rv = client.get("/api/packages/?q=food")
+
+ packages = parse_json(rv.data)
+
+ assert len(packages) == 2
+
+ validate_package_list(packages)
+
+ assert (packages[0]["name"] == "food" and packages[1]["name"] == "food_sweet") or \
+ (packages[1]["name"] == "food" and packages[0]["name"] == "food_sweet")
+
+
+def test_packages_with_protocol_high(client):
+ """Start with a test database."""
+
+ populate_test_data(db.session)
+ db.session.commit()
+
+ rv = client.get("/api/packages/?protocol_version=40")
+
+ packages = parse_json(rv.data)
+
+ assert len(packages) == 4
+
+ for package in packages:
+ assert package["name"] != "mesecons"
+
+ validate_package_list(packages, True)
+
+
+def test_packages_with_protocol_low(client):
+ """Start with a test database."""
+
+ populate_test_data(db.session)
+ db.session.commit()
+
+ rv = client.get("/api/packages/?protocol_version=20")
+
+ packages = parse_json(rv.data)
+
+ assert len(packages) == 4
+
+ for package in packages:
+ assert package["name"] != "awards"
+
+ validate_package_list(packages, True)
diff --git a/app/tests/test_homepage.py b/app/tests/test_homepage.py
index 2bfdbca..1636b6d 100644
--- a/app/tests/test_homepage.py
+++ b/app/tests/test_homepage.py
@@ -14,11 +14,7 @@ def test_homepage_empty(client):
def test_homepage_with_contents(client):
"""Start with a test database."""
- licenses = { x.name : x for x in License.query.all() }
- tags = { x.name : x for x in Tag.query.all() }
- admin_user = User.query.filter_by(rank=UserRank.ADMIN).first()
-
- populate_test_data(db.session, licenses, tags, admin_user)
+ populate_test_data(db.session)
db.session.commit()
rv = client.get("/")
diff --git a/app/tests/utils.py b/app/tests/utils.py
index 782bb1d..be2b869 100644
--- a/app/tests/utils.py
+++ b/app/tests/utils.py
@@ -1,4 +1,4 @@
-import pytest
+import pytest, json
from app import app
from app.models import db, User
from app.default_data import populate
@@ -16,6 +16,21 @@ def recreate_db():
populate(db.session)
db.session.commit()
+def parse_json(b):
+ return json.loads(b.decode("utf8"))
+
+def is_type(t, v):
+ return v and isinstance(v, t)
+
+def is_optional(t, v):
+ return not v or isinstance(v, t)
+
+def is_str(v):
+ return is_type(str, v)
+
+def is_int(v):
+ return is_type(int, v)
+
@pytest.fixture
def client():