Skip to content

Commit 1cf1ac6

Browse files
committed
more GRI corner-case tests
1 parent 304e391 commit 1cf1ac6

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

bindings/python/genomicsqlite.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# One-time global initialization -- load the extension shared-library
1313
_DLL = find_library("genomicsqlite")
1414
assert _DLL, "couldn't locate genomicsqlite shared-library file"
15-
# open a dummy connection to :memory: just for setting up the extension
15+
# open a dummy connection to :memory: just for setting up the extension.
1616
_MEMCONN = sqlite3.connect(":memory:")
1717
_MEMCONN.enable_load_extension(True)
1818
_MEMCONN.load_extension(_DLL)
19+
# now that it's been loaded, the extension will automatically enable itself on any new connections.
1920

2021

2122
def _execute1(conn, sql, params=None):

include/genomicsqlite.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ int GenomicSQLiteOpen(const std::string &dbfile, sqlite3 **ppDb, std::string &er
9999
* (include SQLiteCpp/SQLiteCpp.h first)
100100
*/
101101
#include <memory>
102-
std::unique_ptr<SQLite::Database> GenomicSQLiteOpen(const std::string &dbfile, int flags,
102+
std::unique_ptr<SQLite::Database> GenomicSQLiteOpen(const std::string &dbfile, int flags = 0,
103103
const std::string &config_json = "{}");
104104
#endif
105105

@@ -109,7 +109,7 @@ std::string GenomicSQLiteVacuumIntoSQL(const std::string &dbfile,
109109
std::string CreateGenomicRangeIndexSQL(const std::string &table, const std::string &rid,
110110
const std::string &beg, const std::string &end,
111111
int max_depth = -1);
112-
std::string GenomicRangeRowidsSQL(const std::string &indexed_table, sqlite3 *dbconn = nullptr,
112+
std::string GenomicRangeRowidsSQL(const std::string &indexed_table, sqlite3 *dbconn,
113113
const std::string &qrid = "?1", const std::string &qbeg = "?2",
114114
const std::string &qend = "?3");
115115

test/test_gri.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
import os
23
import sqlite3
34
import random
@@ -18,6 +19,17 @@
1819
1 + 16 + 256 + 4096 + 65536 + 1048576 + 16777216,
1920
1 + 16 + 256 + 4096 + 65336 + 1048576 + 16777216 + 268435456,
2021
]
22+
POS_OFFSETS = [
23+
0,
24+
134217728,
25+
8388608,
26+
524288,
27+
32768,
28+
2048,
29+
128,
30+
8,
31+
0,
32+
]
2133

2234

2335
def test_genomic_range_bin():
@@ -191,6 +203,39 @@ def fanout(query):
191203
assert fanout(query) == 9
192204

193205

206+
def test_boundaries():
207+
# test abutting intervals near bin boundaries
208+
209+
con = sqlite3.connect(":memory:")
210+
con.executescript("CREATE TABLE features(rid INTEGER, beg INTEGER, end INTEGER)")
211+
212+
insert = "INSERT INTO features(rid,beg,end) values(?,?,?)"
213+
for depth in range(2, 9):
214+
boundary = 3 * (16 ** (9 - depth)) + POS_OFFSETS[depth]
215+
for ofs in range(-2, 3):
216+
featlen = math.ceil(16 ** (9 - depth) / 2)
217+
con.execute(insert, (42, boundary + ofs - featlen, boundary + ofs))
218+
con.execute(insert, (42, boundary + ofs, boundary + ofs + featlen))
219+
220+
con.executescript(
221+
genomicsqlite.create_genomic_range_index_sql(con, "features", "rid", "beg", "end")
222+
)
223+
224+
query = genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1]
225+
control = "SELECT _rowid_ FROM features NOT INDEXED WHERE rid = ? AND NOT (? > end OR ? < beg) ORDER BY _rowid_"
226+
total_results = 0
227+
for depth in range(2, 9):
228+
boundary = 3 * (16 ** (9 - depth)) + POS_OFFSETS[depth]
229+
for qlen in range(3):
230+
for ofs in range(-3, 4):
231+
tup = (42, boundary + ofs, boundary + ofs + qlen)
232+
query_results = list(con.execute(query, tup))
233+
control_results = list(con.execute(control, tup))
234+
assert query_results == control_results
235+
total_results += len(query_results)
236+
assert total_results == 938
237+
238+
194239
def test_refseq():
195240
con = sqlite3.connect(":memory:")
196241

0 commit comments

Comments
 (0)