Skip to content

Commit

Permalink
Add test for lemmatization services
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Mar 19, 2024
1 parent 272b825 commit 19d3906
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/templates/main/corpus_new.html
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ <h1>{{ _('Create a new corpus') }}</h1>
var formData = new FormData();
formData.append("data", text_data.value);


// Set up our HTTP request
var xhr = new XMLHttpRequest();
var update = function(xhr) {
Expand All @@ -255,14 +254,16 @@ <h1>{{ _('Create a new corpus') }}</h1>
};
// Setup our listener to process compeleted requests
xhr.onreadystatechange = function () {

// Only run if the request is complete
if (xhr.readyState < 3 || xhr.readyState > 4) return null;
if (typeof xhr.status !== "number") return null;

if (xhr.readyState < 3) return null;

if (xhr.readyState === 3) {
update(xhr);
return null;
}
if (xhr.status === 0) return null;

// Process our return data
if (xhr.status >= 200 && xhr.status < 300) {
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class BaseTestConfig(Config):
LEMMATIZERS = [
LemmatizerService(
"Dummy lemmatizer",
"http://dummy-uri",
"http://localhost:4567/lemma",
provider="ProviderInstitution",
ui="someui.com",
apa="Clérice et al. 2019",
Expand Down
20 changes: 20 additions & 0 deletions test_lemmatizer_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from flask import Flask, request, Response, stream_with_context
app = Flask("fixture")


@app.route("/lemma", methods=["POST"])
def lemmatizing():
r = Response(
"\n".join(
["token\tlemma"] +
["\t".join([tok, f"{idx}"]) for idx, tok in enumerate(request.form.get("data").split())]
),
200,
headers={
'Content-Type': 'text/plain; charset=utf-8',
'Access-Control-Allow-Origin': "*"
}
)
return r

app.run(port="4567")
83 changes: 83 additions & 0 deletions tests/test_selenium/test_corpus_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,86 @@ def test_corpus_name_unique_user(self):
self.driver_find_element_by_id("submit").click()
self.driver.implicitly_wait(5)
self.assertFalse(self.driver_find_elements_by_css_selector(".alert.alert-danger"))

def test_lemmatization_service(self):
"""
Test that a user can create a corpus and that this corpus has its data well recorded
"""

# Click register menu link
self.driver_find_element_by_id("new_corpus_link").click()
self.driver.implicitly_wait(15)

# Fill in registration form
self.driver_find_element_by_id("corpusName").send_keys(PLAINTEXT_CORPORA["Wauchier"]["name"])
self.writeMultiline(self.driver_find_element_by_id("tokens"), PLAINTEXT_CORPORA["Wauchier"]["data"])

# Default
details = self.driver_find_elements_by_css_selector(".lemmatizer-details")
self.assertEqual(details[0].is_displayed(), False, "Nothing should be displayed by default")

# Select a lemmatizer
from selenium.webdriver.support.select import Select
s = Select(self.driver_find_elements_by_css_selector("#language-model")[0])
s.select_by_visible_text("Dummy lemmatizer")
self.assertEqual(details[0].is_displayed(), True, "Something should be displayed")
self.assertIn("Dummy lemmatizer is a lemmatization service provided by ProviderInstitution.", details[0].text)

# Unselect
s.select_by_visible_text("Select a service")
self.assertEqual(details[0].is_displayed(), False, "Nothing should be now")

def test_lemmatization_service_runs(self):
from flask import Flask, request, Response
from threading import Thread

app = Flask("fixture")

@app.route("/lemma", methods=["POST"])
def lemmatizing():
return Response(
"\n".join(
["token\tlemma"] +
["\t".join([tok, f"{idx}"]) for idx, tok in enumerate(request.form.get("data").split())]
),
200,
headers={
'Content-Type': 'text/plain; charset=utf-8',
'Access-Control-Allow-Origin': "*"
}
)

thread = Thread(target=app.run, daemon=True, kwargs=dict(host="localhost", port="4567"))
thread.start()

self.driver_find_element_by_id("new_corpus_link").click()
self.driver.implicitly_wait(15)

# Fill in registration form
self.driver_find_element_by_id("corpusName").send_keys("Test")
self.writeMultiline(self.driver_find_element_by_id("tokens"), "Je suis")

from selenium.webdriver.support.select import Select
s = Select(self.driver_find_elements_by_css_selector("#language-model")[0])
s.select_by_index(1)
self.driver.implicitly_wait(5)

self.driver_find_element_by_id("submit-model").click()

from selenium.webdriver.support.wait import WebDriverWait

wait = WebDriverWait(self.driver, timeout=5)
wait.until(lambda x: self.driver_find_element_by_id("tokens-success").is_displayed())

self.assertEqual(
self.driver_find_element_by_id("tokens").get_property("value").split("\n"),
["token\tlemma", "Je\t0", "suis\t1"]
)

self.assertEqual(
self.driver_find_element_by_id("tokens-success").text,
"Operation finished with success ! 2 tokens analyzed in total.",
"Lemmatization happened"
)

thread.join(1)

0 comments on commit 19d3906

Please sign in to comment.