class SlrPrepATests(TestCase): """Tests for _prep_a()""" def setUp(self): self.testrepo = SQLiteRepo() self.chardict = self.testrepo.special_chars def test_prep_a_special_chars_forbidden(self): """Handle forbidden characters in relation names or anchor content. """ ins = ("".join((self.chardict["E"], x, self.chardict["WC"])) for x in self.chardict["F"]) outs = ("".join((self.chardict["E"], escape(x), self.chardict["WC"])) for x in self.chardict["F"]) for term, expected in zip(ins, outs): with self.subTest(term=term): self.assertEqual(self.testrepo._prep_a(term, wildcards=False), expected) def test_prep_a_special_characters_prefix(self): """Handle prefix characters in relation names or anchor content. """ ins = ("".join((x, self.chardict["E"], self.chardict["WC"])) for x in self.chardict["PX"]) outs = ("".join((escape(x), self.chardict["E"], self.chardict["WC"])) for x in self.chardict["PX"]) for term, expected in zip(ins, outs): with self.subTest(term=term): self.assertEqual(self.testrepo._prep_a(term, wildcards=False), expected)
class SlrPrepTermTests(TestCase): """Tests for _prep_term()""" def setUp(self): self.testrepo = SQLiteRepo() self.escape = self.testrepo.CHAR_ESCAPE self.alias = self.testrepo._char_alias def test_prep_term_alias(self): """Handle ROWID aliases""" val = 9001 term = "{}{}".format(self.alias, val) self.assertEqual(self.testrepo._prep_a(term), val) def test_prep_term_wildcards(self): """Conversion of TAGS wildcards to SQL wildcards""" args_outs = ( { 'args': { 'a': '*ananas' }, 'out': '%ananas' }, { 'args': { 'a': 'ana*nas' }, 'out': 'ana%nas' }, { 'args': { 'a': 'ananas*' }, 'out': 'ananas%' }, { 'args': { 'a': 'a??na' }, 'out': 'a__na' }, { 'args': { 'a': '100%an_a' }, 'out': '100{0}%an{0}_a'.format(self.escape) }, { 'args': { 'a': '100%an_a' }, 'out': '100{0}%an{0}_a'.format(self.escape) }, ) for a in args_outs: with self.subTest(term=a['args']): self.assertEqual(self.testrepo._prep_a(**a['args']), a['out'])
def test_not(self): """Generate SQL expression for q range in WHERE clause (q_not)""" testrep = SQLiteRepo() argtests = ( ({ 'q_eq': 5, 'q_not': True }, ' AND NOT ({} = :q_eq) '), ({ 'q_eq': 0, 'q_not': True }, ' AND NOT ({} = :q_eq) '), ) for x, y in argtests: with self.subTest(args=x): expected = y.format(testrep.COL_Q) self.assertEqual(testrep._slr_q_clause(**x), expected)
def test_reltext(self): testrep = SQLiteRepo() testdb = DB(testrep) data = [('a', None), ('z', None)] testdb.import_data(data) char_rel = testrep._char_rel char_alias = testrep._char_alias argtests = (({ 'name': 'Raz', 'a_from': 'a', 'a_to': 'z', 'alias': 'local', 'alias_fmt': 0 }, 'Raz{0}a{0}z'.format(char_rel)), ) # format: (kwargs, expected_output) for a in argtests: with self.subTest(a=a): self.assertEqual(testrep._reltext(**a[0]), a[1])
def test_get_a_special_chars_mixed(self): """Put anchor containing all special and wildcard characters""" testdb = DB(SQLiteRepo()) chardict = testdb.get_special_chars() suffix = "".join((chardict["E"], chardict["F"], chardict["WC"])) data = [("".join((x, suffix)), -10) for x in chardict["PX"]] testdb.import_data(data) for d in data: samp = list(testdb.export()) self.assertEqual(samp, data)
def test_get_a_alias(self): testdb = DB(SQLiteRepo()) data = ( ('a', 10), ('b', 11), ) testdb.import_data(data) expected = ('a', 10) # assuming ROWID strictly follows insertion order samp = next(testdb.get_a('@1', out_format='interchange')) self.assertEqual(samp, expected)
def setUp(self): # Most direct SQLiteRepo insert humanly possible self.testdb = DB(SQLiteRepo()) sc_insert = "INSERT INTO {} VALUES(?,?)".format(SQLiteRepo.TABLE_A) rt = self.testdb.repo._reltext cs = self.testdb.repo._db_conn.cursor() inp = (('a', None), ('j', None), ('t', 0.0001), ('z', -274), (rt('j', 'a', 'j'), None), (rt('t', 'a', 't'), -274), (rt('a', 'z', 'a'), 37)) cs.executemany(sc_insert, inp)
def test_get_a_exact_sql_wildcard_escape(self): """Get single anchor containing SQL wildcard characters""" testdb = DB(SQLiteRepo()) chars = (SQLiteRepo.CHAR_WC_ZP_SQL, SQLiteRepo.CHAR_WC_1C_SQL) data = [(x, 100) for x in chars] testdb.import_data(data) for c in chars: with self.subTest(char=c): sample = list( testdb.get_a("{}*".format(c), out_format='interchange')) self.assertEqual(sample, [(c, 100)])
def test_put_a_long_content(self): """Put anchors with long-form content""" testrepo = SQLiteRepo() testdb = DB(testrepo) contents = [ "{}{}".format(x, "N" * testrepo.preface_length) for x in range(3) ] data = [(x, None) for x in contents] for d in data: testdb.put_a(*d) samp = list(testdb.export()) self.assertEqual(samp, data)
def test_get_a_special_chars_prefix(self): """Get anchors containing escaped prefix special chars Only the first char needs to be escaped """ testdb = DB(SQLiteRepo()) px_chars = testdb.get_special_chars()["PX"] fi = lambda x: "{}uuu{}u".format(escape(x), x) # format input fo = lambda x: "{0}uuu{0}u".format(x) # format output data = [(fi(x), None) for x in px_chars] testdb.import_data(data) for c in px_chars: samp = next(testdb.get_a(fi(c), out_format='interchange')) expected = (fo(c), None) self.assertEqual(samp, expected)
def test_get_a_wildcard_escape(self): """Get multiple anchors containing TAGS wildcard characters using wildcards TAGS wildcards are allowed to be stored in DB """ testdb = DB(SQLiteRepo()) chars = (CHAR_WC_ZP, CHAR_WC_1C) for c in chars: data = [("{}{}".format(c, n), None) for n in range(3)] testdb.import_data(data) with self.subTest(char=c): term = "{}*".format(escape(c)) samp = list(testdb.get_a(term, out_format='interchange')) self.assertEqual(samp, data)
def test_get_a_wildcard_long_rel_in_db(self): """Get single anchor by wildcard, when long relation is in db""" testrepo = SQLiteRepo() testdb = DB(testrepo) plen = testrepo.preface_length suffix = "N" * plen long_content_a = "{}{}".format("A", suffix) long_content_z = "{}{}".format("Z", suffix) init = ((long_content_a, 0), (long_content_z, 0), ("R", long_content_a[:plen], long_content_z[:plen], None), ("RAnch", 99)) expected = [ ("RAnch", 99), ] testdb.import_data(init) samp = list(testdb.get_a("R*", out_format='interchange')) self.assertEqual(samp, expected)
def test_get_a_long_content(self): """Get Anchors with long-form content The preface of the content must be able to retrieve the Anchor """ testrepo = SQLiteRepo() testdb = DB(testrepo) contents = [ "{}{}".format(x, "N" * testrepo.preface_length) for x in range(3) ] init = [(x, 99) for x in contents] testdb.import_data(init) for c in contents: samp_preface = next(testdb.get_a(c[:-1], out_format='interchange')) expected_preface = (c[:-1], 99) samp_full = next( testdb.get_a(c[:-1], length=None, out_format='interchange')) expected_full = (c, 99) self.assertEqual(samp_preface, expected_preface) self.assertEqual(samp_full, expected_full)
def setUp(self): self.testrepo = SQLiteRepo() self.chardict = self.testrepo.special_chars
def setUp(self): self.testrepo = SQLiteRepo() self.escape = self.testrepo.CHAR_ESCAPE self.alias = self.testrepo._char_alias