Ejemplo n.º 1
0
class MedlineTest(TestCase, TestMixin):
    def setUp(self):
        InitDb(URI, module=dbapi2)
        self.sess = Session()
        self.klass = Medline
        self.entity = namedtuple('Medline', 'pmid status journal pub_date created')
        self.defaults = self.entity(1, 'MEDLINE', 'journal', 'published', date.today())

    def testCreate(self):
        self.assertCreate()

    def testEquals(self):
        self.assertSame()
        self.assertDifference(pmid=2)
        self.assertDifference(status='In-Data-Review')
        self.assertDifference(journal='other')
        self.assertDifference(pub_date='other')

    def testBigPmids(self):
        big = 987654321098765432  # up to 18 decimals
        r = Medline(big, 'MEDLINE', 'journal', 'PubDate', date.today())
        self.sess.add(r)
        self.sess.commit()
        self.assertEqual(big, r.pmid)
        self.assertEqual(big, self.sess.query(Medline).first().pmid)

    # PMID

    def testRequirePmid(self):
        self.assertNonNullValue(TypeError, 'pmid')

    def testRequirePositivePmid(self):
        self.assertPositiveValue(AssertionError, 'pmid')

    # STATUS

    def testRequireStatus(self):
        self.assertNonNullValue(AssertionError, 'status')

    def testRequireValidStatus(self):
        self.assertBadValue('invalid', AssertionError, 'status')

    def testValidStatusNames(self):
        d = date.today()

        for pmid, status in enumerate(Medline.STATES):
            self.sess.add(Medline(pmid + 1, status, 'journal', 'pubdate', d))
            self.sess.commit()

        self.assertTrue(True)

    # JOURNAL

    def testRequireJournal(self):
        self.assertNonNullValue(AssertionError, 'journal')

    def testRequireNonEmptyJournal(self):
        self.assertNonEmptyValue(AssertionError, 'journal')

    # CREATED

    def testRequireCreated(self):
        self.assertNonNullValue(AssertionError, 'created')

    # COMPLETED

    def testRequireCompletedDateOrNone(self):
        self.assertBadExtraValue(AssertionError, 'completed', '')

    # REVISED

    def testRequireRevisedDateOrNone(self):
        self.assertBadExtraValue(AssertionError, 'revised', '')

    # MODIFIED

    def testRequireModifiedDateOrNone(self):
        m = Medline(*self.defaults)
        m.modified = ''
        self.sess.add(m)
        self.assertRaises(StatementError, self.sess.commit)

    def testAutomaticModified(self):
        r = Medline(1, 'MEDLINE', 'journal', 'PubDate', date.today())
        self.sess.add(r)
        self.sess.commit()
        self.assertEqual(date.today(), r.modified)

    def testModifiedBefore(self):
        today = date.today()
        yesterday = today - timedelta(days=1)
        m1 = Medline(1, 'MEDLINE', 'Journal 1', 'PubDate', today)
        m2 = Medline(2, 'MEDLINE', 'Journal 1', 'PubDate', today)
        m1.modified = today
        m2.modified = yesterday
        self.sess.add(m1)
        self.sess.add(m2)
        self.sess.commit()
        self.assertListEqual([2], list(Medline.modifiedBefore([1, 2], date.today())))

    # METHODS

    def testToString(self):
        d = date.today()
        r = Medline(1, 'MEDLINE', 'journal\\.', 'PubDate', d)
        line = "1\tMEDLINE\tjournal\\.\tPubDate\t\\N\t\\N\t{}\t\\N\t\\N\t{}\n".format(
                d.isoformat(), d.isoformat()
        )
        self.assertEqual(line, str(r))

    def testToRepr(self):
        r = Medline(1, 'MEDLINE', 'journal', 'PubDate', date.today())
        self.assertEqual("Medline<1>", repr(r))

    def testInsert(self):
        data = {
            Medline.__tablename__: [
                dict(pmid=1, status='MEDLINE', journal='Journal', pub_date='PubDate',
                     created=date.today())
            ],
            Section.__tablename__: [
                dict(pmid=1, seq=1, name='Title', content='The title.')
            ],
            Author.__tablename__: [
                dict(pmid=1, pos=1, name='Author')
            ],
            Descriptor.__tablename__: [
                dict(pmid=1, num=1, major=True, name='descriptor')
            ],
            Qualifier.__tablename__: [
                dict(pmid=1, num=1, sub=1, major=True, name='descriptor')
            ],
            Identifier.__tablename__: [
                dict(pmid=1, namespace='ns', value='id')
            ],
            Database.__tablename__: [
                dict(pmid=1, name='name', accession='accession')
            ],
            Chemical.__tablename__: [
                dict(pmid=1, idx=1, name='name')
            ],
            Keyword.__tablename__: [
                dict(pmid=1, owner='NLM', cnt=1, major=True, value='name')
            ],
        }
        Medline.insert(data)

        for m in self.sess.query(Medline):
            self.assertEqual(1, m.pmid)

        for t in Medline.CHILDREN:
            self.assertEqual(1, list(self.sess.query(t))[0].pmid)

    def testInsertMultiple(self):
        d = date.today()
        data = {Medline.__tablename__: [
            dict(pmid=1, status='MEDLINE', journal='Journal 1', pub_date='PubDate', created=d),
            dict(pmid=2, status='MEDLINE', journal='Journal 2', pub_date='PubDate', created=d),
            dict(pmid=3, status='MEDLINE', journal='Journal 3', pub_date='PubDate', created=d)
        ]}
        Medline.insert(data)
        count = 0

        for m in self.sess.query(Medline):
            if m.pmid not in (1, 2, 3):
                self.fail(m)
            else:
                count += 1

        self.assertEqual(3, count)

    def addThree(self, d):
        self.sess.add(Medline(1, 'MEDLINE', 'Journal 1', 'PubDate', d))
        self.sess.add(Medline(2, 'MEDLINE', 'Journal 2', 'PubDate', d))
        self.sess.add(Medline(3, 'MEDLINE', 'Journal X', 'PubDate', d))
        self.sess.commit()

    def testSelect(self):
        self.addThree(date.today())
        count = 0
        for row in Medline.select([1, 2], ['journal']):
            self.assertEqual('Journal {}'.format(row['pmid']), row['journal'])
            count += 1
        self.assertEqual(2, count)

    def testSelectAll(self):
        d = date.today()
        self.addThree(d)
        count = 0
        for row in Medline.selectAll([1, 2]):
            self.assertEqual('Journal {}'.format(row['pmid']), row['journal'])
            self.assertEqual(d, row['created'])
            self.assertEqual('MEDLINE', row[1])
            count += 1
        self.assertEqual(2, count)

    def testDelete(self):
        self.addThree(date.today())
        Medline.delete([1, 2])
        count = 0
        for m in self.sess.query(Medline):
            self.assertEqual(3, m.pmid)
            count += 1
        self.assertEqual(1, count)

    def testExisting(self):
        self.addThree(date.today())
        self.assertListEqual([1, 3], list(Medline.existing([1, 3, 5])))
