From 4494287e3e903877f651ad834449e9a6e5013754 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 9 Sep 2021 18:07:25 -0700 Subject: [PATCH] Add index settings page unit test --- tests/test_character.py | 2 - tests/test_index.py | 91 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/test_index.py diff --git a/tests/test_character.py b/tests/test_character.py index 176bbc5..d21c3e2 100644 --- a/tests/test_character.py +++ b/tests/test_character.py @@ -15,7 +15,6 @@ def test_character_view(db: DbContext, app: Flask, make: ObjectFactory): username: str = f"user_{os.urandom(8).hex()}" charname: str = f"char_{os.urandom(8).hex()}" char_sig: str = f"signature_{os.urandom(8).hex()}" - # ub: bytes = username.encode("utf8") with app.test_client() as client: # Create the user and log in @@ -63,7 +62,6 @@ def test_character_view(db: DbContext, app: Flask, make: ObjectFactory): created_redirect, data={"name": charname, "signature": char_sig, "csrf_token": csrf_token}, ) - print(response.data.decode("utf8")) assert 300 <= response.status_code <= 399 # The character is updated diff --git a/tests/test_index.py b/tests/test_index.py new file mode 100644 index 0000000..10300ff --- /dev/null +++ b/tests/test_index.py @@ -0,0 +1,91 @@ +from amanuensis.db.models import IndexType +import os +from urllib.parse import urlsplit + +from bs4 import BeautifulSoup +from flask import Flask, url_for + +from amanuensis.backend import memq, charq, indq +from amanuensis.db import DbContext + +from tests.conftest import ObjectFactory + + +def test_index_view(db: DbContext, app: Flask, make: ObjectFactory): + """Test the lexicon index page""" + + with app.test_client() as client: + # Create the user and log in + user = make.user() + assert user + user_client = make.client(user.id) + assert client + user_client.login(client) + + # Create a lexicon and join as the editor + lexicon = make.lexicon() + assert lexicon + mem = memq.create(db, user.id, lexicon.id, is_editor=True) + assert mem + + # The index settings page exists + index_settings = url_for("lexicon.settings.index", lexicon_name=lexicon.name) + response = client.get(index_settings) + assert response.status_code == 200 + + # Add some indices + i1 = indq.create(db, lexicon.id, IndexType.CHAR, "ABCDE", 0, 0, 0) + assert i1 + p1 = i1.pattern + assert p1 + i2 = indq.create(db, lexicon.id, IndexType.RANGE, "F-M", 0, 0, 0) + assert i2 + p2 = i2.pattern + assert p2 + i3 = indq.create(db, lexicon.id, IndexType.CHAR, "NOPQ", 0, 0, 0) + assert i3 + p3 = i3.pattern + assert p3 + db.session.commit() + + # The index settings page shows the indices + response = client.get(index_settings) + assert response.status_code == 200 + # for i in indq.get_for_lexicon(db, lexicon.id): + assert p1.encode("utf8") in response.data + assert p2.encode("utf8") in response.data + assert p3.encode("utf8") in response.data + + # Indices can be modified + soup = BeautifulSoup(response.data, features="html.parser") + csrf_token = soup.find(id="csrf_token")["value"] + assert csrf_token + response = client.post( + index_settings, + data={ + "csrf_token": csrf_token, + "indices-0-index_type": "CHAR", + "indices-0-pattern": "ABCDEF", + "indices-0-logical_order": 0, + "indices-0-display_order": 0, + "indices-0-capacity": "", + "indices-1-index_type": "PREFIX", + "indices-1-pattern": "F-M", + "indices-1-logical_order": 1, + "indices-1-display_order": -1, + "indices-1-capacity": "", + "indices-2-index_type": "", + "indices-2-pattern": "NOPQ", + "indices-2-logical_order": 0, + "indices-2-display_order": 0, + "indices-2-capacity": "", + }, + ) + assert 300 <= response.status_code <= 399 + + updated_indices = list(indq.get_for_lexicon(db, lexicon.id)) + assert len(updated_indices) == 2 + assert updated_indices[0].index_type == IndexType.CHAR + assert updated_indices[0].pattern == "ABCDEF" + assert updated_indices[1].index_type == IndexType.PREFIX + assert updated_indices[1].pattern == "F-M"