1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
import pytest
from starlette.testclient import TestClient
import wn
import wn._db
from wn import web
# clearing connections on teardown (see conftest.py) isn't enough. For
# this we apparently need to monkeypatch the wn._db.pool as well.
@pytest.fixture
def mini_db_web(monkeypatch, mini_db_dir):
with monkeypatch.context() as m:
m.setattr(wn._db, 'pool', {})
m.setattr(wn.config, 'data_directory', mini_db_dir)
m.setattr(wn.config, 'allow_multithreading', True)
yield
wn._db.clear_connections()
client = TestClient(web.app)
@pytest.mark.usefixtures('mini_db_web')
def test_root():
response = client.get('/')
assert response.status_code == 404
@pytest.mark.usefixtures('mini_db_web')
def test_lexicons():
response = client.get("/lexicons")
assert response.status_code == 200
data = response.json()["data"]
assert [lex["id"] for lex in data] == ["test-en:1", "test-es:1"]
@pytest.mark.usefixtures('mini_db_web')
def test_words():
response = client.get("/words")
assert response.status_code == 200
data = response.json()["data"]
word_ids = {word["id"] for word in data}
assert "test-en-information-n" in word_ids
assert "test-es-información-n" in word_ids
response = client.get("/words", params={"lexicon": "test-en:1"})
assert response.status_code == 200
data = response.json()["data"]
word_ids = {word["id"] for word in data}
assert "test-en-information-n" in word_ids
assert "test-es-información-n" not in word_ids
@pytest.mark.usefixtures('mini_db_web')
def test_senses():
response = client.get("/senses")
assert response.status_code == 200
data = response.json()["data"]
sense_ids = {sense["id"] for sense in data}
assert "test-en-information-n-0001-01" in sense_ids
assert "test-es-información-n-0001-01" in sense_ids
response = client.get("/senses", params={"lexicon": "test-en:1"})
assert response.status_code == 200
data = response.json()["data"]
sense_ids = {sense["id"] for sense in data}
assert "test-en-information-n-0001-01" in sense_ids
assert "test-es-información-n-0001-01" not in sense_ids
@pytest.mark.usefixtures('mini_db_web')
def test_synsets():
response = client.get("/synsets")
assert response.status_code == 200
data = response.json()["data"]
synset_ids = {synset["id"] for synset in data}
assert "test-en-0001-n" in synset_ids
assert "test-es-0001-n" in synset_ids
response = client.get("/synsets", params={"lexicon": "test-en:1"})
assert response.status_code == 200
data = response.json()["data"]
synset_ids = {synset["id"] for synset in data}
assert "test-en-0001-n" in synset_ids
assert "test-es-0001-n" not in synset_ids
@pytest.mark.usefixtures('mini_db_web')
def test_lexicon_words():
response1 = client.get("/lexicons/test-en:1/words")
response2 = client.get("/words", params={"lexicon": "test-en:1"})
assert response1.status_code == 200
assert response2.status_code == 200
data1 = response1.json()["data"]
data2 = response2.json()["data"]
assert {word["id"] for word in data1} == {word["id"] for word in data2}
@pytest.mark.usefixtures('mini_db_web')
def test_lexicon_senses():
response1 = client.get("/lexicons/test-en:1/senses")
response2 = client.get("/senses", params={"lexicon": "test-en:1"})
assert response1.status_code == 200
assert response2.status_code == 200
data1 = response1.json()["data"]
data2 = response2.json()["data"]
assert {sense["id"] for sense in data1} == {sense["id"] for sense in data2}
@pytest.mark.usefixtures('mini_db_web')
def test_lexicon_synsets():
response1 = client.get("/lexicons/test-en:1/synsets")
response2 = client.get("/synsets", params={"lexicon": "test-en:1"})
assert response1.status_code == 200
assert response2.status_code == 200
data1 = response1.json()["data"]
data2 = response2.json()["data"]
assert {synset["id"] for synset in data1} == {synset["id"] for synset in data2}
|