Skip to content

Commit

Permalink
correction erreurs tests - bug new corpus
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliettejns committed Jun 28, 2024
1 parent f5517e9 commit b5a2355
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 33 deletions.
4 changes: 2 additions & 2 deletions app/control_lists/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def information_read(control_list_id):
return render_template_with_nav_info('control_lists/information_read.html', control_list=control_list)


@control_lists_bp.route("/controls/<int:control_list_id>/ignore_terms", methods=["POST", "GET"])
"""@control_lists_bp.route("/controls/<int:control_list_id>/ignore_terms", methods=["POST", "GET"])
@login_required
def ignore_terms_filter(control_list_id):
current_controlListUser = ControlListsUser.query.filter_by(**{"control_lists_id":control_list_id,"user_id":current_user.id}).first_or_404()
Expand Down Expand Up @@ -404,4 +404,4 @@ def ignore_terms_filter(control_list_id):
return render_template_with_nav_info('control_lists/ignore_filter.html', control_list_id=control_list_id,
current_control_list=current_controlListUser)
return render_template_with_nav_info('control_lists/ignore_filter.html', control_list_id=control_list_id, current_control_list=current_controlListUser)
return render_template_with_nav_info('control_lists/ignore_filter.html', control_list_id=control_list_id, current_control_list=current_controlListUser)"""
3 changes: 1 addition & 2 deletions app/main/views/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def error():
if el != None:
filtered_filter.append(el)
filter = " ".join(filtered_filter)
print("FILTER "+str(filter))

try:
corpus: Corpus = Corpus.create(**form_kwargs)
Expand All @@ -129,14 +128,14 @@ def error():
db.session.commit()
current_controlListUser = ControlListsUser.query.filter_by(
**{"control_lists_id": corpus.control_lists_id, "user_id": current_user.id}).first_or_404()
print(current_controlListUser)
current_controlListUser.filter_punct = 'punct' in filter
current_controlListUser.filter_metadata = 'metadata' in filter
current_controlListUser.filter_numeral = 'numeral' in filter
current_controlListUser.filter_ignore = 'ignore' in filter
db.session.commit()
flash("New corpus registered", category="success")
except (sqlalchemy.exc.StatementError, sqlalchemy.exc.IntegrityError) as e:
print(e)
db.session.rollback()
flash("The corpus cannot be registered. Check your data", category="error")
flash(str(e.orig).lower())
Expand Down
3 changes: 2 additions & 1 deletion app/main/views/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ def tokens_correct_single(corpus_id, token_id):
token_id=token_id, corpus_id=corpus_id,
lemma=string_to_none(request.form.get("lemma")),
POS=string_to_none(request.form.get("POS")),
morph=string_to_none(request.form.get("morph"))
morph=string_to_none(request.form.get("morph")),
)
if "similar" in corpus.displayed_columns_by_name:
similar = {
"count": change_record.similar_remaining,
"link": url_for(".tokens_similar_to_record", corpus_id=corpus_id, record_id=change_record.id)
}
else:
Expand Down
52 changes: 30 additions & 22 deletions app/models/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ def get_unallowed(self, user_id, corpus_id, allowed_type="lemma"):
not_(allowed.exists()),
not_(custom_dict.exists())
]

current_corpus = Corpus.query.filter_by(**{"id":corpus_id}).first_or_404()
current_controlListUser = ControlListsUser.query.filter_by(
**{"control_lists_id":current_corpus.control_lists_id, "user_id": user_id}).first_or_404()
Expand Down Expand Up @@ -1096,7 +1097,7 @@ def get_like(filter_id, form, group_by, type_like="lemma", allowed_list=False):
return query

@staticmethod
def is_valid(lemma, POS, morph, corpus, user_id):
def is_valid(lemma, POS, morph, corpus, user_id, filter):
""" Check if a token is valid for a given corpus
:param lemma: Lemma value of the token to validate
Expand All @@ -1121,32 +1122,39 @@ def is_valid(lemma, POS, morph, corpus, user_id):
}

allowed_column = corpus.displayed_columns_by_name
if filter:
current_controlListUser = ControlListsUser.query.filter_by(
**{"control_lists_id": corpus.control_lists_id, "user_id": user_id}).first_or_404()
dict_filter = {'punct': current_controlListUser.filter_punct,
'metadata': current_controlListUser.filter_metadata,
'ignore': current_controlListUser.filter_ignore,
'numeral': current_controlListUser.filter_numeral}
regex_liste = []
if True in dict_filter.values():
if dict_filter['metadata']:
regex_liste.append(r'(\[[^\]]+:[^\]]*\]$)')
if dict_filter['ignore']:
regex_liste.append(r'(^\[IGNORE\])')
if dict_filter['punct']:
regex_liste.append(r"(^[^\w\s]$)")
if dict_filter['numeral']:
regex_liste.append(r'(^\d+$)')
regex = "|".join(regex_liste)

current_controlListUser = ControlListsUser.query.filter_by(
**{"control_lists_id": corpus.control_lists_id, "user_id": user_id}).first_or_404()
dict_filter = {'punct': current_controlListUser.filter_punct,
'metadata': current_controlListUser.filter_metadata,
'ignore': current_controlListUser.filter_ignore,
'numeral': current_controlListUser.filter_numeral}

regex_liste = []
if True in dict_filter.values():
if dict_filter['metadata']:
regex_liste.append(r'(\[[^\]]+:[^\]]*\]$)')
if dict_filter['ignore']:
regex_liste.append(r'(^\[IGNORE\])')
if dict_filter['punct']:
regex_liste.append(r"(^[^\w\s]$)")
if dict_filter['numeral']:
regex_liste.append(r'(^\d+$)')
regex = "|".join(regex_liste)
if lemma is not None \
and "lemma" in allowed_column \
and allowed_lemma.count() > 0 \
and corpus.get_allowed_values("lemma", label=lemma).count() == 0:
if not re.match(regex,lemma):
if filter:
if not re.match(regex, lemma):
if not corpus.has_custom_dictionary_value("lemma", lemma):
statuses["lemma"] = False
else:
if not corpus.has_custom_dictionary_value("lemma", lemma):
statuses["lemma"] = False
else:
if not corpus.has_custom_dictionary_value("lemma", lemma):
statuses["lemma"] = False

if POS is not None \
and "POS" in allowed_column \
Expand Down Expand Up @@ -1319,7 +1327,7 @@ def to_input_format(query):
return csv_file.getvalue()

@staticmethod
def update(user_id, corpus_id, token_id, lemma=None, POS=None, morph=None):
def update(user_id, corpus_id, token_id, lemma=None, POS=None, morph=None, filter=False):
""" Update a given token with lemma, POS and morph value
:param user_id: ID of the user who performs the update
Expand Down Expand Up @@ -1352,7 +1360,7 @@ def update(user_id, corpus_id, token_id, lemma=None, POS=None, morph=None):
raise error

