def test_database_insert(): A = Alphabet('ACGT') S = A.parse('AACT', name='foo') db = DB(':memory:', A) db.initialize() attrs = {'key': 'value'} rec = db.insert(S, source_file='source.fa', source_pos=10, attrs=attrs) assert isinstance(rec.id, int) assert rec.content_id == S.content_id assert rec.source_pos == 10 assert rec.source_file == 'source.fa' assert 'key' in rec.attrs and rec.attrs['key'] == 'value', \ 'attributes must be populated correctly' with db.connection() as conn: cursor = conn.cursor() cursor.execute('SELECT content_id FROM sequence WHERE id = ?', (rec.id,)) # NOTE for some reason if we just say next(cursor) == ... # the cursor remains open after the context is over (which should # not happen as per docs). This leads to BusyError further down. assert cursor.fetchall() == [(S.content_id,)], \ 'content identifier is properly populated' # add a second sequence T = A.parse('GCTG', name='bar') new_rec = db.insert(T) assert new_rec.id != rec.id, 'new ids are assigned to new sequences' with db.connection() as conn: cursor = conn.cursor() cursor.execute('SELECT content_id FROM sequence WHERE id = ?', (new_rec.id,)) assert next(cursor) == (T.content_id,), \ 'correct id must be populated'
def test_database_basic(): A = Alphabet('ACGT') db = DB(':memory:', A) db.initialize() db.initialize() # should be able to call it twice with db.connection() as conn: # a sequence table should be created conn.cursor().execute('SELECT * FROM sequence LIMIT 1;') with pytest.raises(AssertionError): DB('/cannot/possibly/exist/directory/', A)
def test_database_overwrite(): A = Alphabet('ACGT') S = A.parse('AACT', name='foo') db = DB(':memory:', A) db.initialize() db.insert(S, source_file='old_source.fa') db.insert(S, source_file='new_source.fa') with db.connection() as conn: cursor = conn.cursor() cursor.execute( 'SELECT source_file FROM sequence WHERE content_id = ?', (S.content_id,) ) res = [x[0] for x in cursor] assert len(res) == 1 and res[0] == 'old_source.fa', \ 'Sequences with observed content id should be ignored'