Ejemplo n.º 2
0
class CitationTest(TestCase, TestMixin):
    def setUp(self):
        InitDb(URI, module=dbapi2)
        self.sess = Session()
        self.klass = Citation
        self.entity = namedtuple('Citation',
                                 'pmid status title journal pub_date created')
        self.defaults = self.entity(1, 'MEDLINE', 'title', 'journal',
                                    '1990 published', date.today())

    def testCreate(self):
        self.assertCreate()

    def testEquals(self):
        self.assertSame()
        self.assertDifference(pmid=2)
        self.assertDifference(status='In-Data-Review')
        self.assertDifference(journal='other')
        self.assertDifference(pub_date='1960 other')

    def testBigPmids(self):
        big = 987654321098765432  # up to 18 decimals
        r = DefaultCitation(big)
        self.sess.add(r)
        self.sess.commit()
        self.assertEqual(big, r.pmid)
        self.assertEqual(big, self.sess.query(Citation).first().pmid)

    # PMID

    def testRequirePmid(self):
        self.assertNonNullValue(TypeError, 'pmid')

    def testRequirePositivePmid(self):
        self.assertPositiveValue(AssertionError, 'pmid')

    # STATUS

    def testRequireStatus(self):
        self.assertNonNullValue(AssertionError, 'status')

    def testRequireValidStatus(self):
        self.assertBadValue('invalid', AssertionError, 'status')

    def testValidStatusNames(self):
        for pmid, status in enumerate(Citation.STATES):
            self.sess.add(DefaultCitation(pmid + 1, status))
            self.sess.commit()

        self.assertTrue(True)

    # JOURNAL

    def testRequireJournal(self):
        self.assertNonNullValue(AssertionError, 'journal')

    def testRequireNonEmptyJournal(self):
        self.assertNonEmptyValue(AssertionError, 'journal')

    # CREATED

    def testRequireCreated(self):
        self.assertNonNullValue(AssertionError, 'created')

    # COMPLETED

    def testRequireCompletedDateOrNone(self):
        self.assertBadExtraValue(AssertionError, 'completed', '')

    # REVISED

    def testRequireRevisedDateOrNone(self):
        self.assertBadExtraValue(AssertionError, 'revised', '')

    # MODIFIED

    def testRequireModifiedDateOrNone(self):
        m = Citation(*self.defaults)
        m.modified = ''
        self.sess.add(m)
        self.assertRaises(StatementError, self.sess.commit)

    def testAutomaticModified(self):
        r = DefaultCitation()
        self.sess.add(r)
        self.sess.commit()
        self.assertEqual(date.today(), r.modified)

    def testModifiedBefore(self):
        today = date.today()
        yesterday = today - timedelta(days=1)
        m1 = DefaultCitation()
        m2 = DefaultCitation(2)
        m1.modified = today
        m2.modified = yesterday
        self.sess.add(m1)
        self.sess.add(m2)
        self.sess.commit()
        self.assertListEqual([2],
                             list(Citation.modifiedBefore([1, 2],
                                                          date.today())))

    # METHODS

    def testToString(self):
        d = date.today()
        r = Citation(1, 'MEDLINE', 'title', 'journal\\.', '1990 PubDate', d)
        line = "1\tMEDLINE\t1990\ttitle\tjournal\\\\.\t1990 PubDate\t\\N\t\\N\t{}\t\\N\t\\N\t{}\n".format(
            d.isoformat(), d.isoformat())
        self.assertEqual(line, str(r))

    def testToRepr(self):
        r = DefaultCitation(123)
        self.assertEqual("Citation<123>", repr(r))

    def testInsert(self):
        data = {
            Citation.__tablename__: [
                dict(pmid=1,
                     status='MEDLINE',
                     year=1990,
                     title='Title',
                     journal='Journal',
                     pub_date='1990 PubDate',
                     created=date.today())
            ],
            Abstract.__tablename__: [dict(pmid=1, source='NLM')],
            Section.__tablename__: [
                dict(pmid=1,
                     source='NLM',
                     seq=1,
                     name='Title',
                     content='The title.')
            ],
            Author.__tablename__: [dict(pmid=1, pos=1, name='Author')],
            Descriptor.__tablename__:
            [dict(pmid=1, num=1, major=True, name='descriptor')],
            Qualifier.__tablename__:
            [dict(pmid=1, num=1, sub=1, major=True, name='descriptor')],
            Identifier.__tablename__:
            [dict(pmid=1, namespace='ns', value='id')],
            Database.__tablename__:
            [dict(pmid=1, name='name', accession='accession')],
            Chemical.__tablename__: [dict(pmid=1, idx=1, name='name')],
            Keyword.__tablename__:
            [dict(pmid=1, owner='NLM', cnt=1, major=True, value='name')],
        }
        Citation.insert(data)

        for m in self.sess.query(Citation):
            self.assertEqual(1, m.pmid)

        for t in Citation.CHILDREN:
            self.assertEqual(1, list(self.sess.query(t))[0].pmid)

    def testInsertMultiple(self):
        d = date.today()
        data = {
            Citation.__tablename__: [
                dict(pmid=1,
                     status='MEDLINE',
                     year=1990,
                     title='Title 1',
                     journal='Journal 1',
                     pub_date='1990 PubDate',
                     created=d),
                dict(pmid=2,
                     status='MEDLINE',
                     year=1990,
                     title='Title 2',
                     journal='Journal 2',
                     pub_date='1990 PubDate',
                     created=d),
                dict(pmid=3,
                     status='MEDLINE',
                     year=1990,
                     title='Title 3',
                     journal='Journal 3',
                     pub_date='1990 PubDate',
                     created=d)
            ]
        }
        Citation.insert(data)
        count = 0

        for m in self.sess.query(Citation):
            if m.pmid not in (1, 2, 3):
                self.fail(m)
            else:
                count += 1

        self.assertEqual(3, count)

    def addThree(self, d):
        self.sess.add(DefaultCitation(1, journal='Journal 1', created=d))
        self.sess.add(DefaultCitation(2, journal='Journal 2', created=d))
        self.sess.add(DefaultCitation(3, created=d))
        self.sess.commit()

    def testSelect(self):
        self.addThree(date.today())
        count = 0
        for row in Citation.select([1, 2], ['journal']):
            self.assertEqual('Journal {}'.format(row['pmid']), row['journal'])
            count += 1
        self.assertEqual(2, count)

    def testSelectAll(self):
        d = date.today()
        self.addThree(d)
        count = 0
        for row in Citation.selectAll([1, 2]):
            self.assertEqual('Journal {}'.format(row['pmid']), row['journal'])
            self.assertEqual(d, row['created'])
            self.assertEqual('MEDLINE', row[1])
            count += 1
        self.assertEqual(2, count)

    def testDelete(self):
        self.addThree(date.today())
        Citation.delete([1, 2])
        count = 0
        for m in self.sess.query(Citation):
            self.assertEqual(3, m.pmid)
            count += 1
        self.assertEqual(1, count)

    def testExisting(self):
        self.addThree(date.today())
        self.assertListEqual([1, 3], list(Citation.existing([1, 3, 5])))
