Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dionhaefner committed May 15, 2023
1 parent a2e1ddb commit 34248b7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ def testdb(raster_file, tmpdir_factory):
return dbpath


@pytest.fixture(scope="session")
@pytest.fixture()
def v07_db(tmpdir_factory):
"""A read-only, pre-populated test database"""
"""A read-only, pre-populated test database at terracotta v0.7"""
import shutil

dbpath = tmpdir_factory.mktemp("db").join("db-outdated.sqlite")
Expand Down
46 changes: 40 additions & 6 deletions tests/scripts/test_migrate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from click.testing import CliRunner


Expand All @@ -9,6 +10,38 @@ def parse_version(verstr):


def test_migrate(v07_db, monkeypatch, force_reload):
"""Test database migration to this major version."""
import terracotta
from terracotta import get_driver
from terracotta.scripts import cli

current_version = parse_version(terracotta.__version__)

# run migration
runner = CliRunner()
result = runner.invoke(
cli.cli,
[
"migrate",
str(v07_db),
"--from",
"v0.7",
"--to",
f"v{current_version[0]}.{current_version[1]}",
"--yes",
],
)

assert result.exit_code == 0
assert "Upgrade path found" in result.output

driver_updated = get_driver(str(v07_db), provider="sqlite")

# key_names did not exist in v0.7
assert driver_updated.key_names == ("key1", "akey", "key2")


def test_migrate_next(v07_db, monkeypatch, force_reload):
"""Test database migration to next major version if one is available."""
with monkeypatch.context() as m:
# pretend we are at the next major version
Expand All @@ -18,22 +51,23 @@ def test_migrate(v07_db, monkeypatch, force_reload):
next_major_version = (current_version[0], current_version[1] + 1, 0)
m.setattr(terracotta, "__version__", ".".join(map(str, next_major_version)))

# run migration
from terracotta import get_driver
from terracotta.scripts import cli
from terracotta.migrations import MIGRATIONS

if next_major_version[:2] not in [m.up_version for m in MIGRATIONS.values()]:
pytest.skip("No migration available for next major version")

# run migration
runner = CliRunner()
result = runner.invoke(
cli.cli, ["migrate", str(v07_db), "--from", "v0.7", "--yes"]
)
assert result.exit_code == 0

if next_major_version[:2] not in [m.up_version for m in MIGRATIONS.values()]:
assert "Unknown target version" in result.output
return

assert result.exit_code == 0
assert "Upgrade path found" in result.output

driver_updated = get_driver(str(v07_db), provider="sqlite")

# key_names did not exist in v0.7
assert driver_updated.key_names == ("key1", "akey", "key2")
19 changes: 6 additions & 13 deletions tests/server/test_flask_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from io import BytesIO
import json
import urllib.parse
from collections import OrderedDict

from PIL import Image
import numpy as np
Expand Down Expand Up @@ -94,40 +93,34 @@ def test_get_metadata_nonexisting(client, use_testdb):
def test_get_datasets(client, use_testdb):
rv = client.get("/datasets")
assert rv.status_code == 200
datasets = json.loads(rv.data, object_pairs_hook=OrderedDict)["datasets"]
datasets = json.loads(rv.data)["datasets"]
assert len(datasets) == 4
assert (
OrderedDict([("key1", "val11"), ("akey", "x"), ("key2", "val12")]) in datasets
)
assert dict([("key1", "val11"), ("akey", "x"), ("key2", "val12")]) in datasets


def test_get_datasets_pagination(client, use_testdb):
# no page (implicit 0)
rv = client.get("/datasets?limit=2")
assert rv.status_code == 200
response = json.loads(rv.data, object_pairs_hook=OrderedDict)
response = json.loads(rv.data)
assert response["limit"] == 2
assert response["page"] == 0

first_datasets = response["datasets"]
assert len(first_datasets) == 2
assert (
OrderedDict([("key1", "val11"), ("akey", "x"), ("key2", "val12")])
in first_datasets
)
assert dict([("key1", "val11"), ("akey", "x"), ("key2", "val12")]) in first_datasets

# second page
rv = client.get("/datasets?limit=2&page=1")
assert rv.status_code == 200
response = json.loads(rv.data, object_pairs_hook=OrderedDict)
response = json.loads(rv.data)
assert response["limit"] == 2
assert response["page"] == 1

last_datasets = response["datasets"]
assert len(last_datasets) == 2
assert (
OrderedDict([("key1", "val11"), ("akey", "x"), ("key2", "val12")])
not in last_datasets
dict([("key1", "val11"), ("akey", "x"), ("key2", "val12")]) not in last_datasets
)

# page out of range
Expand Down

0 comments on commit 34248b7

Please sign in to comment.