def test_same_database_can_contain_two_namespaces(self): kv1 = kv.KV(self.tmp / 'kv.sqlite') kv2 = kv.KV(self.tmp / 'kv.sqlite', table='other') kv1['a'] = 'b' kv2['a'] = 'c' self.assertEqual(kv1['a'], 'b') self.assertEqual(kv2['a'], 'c')
def test_lock_fails_if_db_already_locked(self): db_path = self.tmp / 'kv.sqlite' q1 = Queue() q2 = Queue() kv2 = kv.KV(db_path, timeout=0.1) def locker(): kv1 = kv.KV(db_path) with kv1.lock(): q1.put(None) q2.get() th = Thread(target=locker) th.start() try: q1.get() with self.assertRaises(sqlite3.OperationalError) as cm1: with kv2.lock(): pass self.assertEqual(str(cm1.exception), 'database is locked') with self.assertRaises(sqlite3.OperationalError) as cm2: kv2['a'] = 'b' self.assertEqual(str(cm2.exception), 'database is locked') finally: q2.put(None) th.join()
def __init__(self, db_path="./db/qa.db", kv_path=("./resources/kv.json", ), stopwords_path="./resources/stopwords.txt", model_path="./word2vec/word2vec_wordlevel_weibo"): self.config = {} for path in kv_path: json_data = json.load(open(path, "r", encoding="utf-8")) self.config.update(json_data) self.kv = kv.KV(self.config['qa'], self.config["standard"]) self.last_text = None self.qa = SqliteQA(db_path, kv_path[0], stopwords_path, model_path) self.gen = General(stopwords_path)
def setUp(self): self.kv = kv.KV()
def setUp(self): self.tmp = path(tempfile.mkdtemp()) self.kv_file = str(self.tmp / 'kv.sqlite') self.kv = kv.KV(self.kv_file) self.addCleanup(self.tmp.rmtree)
def test_lock_during_lock_still_saves_value(self): kv1 = kv.KV(self.tmp / 'kv.sqlite') with kv1.lock(): with kv1.lock(): kv1['a'] = 'b' self.assertEqual(kv1['a'], 'b')
def locker(): kv1 = kv.KV(db_path) with kv1.lock(): q1.put(None) q2.get()
def test_deep_structure_is_retrieved_the_same(self): value = {'a': ['b', {'c': 123}]} kv1 = kv.KV(self.tmp / 'kv.sqlite') kv1['a'] = deepcopy(value) kv2 = kv.KV(self.tmp / 'kv.sqlite') self.assertEqual(kv2['a'], value)
def test_value_saved_by_one_kv_client_is_read_by_another(self): kv1 = kv.KV(self.tmp / 'kv.sqlite') kv1['a'] = 'b' kv2 = kv.KV(self.tmp / 'kv.sqlite') self.assertEqual(kv2['a'], 'b')