Ejemplo n.º 3
0
class AuthorTest(TestCase, TestMixin):
    def setUp(self):
        InitDb(URI, module=dbapi2)
        self.sess = Session()
        self.M = Medline(1, 'MEDLINE', 'Journal', 'PubDate', date.today())
        self.sess.add(self.M)
        self.klass = Author
        self.entity = namedtuple('Author', 'pmid pos name')
        self.defaults = self.entity(1, 1, 'last')

    def testCreate(self):
        self.assertCreate()

    def testEquals(self):
        self.assertSame()
        self.assertDifference(pos=2)
        self.assertDifference(name='other')

    # PMID

    def testRequirePmid(self):
        self.assertNonNullValue(TypeError, 'pmid')

    # POS

    def testRequirePos(self):
        self.assertNonNullValue(TypeError, 'pos')

    def testRequirePositivePos(self):
        self.assertPositiveValue(AssertionError, 'pos')

    # NAME

    def testRequireName(self):
        self.assertNonNullValue(AssertionError, 'name')

    def testRequireNonEmptyName(self):
        self.assertNonEmptyValue(AssertionError, 'name')

    # INITIALS

    def testAssignInitials(self):
        self.sess.add(Author(1, 1, 'last', initials='test'))
        self.assertEqual('test', self.sess.query(Author).first().initials)

    # FORENAME

    def testAssignFirstName(self):
        self.sess.add(Author(1, 1, 'last', forename='test'))
        self.assertEqual('test', self.sess.query(Author).first().forename)

    # SUFFIX

    def testAssignSuffix(self):
        self.sess.add(Author(1, 1, 'last', suffix='test'))
        self.assertEqual('test', self.sess.query(Author).first().suffix)

    # METHODS

    def testFullNameDefault(self):
        a = Author(1, 1, 'last', initials='init', forename='first', suffix='suffix')
        self.assertEqual('first last suffix', a.fullName())

    def testFullNameNoFirst(self):
        a = Author(1, 1, 'last', initials='init', forename='', suffix='suffix')
        self.assertEqual('init last suffix', a.fullName())

    def testFullNameNoFirstOrInitials(self):
        a = Author(1, 1, 'last', initials='', forename='', suffix='suffix')
        self.assertEqual('last suffix', a.fullName())

    def testFullNameNoSuffix(self):
        a = Author(1, 1, 'last', initials='init', forename='first', suffix='')
        self.assertEqual('first last', a.fullName())

    def testShortNameDefault(self):
        a = Author(1, 1, 'last', initials='init', forename='first', suffix='suffix')
        self.assertEqual('init last', a.shortName())

    def testShortNameNoInitials(self):
        a = Author(1, 1, 'last', initials='', forename='first second', suffix='suffix')
        self.assertEqual('fs last', a.shortName())

    def testShortNameNoFirstOrInitials(self):
        a = Author(1, 1, 'last', initials='', forename='', suffix='suffix')
        self.assertEqual('last', a.shortName())

    def testToString(self):
        self.assertEqual('1\t1\tlast\t\\N\tfirst\t\n',
                         str(Author(1, 1, 'last', forename='first', suffix='')))

    def testToRepr(self):
        self.assertEqual('Author<1:1>', repr(Author(1, 1, 'name')))

    def testMedlineRelations(self):
        a = Author(1, 1, 'last')
        self.sess.add(a)
        self.sess.commit()
        self.assertListEqual([a], self.M.authors)
        self.assertEqual(self.M, a.medline)
