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 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 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 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())))
def testDoi2Pmid(self): self.sess.add(Identifier(1, 'doi', 'id')) self.sess.add(Medline(2, 'MEDLINE', 'Journal', 'PubDate', date.today())) self.sess.commit() self.assertEqual(1, Identifier.doi2pmid('id')) self.assertEqual(None, Identifier.doi2pmid('other'))
def MedlineCitation(self, element): options = {} for name, key in (('DateCompleted', 'completed'), ('DateCreated', 'created'), ('DateRevised', 'revised')): e = element.find(name) if e is not None: options[key] = ParseDate(e) created = options['created'] del options['created'] status = element.get('Status') journal = element.find('MedlineJournalInfo').find('MedlineTA').text.strip() article = element.find('Article') pub_date = self.parsePubDate(article.find('Journal/JournalIssue/PubDate')) issue = self.parseIssue(article.find('Journal/JournalIssue')) pagination = self.parsePagination(article.find('Pagination/MedlinePgn')) if issue: options['issue'] = issue if pagination: options['pagination'] = pagination return Medline(self.pmid, status, journal, pub_date, created, **options)
def testPmid2Doi(self): self.sess.add(Identifier(1, 'doi', 'id')) self.sess.add(Medline(2, 'MEDLINE', 'Journal', 'PubDate', date.today())) self.sess.commit() self.assertEqual('id', Identifier.pmid2doi(1)) self.assertEqual(None, Identifier.pmid2doi(2))
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)
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 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 = Section self.entity = namedtuple('Section', 'pmid seq name content') self.defaults = self.entity(1, 1, 'Title', 'The Title')
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 = PublicationType self.entity = namedtuple('PublicationType', 'pmid value') self.defaults = self.entity(1, 'type')
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)
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 = Chemical self.entity = namedtuple('Chemical', 'pmid idx name') self.defaults = self.entity(1, 1, 'chem')
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 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 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 = Identifier self.entity = namedtuple('Identifier', 'pmid namespace value') self.defaults = self.entity(1, 'ns', 'id')
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 = Database self.entity = namedtuple('Database', 'pmid name accession') self.defaults = self.entity(1, 'name', 'accession')
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 = Keyword self.entity = namedtuple('Keyword', 'pmid owner cnt name') self.defaults = self.entity(1, 'NASA', 1, 'keyword')
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.D = Descriptor(1, 1, 'd_name', True) self.sess.add(self.D) self.klass = Qualifier self.entity = namedtuple('Qualifier', 'pmid num sub name') self.defaults = self.entity(1, 1, 1, 'name')
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 testMapPmids2Dois(self): self.sess.add(Identifier(1, 'doi', 'id1')) self.sess.add(Medline(2, 'MEDLINE', 'Journal', 'PubDate', date.today())) self.sess.add(Identifier(2, 'doi', 'id2')) self.sess.commit() self.assertDictEqual({ 1: 'id1', 2: 'id2' }, Identifier.mapPmids2Dois([1, 2, 3])) self.assertDictEqual({}, Identifier.mapPmids2Dois([3, 4]))
def testMapDois2Pmids(self): self.sess.add(Identifier(1, 'doi', 'id1')) self.sess.add(Medline(2, 'MEDLINE', 'Journal', 'PubDate', date.today())) self.sess.add(Identifier(2, 'doi', 'id2')) self.sess.commit() self.assertDictEqual({ 'id1': 1, 'id2': 2 }, Identifier.mapDois2Pmids(['id1', 'id2', 'id3'])) self.assertDictEqual({}, Identifier.mapDois2Pmids(['id3', 'id4']))
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 testToRepr(self): r = Medline(1, 'MEDLINE', 'journal', 'PubDate', date.today()) self.assertEqual("Medline<1>", repr(r))
def testExisting(self): self.addThree(date.today()) self.assertListEqual([1, 3], list(Medline.existing([1, 3, 5])))
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 testRequireModifiedDateOrNone(self): m = Medline(*self.defaults) m.modified = '' self.sess.add(m) self.assertRaises(StatementError, self.sess.commit)
DATA = [ Section(1, 1, 'Title', 'The Title'), Section(1, 2, 'Abstract', 'The Abstract'), Descriptor(1, 1, 'd_name', True), Descriptor(1, 2, 'd_name'), Qualifier(1, 1, 1, 'q_name', True), Author(1, 1, 'first'), Author(1, 2, 'last'), Identifier(1, 'ns', 'id'), Database(1, 'name', 'accession'), PublicationType(1, 'some'), PublicationType(1, 'another'), Chemical(1, 1, 'name', 'uid'), Keyword(1, 'NOTNLM', 1, 'name', True), Medline(1, 'MEDLINE', 'journal', 'pub_date', date.today()), ] class ParserMock: def __init__(self, instances): self.instances = instances def parse(self, _): for i in self.instances: yield i class TestDump(unittest.TestCase): def setUp(self): self.out = {
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 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()