Skip to content

Use assertEqual etc unittest methods #2835

Closed

Description

@peterjc

See #2700 for background, cross reference #2833 (comment) e.g.

$ grep assertTrue *.py | grep "=="
common_BioSQL.py:        self.assertTrue(self.db.load(seqiter) == 1)
test_ClustalOmega_tool.py:        self.assertTrue(error.strip() == "" or
...

and:

$ grep assertFalse *.py | grep "!="
test_Restriction.py:        self.assertFalse(Acc65I != Asp718I)

Those should use assertEqual, while:

$ grep assertTrue *.py | grep "!="
test_SearchIO_model.py:        self.assertTrue(hit11.id != "alt1")
test_SearchIO_model.py:        self.assertTrue(hit11.id != "alt11")
test_SearchIO_model.py:        self.assertTrue(hit11.id != "alt1")

and:

$ grep assertFalse *.py | grep "=="
test_PDB.py:        self.assertFalse(struct[0] == struct[1])
test_PDB.py:        self.assertFalse(struct[0] == [])  # __eq__ diff. types
test_PDB.py:        self.assertFalse(struct == model)
test_PDB.py:        self.assertFalse(res1 == res2)
test_PDB_StructureAlignment.py:        self.assertFalse(al.map12 == al.map21)
test_Restriction.py:        self.assertFalse(FormattedSeq(Seq("GATC")) ==
test_Restriction.py:        self.assertFalse(FormattedSeq(Seq("TAGC")) == Seq("TAGC"))
test_Restriction.py:        self.assertFalse(Acc65I == Asp718I)

Those shoud use self.assertNotEqual

Not this cleanup work is not specific to dropping Python 3:

https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertEqual
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertNotEqual

However, some of the older usage probably did require at least Python 2.7:

https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertEqual

Activity

  1. added
    good first issue
    on Apr 11, 2020
  2. peterjc commented on Apr 11, 2020

    @peterjc
    MemberAuthor

    See also #2700 (comment) where the A500 check from flake8-assertive should catch more than my quick grep searches.

  3. trkohler commented on Apr 30, 2020

    @trkohler
    Contributor

    Hi!
    Can I work on this? I'd like to help!
    It might be my first open-source issue so if it requires any special knowledge, please let me know

  4. peterjc commented on Apr 30, 2020

    @peterjc
    MemberAuthor

    @trkohler Welcome! You should be fine with basic Python knowledge (very few of the Biopython objects do anything strange with self-defined equality rules). I suggest you pick a related batch of files, say which ones here (to reduce the chance of someone else working on the same thing), and start working. Thank you.

  5. peterjc commented on May 1, 2020

    @peterjc
    MemberAuthor

    To clarify, probably best don't touch test_Restriction.py which was done in #2833.

  6. trkohler commented on May 1, 2020

    @trkohler
    Contributor

    may I consider this one as done? Or did I miss something again? I'm sorry for my inattention, I was too excited by the fact I can do something for such a project.

  7. peterjc commented on May 8, 2020

    @peterjc
    MemberAuthor

    Using the flake8-assertive plugin, code A500 covers more than just equal and not equal, but:

    $ flake8 --select A500 .
    ...
    ./test_Pathway.py:80:9: A500 prefer assertEqual() for '==' comparisons
    ./test_Pathway.py:82:9: A500 prefer assertEqual() for '==' comparisons
    ./test_Pathway.py:160:9: A500 prefer assertEqual() for '==' comparisons
    ...
    ./test_PDB.py:682:9: A500 prefer assertEqual() for '==' comparisons
    ./common_BioSQL.py:1212:9: A500 prefer assertEqual() for '==' comparisons
    ...
    ./test_Restriction.py:233:9: A500 prefer assertNotEqual() for '==' comparisons
    ./test_Restriction.py:235:9: A500 prefer assertEqual() for '!=' comparisons
    ./test_Restriction.py:242:9: A500 prefer assertGreaterEqual() for '>=' comparisons
    ./test_Restriction.py:249:9: A500 prefer assertLessEqual() for '<=' comparisons
    ...
    

    So assuming test_Restriction.py is all fine via #2833, that leaves a few cases in test_Pathway.py, test_PDB.py, and common_BioSQL.py remaining.

  8. trkohler commented on May 8, 2020

    @trkohler
    Contributor

    cool! It means I still have some easy things to do! 👯‍♀️

  9. trkohler commented on May 8, 2020

    @trkohler
    Contributor

    Can I run somehow CI/CD checks in draft PR once again? I added commit to fix errors, but can't test locally - my machine is too weak, it lacks memory and tests run too long.

  10. peterjc commented on May 8, 2020

    @peterjc
    MemberAuthor

    Since you are only editing the tests, you only need to run that one test - e.g. python run_tests.py test_Pathway.py which should be quick.

    Or I guess you must change the GitHub pull request from draft to normal? The draft thing is quite new so we don't yet have that much experience with it.

  11. changed the title [-]Use assertEqual and assertNotEqual unittest methods[/-] [+]Use assertEqual etc unittest methods[/+] on May 9, 2020
  12. 16 remaining items

  13. sgalpha01 commented on Sep 11, 2020

    @sgalpha01
    Contributor

    Do this issue still need some work?

  14. peterjc commented on Sep 11, 2020

    @peterjc
    MemberAuthor

    Yes, there are still a few files to be fixed - using flake8-assertive v1.2.1

    $ pip install flake8-assertive
    $ flake8 --isolated --select A500 .
    ./Tests/test_NCBI_qblast.py:293:9: A500 prefer assertLessEqual() for '<=' comparisons
    ./Tests/test_NCBI_qblast.py:294:9: A500 prefer assertLessEqual() for '<=' comparisons
    ./Tests/test_NCBI_qblast.py:300:13: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_NCBI_qblast.py:320:13: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_PopGen_GenePop_EasyController.py:78:9: A500 prefer assertLess() for '<' comparisons
    ./Tests/test_PopGen_GenePop_EasyController.py:112:9: A500 prefer assertLess() for '<' comparisons
    ./Tests/test_PopGen_GenePop_EasyController.py:118:9: A500 prefer assertLess() for '<' comparisons
    ./Tests/test_PAML_tools.py:89:9: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_PAML_tools.py:110:9: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_Entrez_online.py:58:9: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_Entrez_online.py:72:9: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_Entrez_online.py:226:17: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_Restriction.py:232:9: A500 prefer assertNotEqual() for '==' comparisons
    ./Tests/test_Restriction.py:234:9: A500 prefer assertEqual() for '!=' comparisons
    ./Tests/test_Restriction.py:241:9: A500 prefer assertGreaterEqual() for '>=' comparisons
    ./Tests/test_Restriction.py:245:9: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_Restriction.py:248:9: A500 prefer assertLessEqual() for '<=' comparisons
    ./Tests/test_Restriction.py:252:9: A500 prefer assertLess() for '<' comparisons
    ./Tests/test_PDB_internal_coords.py:149:9: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_PDB_internal_coords.py:150:9: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_PDB_internal_coords.py:151:9: A500 prefer assertGreater() for '>' comparisons
    ./Tests/test_SearchIO_model.py:1232:9: A500 prefer assertEqual() for '==' comparisons
    ./Tests/test_SearchIO_model.py:1234:13: A500 prefer assertEqual() for '==' comparisons
    ./Tests/test_SearchIO_model.py:1386:9: A500 prefer assertEqual() for '==' comparisons
    ./Tests/test_SearchIO_model.py:1394:9: A500 prefer assertEqual() for '==' comparisons
    ./Tests/test_SearchIO_model.py:1395:9: A500 prefer assertEqual() for '==' comparisons
    ./Tests/test_SearchIO_model.py:1396:9: A500 prefer assertEqual() for '==' comparisons
    ./Tests/test_PDB_vectors.py:147:9: A500 prefer assertLess() for '<' comparisons
    ./Tests/test_PDB_vectors.py:155:9: A500 prefer assertLess() for '<' comparisons
    ./Tests/test_PDB_vectors.py:156:9: A500 prefer assertLess() for '<' comparisons
    ./Tests/test_PDB_vectors.py:165:9: A500 prefer assertLess() for '<' comparisons
    

    (As noted above, best avoid test_Restriction.py due to non standard object sorting behaviour)

  15. sgalpha01 commented on Sep 11, 2020

    @sgalpha01
    Contributor

    Okay, I will try my best. This is going to be my first open-source issue

  16. peterjc commented on Sep 11, 2020

    @peterjc
    MemberAuthor

    Good job @sgalpha01 !

    If you want to try test_Restriction.py as well, you'd be most welcome. However, it may much more care since the comparison methods defined are a little strange -- __ne__ is not the inverse of __eq__ for a start:

    https://github.com/biopython/biopython/blob/master/Bio/Restriction/Restriction.py#L378
    https://github.com/biopython/biopython/blob/master/Bio/Restriction/Restriction.py#L388

  17. sgalpha01 commented on Sep 12, 2020

    @sgalpha01
    Contributor

    flake8 --isolated --select A500 . >> ../TODO.txt gives the following text file:

    .\Tests\test_NCBI_qblast.py:293:9: A500 prefer assertLessEqual() for '<=' comparisons
    .\Tests\test_NCBI_qblast.py:294:9: A500 prefer assertLessEqual() for '<=' comparisons
    .\Tests\test_NCBI_qblast.py:300:13: A500 prefer assertGreater() for '>' comparisons
    .\Tests\test_NCBI_qblast.py:320:13: A500 prefer assertGreater() for '>' comparisons
    .\Tests\test_Restriction.py:232:9: A500 prefer assertNotEqual() for '==' comparisons
    .\Tests\test_Restriction.py:234:9: A500 prefer assertEqual() for '!=' comparisons
    .\Tests\test_Restriction.py:241:9: A500 prefer assertGreaterEqual() for '>=' comparisons
    .\Tests\test_Restriction.py:245:9: A500 prefer assertGreater() for '>' comparisons
    .\Tests\test_Restriction.py:248:9: A500 prefer assertLessEqual() for '<=' comparisons
    .\Tests\test_Restriction.py:252:9: A500 prefer assertLess() for '<' comparisons

    So, there are two files which needs attention.
    I started with test_NCBI_qblast.py, and made the required changes.
    But, I am not able to commit because of the following error:

    $ git commit -m "changed assertion styling for test_NCBI_qblast.py"
    Check that executables have shebangs.....................................Passed
    Check JSON...........................................(no files to check)Skipped
    Debug Statements (Python)................................................Passed
    Fix End of Files.........................................................Passed
    Mixed line ending........................................................Passed
    black....................................................................Failed
    - hook id: black
    - exit code: 1
    
    would reformat Tests\test_NCBI_qblast.py
    Oh no! \U0001f4a5 \U0001f494 \U0001f4a5
    1 file would be reformatted.
    
    flake8...................................................................Passed
    blacken-docs.............................................................Passed
    rstcheck.............................................(no files to check)Skipped
    doc8.................................................(no files to check)Skipped
    Enforce https://doi.org/ style.......................(no files to check)Skipped
    

    I am not sure what's the problem. At first, I thought it was editor specific. So, I tried Vim, but no luck.

    Update: I just needed to apply black.

  18. lacrosse91 commented on Sep 14, 2020

    @lacrosse91
    Contributor

    Hello!
    Do this issue still need some work?
    I'd like to help!

  19. removed
    good first issue
    on Sep 14, 2020
  20. peterjc commented on Sep 14, 2020

    @peterjc
    MemberAuthor

    Just test_Restriction.py left, which is somewhat unusual so I've taken off the "good first issue" tag:

    $ flake8 --isolated --select A500 .
    ./test_Restriction.py:232:9: A500 prefer assertNotEqual() for '==' comparisons
    ./test_Restriction.py:234:9: A500 prefer assertEqual() for '!=' comparisons
    ./test_Restriction.py:241:9: A500 prefer assertGreaterEqual() for '>=' comparisons
    ./test_Restriction.py:245:9: A500 prefer assertGreater() for '>' comparisons
    ./test_Restriction.py:248:9: A500 prefer assertLessEqual() for '<=' comparisons
    ./test_Restriction.py:252:9: A500 prefer assertLess() for '<' comparisons
    

    If @sgalpha01 isn't working on it, you could give it a try. It might go smoothly - just don't change the condition (e.g. don't assume a < b is the same as b > a, or not(a == b) is the same as a != b with these objects).

  21. lacrosse91 commented on Sep 14, 2020

    @lacrosse91
    Contributor

    Thanks for reply.
    I'll try it.

  22. sgalpha01 commented on Sep 14, 2020

    @sgalpha01
    Contributor

    @lacrosse91 , hey you can work on this if you want.

  23. lacrosse91 commented on Sep 14, 2020

    @lacrosse91
    Contributor

    @sgalpha01 , Thank you!
    I'm going to work on this.

  24. peterjc commented on Sep 15, 2020

    @peterjc
    MemberAuthor

    That's all done now:

    $ flake8 --isolated --select A500 .
    (no issues)
    

    Thank you everyone - a big team effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions