Exemple #1
0
class TestReferenceTable:
    @pytest.fixture(autouse=True)
    def setup_items(self):
        self.table = ReferenceTable()

        self.author1 = Author(lastname="FirstLast",
                              firstname="FirstFirst",
                              initials="FirstInitials")

        self.author2 = Author(lastname="SecondLast",
                              firstname="SecondFirst",
                              initials="SecondInitials")

        self.pmid = "123456"
        self.pmc = "PMC12345"
        self.url = "http://www.google.com"
        self.journal = "Test journal"
        self.year = "1987"
        self.title = "Test title"
        self.doi = "Test doi"

        self.reference = Reference(pmid=self.pmid,
                                   pmc=self.pmc,
                                   doi=self.doi,
                                   url=self.url,
                                   year=self.year,
                                   title=self.title,
                                   journal=self.journal,
                                   authors=[self.author1, self.author2])

    def test_setup(self):
        assert issubclass(ReferenceTable, ElementTable)
        assert self.table.rowCount() == 0
        assert self.reference.pmid == self.pmid
        assert self.reference.pmc == self.pmc
        assert self.reference.doi == self.doi
        assert self.reference.url == self.url
        assert self.reference.year == self.year
        assert self.reference.title == self.title
        assert self.reference.journal == self.journal
        assert self.reference.authors[0] is self.author1
        assert self.reference.authors[1] is self.author2

    def test_row_from_item(self):
        return_value = self.table.row_from_item(self.reference)

        assert len(return_value) == len(self.table.header)
        for x in return_value:
            assert isinstance(x, LinkedItem)
            assert x.link is self.reference

        assert return_value[0].text() == self.reference.reference_string()
        assert return_value[1].text() == self.title
        assert return_value[2].text() == self.journal
        assert return_value[3].text() == self.year
        assert return_value[4].text() == self.pmid
        assert return_value[5].text() == self.pmc
        assert return_value[6].text() == self.doi
        assert return_value[7].text() == self.url

    def test_update_row_from_item(self):
        assert self.table.rowCount() == 0
        self.table.update_row_from_item(self.reference)
        assert self.table.rowCount() == 1

        assert self.table.item(0,
                               0).text() == self.reference.reference_string()
        assert self.table.item(0, 1).text() == self.title
        assert self.table.item(0, 2).text() == self.journal
        assert self.table.item(0, 3).text() == self.year
        assert self.table.item(0, 4).text() == self.pmid
        assert self.table.item(0, 5).text() == self.pmc
        assert self.table.item(0, 6).text() == self.doi
        assert self.table.item(0, 7).text() == self.url

    def test_get_item_from_row(self):
        self.table.update_row_from_item(self.reference)
        assert self.table.item_from_row(0) is self.reference

    def test_get_items(self):
        self.table.update_row_from_item(self.reference)
        assert self.table.get_items() == [self.reference]
class TestReference:
    @pytest.fixture(autouse=True)
    def init_objects(self):
        self.reference1 = Reference("test_abbrev", "name")
        self.reference2 = Reference("test_abbrev", "name")
        self.empty_reference = Reference()

    def test___init__(self):
        """ Test the default values of empty reference """
        assert self.empty_reference.authors == []
        assert self.empty_reference.pmid == ""
        assert self.empty_reference.pmc == ""
        assert self.empty_reference.abstract == ""
        assert self.empty_reference.doi == ""
        assert self.empty_reference.title == ""
        assert self.empty_reference.year == ""
        assert self.empty_reference.url == ""
        assert self.empty_reference.journal == ""

    def test_author_string1(self):
        """ Check that empty string is returned if no author is set """
        assert self.empty_reference.reference_string() == ""

    def test_author_string2(self):
        """ Test correct string for single author """
        self.empty_reference.authors = [Author("Family", "First", "Initials")]
        self.empty_reference.year = "2015"
        assert self.empty_reference.reference_string(
        ) == "Family Initials, 2015"

    def test_author_string3(self):
        """ Test correct string for two authors """
        self.empty_reference.authors = [
            Author("FamilyFirst", "", "InitialsFirst"),
            Author("FamilySecond", "", "InitialsSecond")
        ]

        self.empty_reference.year = "2015"
        expected = "FamilyFirst InitialsFirst and FamilySecond InitialsSecond, 2015"
        assert self.empty_reference.reference_string() == expected

    def test_author_string4(self):
        """ Test correct string for 3 authors """
        self.empty_reference.authors = [
            Author("Family1", "", "Initials1"),
            Author("Family2", "", "Initials2"),
            Author("Family3", "", "Initials3")
        ]
        self.empty_reference.year = "2015"
        expected = "Family1 Initials1 et al., 2015"
        assert self.empty_reference.reference_string() == expected

    def test_annotation_property(self):
        reference = Reference()
        reference.pmc = "1234"
        reference.pmid = "54321"
        reference.doi = "5555"

        assert Annotation("pubmed", reference.pmid) in reference.annotation
        assert Annotation("pmc", reference.pmc) in reference.annotation
        assert Annotation("doi", reference.doi) in reference.annotation