plumdog / flask_table

Because writing HTML is fiddly and all of your tables are basically the same
http://flask-table.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
210 stars 45 forks source link

test.DateTest and test.DatetimeTest fail when running unittest locally #110

Closed CurtLH closed 6 years ago

CurtLH commented 6 years ago

I just cloned this repo and when I ran the tests, I noticed that two of the tests are failing.

θ62° [curtis:~/github/flask_table] [py35] master 1 ± python -m unittest discover
..................................F.F...................................................
======================================================================
FAIL: test_one (tests.DateTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/curtis/github/flask_table/tests/__init__.py", line 794, in test_one
    'date_test', 'test_one', items)
  File "/home/curtis/github/flask_table/tests/__init__.py", line 90, in assert_html_equivalent_from_file
    self.assert_html_equivalent(table, html)
  File "/home/curtis/github/flask_table/tests/__init__.py", line 56, in assert_html_equivalent
    html_reduce(reference))
AssertionError: '<tab[55 chars]<tr><td>1/1/14</td></tr><tr><td></td></tr></tbody></table>' != '<tab[55 chars]<tr><td>01/01/2014</td></tr><tr><td></td></tr></tbody></table>'
- <table><thead><tr><th>Date Heading</th></tr></thead><tbody><tr><td>1/1/14</td></tr><tr><td></td></tr></tbody></table>
+ <table><thead><tr><th>Date Heading</th></tr></thead><tbody><tr><td>01/01/2014</td></tr><tr><td></td></tr></tbody></table>
?                                                                    +  +  ++

======================================================================
FAIL: test_one (tests.DatetimeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/curtis/github/flask_table/tests/__init__.py", line 818, in test_one
    'datetime_test', 'test_one', items)
  File "/home/curtis/github/flask_table/tests/__init__.py", line 90, in assert_html_equivalent_from_file
    self.assert_html_equivalent(table, html)
  File "/home/curtis/github/flask_table/tests/__init__.py", line 56, in assert_html_equivalent
    html_reduce(reference))
AssertionError: '<tab[62 chars]><td>1/1/14, 10:20 AM</td></tr><tr><td></td></[15 chars]ble>' != '<tab[62 chars]><td>01/01/2014, 10:20</td></tr><tr><td></td><[16 chars]ble>'
- <table><thead><tr><th>DateTime Heading</th></tr></thead><tbody><tr><td>1/1/14, 10:20 AM</td></tr><tr><td></td></tr></tbody></table>
?                                                                                     ---
+ <table><thead><tr><th>DateTime Heading</th></tr></thead><tbody><tr><td>01/01/2014, 10:20</td></tr><tr><td></td></tr></tbody></table>
?                                                                        +  +  ++

----------------------------------------------------------------------
Ran 88 tests in 0.082s

FAILED (failures=2)
plumdog commented 6 years ago

Interesting. This is almost certainly due to some environment variables that Babel is using to determine the locale (and therefore the correct way to format dates and datetimes).

I had hoped this was fixed by #46. Could you perhaps checkout the tests_envvars_check branch and run the tests again, then post here the DEBUG: env vars section at the top?

Ignore. See https://github.com/plumdog/flask_table/issues/110#issuecomment-410180848 below.

plumdog commented 6 years ago

Huh.

So for reasons I don't understand:

LANG=en_US.UTF-8 python -m unittest discover

fails for me while

LANG=en_US.UTF-8 python setup.py test

passes.

Does just a plain python setup.py test pass for you?

plumdog commented 6 years ago

Actually, I bet I know why this is - I bet it is because unittest is importing everything so it can discover the tests.

I added a print to the top of flask_table/__init__.py and to the top of tests/__init__.py and ran both test commands:

❯ python setup.py test 2>&1 | grep 'Importing'
Importing tests/__init__.py
Importing flask_table/__init__.py
❯ python -m unittest discover 2>&1 | grep 'Importing'
Importing flask_table/__init__.py
Importing tests/__init__.py

So discover is importing the library before it imports the tests, so the environment variables are not overridden when Babel is imported (Babel configures things at import time), and so it loads based on your local environment.

If you run python -m unittest tests, it won't look for the tests in the library code, and - I hope - the tests will pass for you. Or just run python setup.py tests.

(Assuming all of that is correct and works for you as I think it will, I'll add a bit to the documentation to explain.)

CurtLH commented 6 years ago

Thanks for looking into this @plumdog. As you assumed, using the commands you mentioned above, everything passed as expected.

θ63° [curtis:~/github/flask_table] [py35] master ± python -m unittest tests
........................................................................................
----------------------------------------------------------------------
Ran 88 tests in 0.081s

OK
plumdog commented 6 years ago

Great stuff - I'll add a bit to the readme to make this clear.