Add API tests
This commit is contained in:
105
app/tests/test_api.py
Normal file
105
app/tests/test_api.py
Normal file
@@ -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)
|
||||
@@ -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("/")
|
||||
|
||||
@@ -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():
|
||||
|
||||
Reference in New Issue
Block a user