Ejemplo n.º 4
0
class AuthorTest(TestCase, TestMixin):
    def setUp(self):
        InitDb(URI, module=dbapi2)
        self.sess = Session()
        self.M = DefaultCitation()
        self.sess.add(self.M)
        self.klass = Author
        self.entity = namedtuple('Author', 'pmid pos name')
        self.defaults = self.entity(1, 1, 'last')

    def testCreate(self):
        self.assertCreate()

    def testEquals(self):
        self.assertSame()
        self.assertDifference(pos=2)
        self.assertDifference(name='other')

    # PMID

    def testRequirePmid(self):
        self.assertNonNullValue(TypeError, 'pmid')

    # POS

    def testRequirePos(self):
        self.assertNonNullValue(TypeError, 'pos')

    def testRequirePositivePos(self):
        self.assertPositiveValue(AssertionError, 'pos')

    # NAME

    def testRequireName(self):
        self.assertNonNullValue(AssertionError, 'name')

    def testRequireNonEmptyName(self):
        self.assertNonEmptyValue(AssertionError, 'name')

    # INITIALS

    def testAssignInitials(self):
        self.sess.add(Author(1, 1, 'last', initials='test'))
        self.assertEqual('test', self.sess.query(Author).first().initials)

    # FORENAME

    def testAssignFirstName(self):
        self.sess.add(Author(1, 1, 'last', forename='test'))
        self.assertEqual('test', self.sess.query(Author).first().forename)

    # SUFFIX

    def testAssignSuffix(self):
        self.sess.add(Author(1, 1, 'last', suffix='test'))
        self.assertEqual('test', self.sess.query(Author).first().suffix)

    # METHODS

    def testFullNameDefault(self):
        a = Author(1,
                   1,
                   'last',
                   initials='init',
                   forename='first',
                   suffix='suffix')
        self.assertEqual('first last suffix', a.fullName())

    def testFullNameNoFirst(self):
        a = Author(1, 1, 'last', initials='init', forename='', suffix='suffix')
        self.assertEqual('init last suffix', a.fullName())

    def testFullNameNoFirstOrInitials(self):
        a = Author(1, 1, 'last', initials='', forename='', suffix='suffix')
        self.assertEqual('last suffix', a.fullName())

    def testFullNameNoSuffix(self):
        a = Author(1, 1, 'last', initials='init', forename='first', suffix='')
        self.assertEqual('first last', a.fullName())

    def testShortNameDefault(self):
        a = Author(1,
                   1,
                   'last',
                   initials='init',
                   forename='first',
                   suffix='suffix')
        self.assertEqual('init last', a.shortName())

    def testShortNameNoInitials(self):
        a = Author(1,
                   1,
                   'last',
                   initials='',
                   forename='first second',
                   suffix='suffix')
        self.assertEqual('fs last', a.shortName())

    def testShortNameNoFirstOrInitials(self):
        a = Author(1, 1, 'last', initials='', forename='', suffix='suffix')
        self.assertEqual('last', a.shortName())

    def testToString(self):
        self.assertEqual(
            '1\t1\tlast\t\\N\tfirst\t\n',
            str(Author(1, 1, 'last', forename='first', suffix='')))

    def testToRepr(self):
        self.assertEqual('Author<1:1>', repr(Author(1, 1, 'name')))

    def testRelations(self):
        a = Author(1, 1, 'last')
        self.sess.add(a)
        self.sess.commit()
        self.assertListEqual([a], self.M.authors)
        self.assertEqual(self.M, a.citation)