# Check if values are correct regarding allowed values
validity = WordToken.is_valid(lemma=lemma, POS=POS, morph=morph, corpus=corpus, user_id=user_id)
validity = WordToken.is_valid(lemma=lemma, POS=POS, morph=morph, corpus=corpus, user_id=user_id, filter=filter)
if False in list(validity.values()):
error_msg = "Invalid value in {}".format(
", ".join([key for key in validity.keys() if validity[key] is False])
Expand Down
4 changes: 3 additions & 1 deletion tests/test_models/test_record.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app.models import ChangeRecord, WordToken, Corpus, ControlLists
from app.models import ChangeRecord, WordToken, Corpus, ControlLists, ControlListsUser, CorpusUser
from .base import TestModels
import copy

Expand All @@ -14,6 +14,7 @@
WordToken(corpus=1, form="Cil", lemma="cel", left_context="_", right_context="_", label_uniform="cel", morph="smn", POS="p"), # 6
WordToken(corpus=1, form="Cil", lemma="cel", left_context="_", right_context="_", label_uniform="cel", morph="smn", POS="p"), # 7
WordToken(corpus=1, form="Cil", lemma="cel", left_context="_", right_context="_", label_uniform="cel", morph="smn", POS="p"), # 8

]


Expand Down Expand Up @@ -57,6 +58,7 @@ def tok_with_id(self, list_of_tokens, _id):
def test_similar_lemma_single_change(self):
""" Ensure only similar features are fixed """
self.load_fixtures()

token, change_record = WordToken.update(
user_id=1,
token_id=1, corpus_id=1,
Expand Down
10 changes: 6 additions & 4 deletions tests/test_selenium/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def edith_nth_row_value(
if go_to_edit_token_page is None:
go_to_edit_token_page = self.go_to_edit_token_page(corpus_id)
go_to_edit_token_page()

self.driver.save_screenshot("./token_correct_1.png")
if additional_action_before is not None:
additional_action_before()

Expand All @@ -583,14 +583,16 @@ def edith_nth_row_value(
td = self.element_find_element_by_class_name(row, "token_morph")
else:
td = self.element_find_element_by_class_name(row, "token_lemma")

self.driver.save_screenshot("./token_correct_2.png")
# Click, clear the td and send a new value
td.click()
td.clear()
self.driver.save_screenshot("./token_correct_25.png")
td.send_keys(value)

self.driver.save_screenshot("./token_correct_3.png")
# Save
self.element_find_element_by_css_selector(row, "a.save").click()
self.driver.save_screenshot("./token_correct_3.png")
# It's safer to wait for the AJAX call to be completed
row = self.driver_find_element_by_id("token_" + id_row + "_row")

Expand Down Expand Up @@ -631,10 +633,10 @@ def test_edit_token(self):
self.addCorpus(with_token=True, tokens_up_to=24)
self.driver.refresh()
token, status_text, row = self.edith_nth_row_value("un", corpus_id=self.CORPUS_ID)
self.driver.save_screenshot('./token_correct_4.png')
self.assertEqual(token.lemma, "un", "Lemma should have been changed")
self.assertEqual(status_text, "Save")
self.assert_saved(row)

self.assertIn("table-changed", row.get_attribute("class"))
self.driver.refresh()
row = self.driver_find_element_by_id("token_1_row")
Expand Down
4 changes: 3 additions & 1 deletion tests/test_selenium/test_corpus_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ def test_registration(self):
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"])
self.driver_find_element_by_id("label_checkbox_create").click()
self.driver.save_screenshot("./test_registration.png")
self.driver_find_element_by_id("submit").click()

self.driver.implicitly_wait(15)
self.driver.save_screenshot("./test_registration1.png")
self.assertIn(
url_for('main.corpus_get', corpus_id=1), self.driver.current_url,
"Result page is the corpus new page"
Expand Down Expand Up @@ -197,7 +199,7 @@ def test_registration_with_allowed_morph(self):

# Checking the model
self.assertEqual(
corpus.get_unallowed(user_id=1, corpus_id=1, allowed_lemma="lemma").count(), 22,
corpus.get_unallowed(user_id=1, corpus_id=1, allowed_type="lemma").count(), 22,
"There should be 22 unallowed value as only de saint martin are allowed"
)

Expand Down

0 comments on commit b5a2355

Please sign in to comment.