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 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 DefaultCitation(pmid=1, status='MEDLINE', title='title', journal='journal', pub_date='1990 published', created=date.today()): return Citation(pmid, status, title, journal, pub_date, created)
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 testInsertMultiple(self): d = date.today() data = {Citation.__tablename__: [ dict(pmid=1, status='MEDLINE', title='Title 1', journal='Journal 1', pub_date='PubDate', created=d), dict(pmid=2, status='MEDLINE', title='Title 2', journal='Journal 2', pub_date='PubDate', created=d), dict(pmid=3, status='MEDLINE', title='Title 3', journal='Journal 3', pub_date='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 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())))
def testInsert(self): data = { Citation.__tablename__: [ dict(pmid=1, status='MEDLINE', title='Title', journal='Journal', pub_date='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 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 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') title = article.find('ArticleTitle').text # ArticleTitle element tags should never be empty when in fact they are sometimes, # in violation of the MEDLINE DTD; For example, while PMID 22536004 has a # VernacularTitle, its ArticleTitle element is empty. if title is None and article.find('VernacularTitle') is not None: logging.info( 'PMID %d had no ArticleTitle; using VernacularTitle instead' % self.pmid) title = article.find('VernacularTitle').text if title is None: logging.warning('could not find any title for PMID %d' % self.pmid) title = 'UNKNOWN' # NB: a DB constraint ensures titles are never empty! title = title.strip() 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 Citation(self.pmid, status, title, journal, pub_date, created, **options)
DATA = [ Abstract(1, 'NLM'), Section(1, 'NLM', 1, 'Abstract', 'The Abstract 1'), Section(1, 'NLM', 2, 'Abstract', 'The Abstract 2'), 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), Citation(1, 'MEDLINE', 'title', 'journal', '1990 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 testExisting(self): self.addThree(date.today()) self.assertListEqual([1, 3], list(Citation.existing([1, 3, 5])))
def testRequireModifiedDateOrNone(self): m = Citation(*self.defaults) m.modified = '' self.sess.add(m) self.assertRaises(StatementError, self.sess.commit)
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))