From 070fe823d35c5c8cf72aab379727b7777948856a Mon Sep 17 00:00:00 2001 From: richard-dds Date: Thu, 16 Aug 2018 13:24:11 -0400 Subject: [PATCH] Fix bug in UII field Was displaying an empty list of UIIs as [''] in the textarea --- atst/forms/fields.py | 2 +- tests/forms/test_fields.py | 43 +++++++++++++++++++++++++++++++---- tests/forms/test_financial.py | 6 ++--- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/atst/forms/fields.py b/atst/forms/fields.py index 2c06154a..f573f179 100644 --- a/atst/forms/fields.py +++ b/atst/forms/fields.py @@ -32,6 +32,6 @@ class NewlineListField(Field): def process_formdata(self, valuelist): if valuelist: - self.data = [l.strip() for l in valuelist[0].split("\n")] + self.data = [l.strip() for l in valuelist[0].split("\n") if l] else: self.data = [] diff --git a/tests/forms/test_fields.py b/tests/forms/test_fields.py index ceece7c5..04dd88c1 100644 --- a/tests/forms/test_fields.py +++ b/tests/forms/test_fields.py @@ -1,25 +1,58 @@ import pytest from wtforms import Form import pendulum +from werkzeug.datastructures import ImmutableMultiDict -from atst.forms.fields import DateField +from atst.forms.fields import DateField, NewlineListField -class MyForm(Form): +class DateForm(Form): date = DateField() +class NewlineListForm(Form): + newline_list = NewlineListField() + + def test_date_ie_format(): - form = MyForm(data={"date": "12/24/2018"}) + form = DateForm(data={"date": "12/24/2018"}) assert form.date._value() == pendulum.date(2018, 12, 24) def test_date_sane_format(): - form = MyForm(data={"date": "2018-12-24"}) + form = DateForm(data={"date": "2018-12-24"}) assert form.date._value() == pendulum.date(2018, 12, 24) def test_date_insane_format(): - form = MyForm(data={"date": "hello"}) + form = DateForm(data={"date": "hello"}) with pytest.raises(ValueError): form.date._value() + + +@pytest.mark.parametrize("input_,expected", [ + ("", []), + ("hello", ["hello"]), + ("hello\n", ["hello"]), + ("hello\nworld", ["hello", "world"]), + ("hello\nworld\n", ["hello", "world"]) +]) +def test_newline_list_process(input_, expected): + form_data = ImmutableMultiDict({"newline_list": input_}) + form = NewlineListForm(form_data) + + assert form.validate() + assert form.data == {"newline_list": expected} + + +@pytest.mark.parametrize("input_,expected", [ + ([], ""), + (["hello"], "hello"), + (["hello", "world"], "hello\nworld") +]) +def test_newline_list_value(input_, expected): + form_data = {"newline_list": input_} + form = NewlineListForm(data=form_data) + + assert form.validate() + assert form.newline_list._value() == expected diff --git a/tests/forms/test_financial.py b/tests/forms/test_financial.py index d58aa61c..c24c742d 100644 --- a/tests/forms/test_financial.py +++ b/tests/forms/test_financial.py @@ -3,12 +3,12 @@ import pytest from atst.forms.financial import suggest_pe_id -@pytest.mark.parametrize("input,expected", [ +@pytest.mark.parametrize("input_,expected", [ ('0603502N', None), ('0603502NZ', None), ('603502N', '0603502N'), ('063502N', '0603502N'), ('63502N', '0603502N'), ]) -def test_suggest_pe_id(input, expected): - assert suggest_pe_id(input) == expected +def test_suggest_pe_id(input_, expected): + assert suggest_pe_id(input_) == expected