mirror of
https://github.com/minetest/contentdb.git
synced 2024-11-08 08:33:45 +01:00
Add API tests
This commit is contained in:
parent
fd6ba459f9
commit
0bda16de6d
@ -18,5 +18,5 @@ UI_Test:
|
|||||||
- cp utils/gitlabci/* .
|
- cp utils/gitlabci/* .
|
||||||
- docker-compose up -d
|
- docker-compose up -d
|
||||||
- ./utils/run_migrations.sh
|
- ./utils/run_migrations.sh
|
||||||
- ./utils/tests.sh
|
- ./utils/tests_cov.sh
|
||||||
- docker-compose down
|
- docker-compose down
|
||||||
|
@ -1 +0,0 @@
|
|||||||
ignored-classes=SQLObject,Registrant,scoped_session
|
|
@ -11,6 +11,11 @@ def populate(session):
|
|||||||
admin_user.rank = UserRank.ADMIN
|
admin_user.rank = UserRank.ADMIN
|
||||||
session.add(admin_user)
|
session.add(admin_user)
|
||||||
|
|
||||||
|
session.add(MinetestRelease("None", 0))
|
||||||
|
session.add(MinetestRelease("0.4.16/17", 32))
|
||||||
|
session.add(MinetestRelease("5.0", 37))
|
||||||
|
session.add(MinetestRelease("5.1", 38))
|
||||||
|
|
||||||
tags = {}
|
tags = {}
|
||||||
for tag in ["Inventory", "Mapgen", "Building", \
|
for tag in ["Inventory", "Mapgen", "Building", \
|
||||||
"Mobs and NPCs", "Tools", "Player effects", \
|
"Mobs and NPCs", "Tools", "Player effects", \
|
||||||
@ -34,7 +39,14 @@ def populate(session):
|
|||||||
session.add(row)
|
session.add(row)
|
||||||
|
|
||||||
|
|
||||||
def populate_test_data(session, licenses, tags, admin_user):
|
def populate_test_data(session):
|
||||||
|
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()
|
||||||
|
v4 = MinetestRelease.query.filter_by(protocol=32).first()
|
||||||
|
v50 = MinetestRelease.query.filter_by(protocol=37).first()
|
||||||
|
v51 = MinetestRelease.query.filter_by(protocol=38).first()
|
||||||
|
|
||||||
ez = User("Shara")
|
ez = User("Shara")
|
||||||
ez.github_username = "Ezhh"
|
ez.github_username = "Ezhh"
|
||||||
ez.forums_username = "Shara"
|
ez.forums_username = "Shara"
|
||||||
@ -105,6 +117,7 @@ awards.register_achievement("award_mesefind",{
|
|||||||
|
|
||||||
rel = PackageRelease()
|
rel = PackageRelease()
|
||||||
rel.package = mod1
|
rel.package = mod1
|
||||||
|
rel.min_rel = v51
|
||||||
rel.title = "v1.0.0"
|
rel.title = "v1.0.0"
|
||||||
rel.url = "https://github.com/rubenwardy/awards/archive/master.zip"
|
rel.url = "https://github.com/rubenwardy/awards/archive/master.zip"
|
||||||
rel.approved = True
|
rel.approved = True
|
||||||
@ -218,6 +231,7 @@ No warranty is provided, express or implied, for any part of the project.
|
|||||||
rel = PackageRelease()
|
rel = PackageRelease()
|
||||||
rel.package = mod
|
rel.package = mod
|
||||||
rel.title = "v1.0.0"
|
rel.title = "v1.0.0"
|
||||||
|
rel.max_rel = v4
|
||||||
rel.url = "https://github.com/ezhh/handholds/archive/master.zip"
|
rel.url = "https://github.com/ezhh/handholds/archive/master.zip"
|
||||||
rel.approved = True
|
rel.approved = True
|
||||||
session.add(rel)
|
session.add(rel)
|
||||||
|
@ -472,8 +472,7 @@ class Package(db.Model):
|
|||||||
"short_description": self.short_desc,
|
"short_description": self.short_desc,
|
||||||
"type": self.type.toName(),
|
"type": self.type.toName(),
|
||||||
"release": release and release.id,
|
"release": release and release.id,
|
||||||
"thumbnail": (base_url + tnurl) if tnurl is not None else None,
|
"thumbnail": (base_url + tnurl) if tnurl is not None else None
|
||||||
"score": round(self.score * 10) / 10
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def getAsDictionary(self, base_url, version=None, protonum=None):
|
def getAsDictionary(self, base_url, version=None, protonum=None):
|
||||||
@ -708,8 +707,9 @@ class MinetestRelease(db.Model):
|
|||||||
name = db.Column(db.String(100), unique=True, nullable=False)
|
name = db.Column(db.String(100), unique=True, nullable=False)
|
||||||
protocol = db.Column(db.Integer, nullable=False, default=0)
|
protocol = db.Column(db.Integer, nullable=False, default=0)
|
||||||
|
|
||||||
def __init__(self, name=None):
|
def __init__(self, name=None, protocol=0):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.protocol = protocol
|
||||||
|
|
||||||
def getActual(self):
|
def getActual(self):
|
||||||
return None if self.name == "None" else self
|
return None if self.name == "None" else self
|
||||||
|
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):
|
def test_homepage_with_contents(client):
|
||||||
"""Start with a test database."""
|
"""Start with a test database."""
|
||||||
|
|
||||||
licenses = { x.name : x for x in License.query.all() }
|
populate_test_data(db.session)
|
||||||
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)
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
rv = client.get("/")
|
rv = client.get("/")
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import pytest
|
import pytest, json
|
||||||
from app import app
|
from app import app
|
||||||
from app.models import db, User
|
from app.models import db, User
|
||||||
from app.default_data import populate
|
from app.default_data import populate
|
||||||
@ -16,6 +16,21 @@ def recreate_db():
|
|||||||
populate(db.session)
|
populate(db.session)
|
||||||
db.session.commit()
|
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
|
@pytest.fixture
|
||||||
def client():
|
def client():
|
||||||
|
@ -40,6 +40,9 @@ if create_db:
|
|||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
||||||
print("Filling database...")
|
print("Filling database...")
|
||||||
|
|
||||||
populate(db.session)
|
populate(db.session)
|
||||||
if test_data:
|
if test_data:
|
||||||
populate_test_data(licenses, tags, User.filter_by(rank=UserRank.ADMIN).first())
|
populate_test_data(db.session)
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py python -m pytest app/tests/ --cov=app --disable-warnings"
|
docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py python -m pytest app/tests/ --disable-warnings"
|
||||||
|
3
utils/tests_cov.sh
Executable file
3
utils/tests_cov.sh
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py python -m pytest app/tests/ --cov=app --disable-warnings"
|
Loading…
Reference in New Issue
Block a user