Ejemplo n.º 1
0
 def test__query_init_two_fields(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     self.db.create_index("two-idx", "key", "key2")
     query = self.db._query_init("two-idx")
     self.assertEqual("two-idx", query.index_name)
     self.assertEqual(2, query.num_fields)
     self.assertEqual(["key", "key2"], query.fields)
Ejemplo n.º 2
0
 def test__get_generation_info(self):
     db = c_backend_wrapper.CDatabase(':memory:')
     self.assertEqual((0, ''), db._get_generation_info())
     db.create_doc_from_json(tests.simple_doc)
     info = db._get_generation_info()
     self.assertEqual(1, info[0])
     self.assertTrue(info[1].startswith('T-'))
Ejemplo n.º 3
0
 def test_create_index_after_deleting_document(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     doc = self.db.create_doc_from_json(tests.simple_doc)
     doc2 = self.db.create_doc_from_json(tests.simple_doc)
     self.db.delete_doc(doc2)
     self.db.create_index_list('test-idx', ['key'])
     self.assertEqual([doc], self.db.get_from_index('test-idx', 'value'))
Ejemplo n.º 4
0
 def test_get_conflicts_with_borked_data(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     # We add an entry to conflicts, but not to documents, which is an
     # invalid situation
     self.db._run_sql("INSERT INTO conflicts"
                      " VALUES ('doc-id', 'doc-rev', '{}')")
     self.assertRaises(Exception, self.db.get_doc_conflicts, 'doc-id')
Ejemplo n.º 5
0
 def test_get_from_index_list(self):
     # We manually poke data into the DB, so that we test just the "get_doc"
     # code, rather than also testing the index management code.
     self.db = c_backend_wrapper.CDatabase(':memory:')
     doc = self.db.create_doc_from_json(tests.simple_doc)
     self.db.create_index("key-idx", "key")
     docs = self.db.get_from_index_list('key-idx', ['value'])
     self.assertEqual([doc], docs)
Ejemplo n.º 6
0
 def test_get_from_index_list_multi(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     content = '{"key": "value", "key2": "value2"}'
     doc = self.db.create_doc_from_json(content)
     self.db.create_index('test-idx', 'key', 'key2')
     self.assertEqual([doc],
                      self.db.get_from_index_list('test-idx',
                                                  ['value', 'value2']))
Ejemplo n.º 7
0
 def test__set_replica_uid(self):
     db = c_backend_wrapper.CDatabase(':memory:')
     self.assertIsNot(None, db._replica_uid)
     db._set_replica_uid('foo')
     self.assertEqual(
         [('foo', )],
         db._run_sql(
             "SELECT value FROM u1db_config WHERE name='replica_uid'"))
Ejemplo n.º 8
0
 def test_get_from_index_list_multi_ordered(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     doc1 = self.db.create_doc('{"key": "value3", "key2": "value4"}')
     doc2 = self.db.create_doc('{"key": "value2", "key2": "value3"}')
     doc3 = self.db.create_doc('{"key": "value2", "key2": "value2"}')
     doc4 = self.db.create_doc('{"key": "value1", "key2": "value1"}')
     self.db.create_index('test-idx', 'key', 'key2')
     self.assertEqual([doc4, doc3, doc2, doc1],
                      self.db.get_from_index_list('test-idx', ['v*', '*']))
Ejemplo n.º 9
0
 def test_trivial_sync(self):
     mem_db = self.request_state._create_database('test.db')
     mem_doc = mem_db.create_doc_from_json(tests.nested_doc)
     url = self.getURL('test.db')
     target = c_backend_wrapper.create_http_sync_target(url)
     db = c_backend_wrapper.CDatabase(':memory:')
     doc = db.create_doc_from_json(tests.simple_doc)
     c_backend_wrapper.sync_db_to_target(db, target)
     self.assertGetDoc(mem_db, doc.doc_id, doc.rev, doc.get_json(), False)
     self.assertGetDoc(db, mem_doc.doc_id, mem_doc.rev, mem_doc.get_json(),
                       False)
Ejemplo n.º 10
0
 def test_db_sync(self):
     mem_db = self.request_state._create_database('test.db')
     mem_doc = mem_db.create_doc_from_json(tests.nested_doc)
     url = self.getURL('test.db')
     db = c_backend_wrapper.CDatabase(':memory:')
     doc = db.create_doc_from_json(tests.simple_doc)
     local_gen_before_sync = db.sync(url)
     gen, _, changes = db.whats_changed(local_gen_before_sync)
     self.assertEqual(1, len(changes))
     self.assertEqual(mem_doc.doc_id, changes[0][0])
     self.assertEqual(1, gen - local_gen_before_sync)
     self.assertEqual(1, local_gen_before_sync)
     self.assertGetDoc(mem_db, doc.doc_id, doc.rev, doc.get_json(), False)
     self.assertGetDoc(db, mem_doc.doc_id, mem_doc.rev, mem_doc.get_json(),
                       False)
Ejemplo n.º 11
0
    def test_unavailable(self):
        mem_db = self.request_state._create_database('test.db')
        mem_db.create_doc_from_json(tests.nested_doc)
        tries = []

        def wrapper(instance, *args, **kwargs):
            tries.append(None)
            raise errors.Unavailable

        mem_db.whats_changed = wrapper
        url = self.getURL('test.db')
        target = c_backend_wrapper.create_http_sync_target(url)
        db = c_backend_wrapper.CDatabase(':memory:')
        db.create_doc_from_json(tests.simple_doc)
        self.assertRaises(errors.Unavailable,
                          c_backend_wrapper.sync_db_to_target, db, target)
        self.assertEqual(5, len(tries))
Ejemplo n.º 12
0
    def test_sync_exchange_doc_ids(self):
        doc1 = self.db.create_doc_from_json(tests.simple_doc, doc_id='doc-1')
        db2 = c_backend_wrapper.CDatabase(':memory:')
        doc2 = db2.create_doc_from_json(tests.nested_doc, doc_id='doc-2')
        returned = []

        def return_doc_cb(doc, gen, trans_id):
            returned.append((doc, gen, trans_id))

        val = self.st.sync_exchange_doc_ids(db2, [(doc2.doc_id, 1, 'T-sid')],
                                            0, None, return_doc_cb)
        last_trans_id = self.db._get_transaction_log()[-1][1]
        self.assertEqual(2, self.db._get_generation())
        self.assertEqual((2, last_trans_id), val)
        self.assertGetDoc(self.db, doc2.doc_id, doc2.rev, tests.nested_doc,
                          False)
        self.assertEqual((doc1, 1), returned[0][:-1])
Ejemplo n.º 13
0
    def test_unavailable_then_available(self):
        mem_db = self.request_state._create_database('test.db')
        mem_doc = mem_db.create_doc_from_json(tests.nested_doc)
        orig_whatschanged = mem_db.whats_changed
        tries = []

        def wrapper(instance, *args, **kwargs):
            if len(tries) < 1:
                tries.append(None)
                raise errors.Unavailable
            return orig_whatschanged(instance, *args, **kwargs)

        mem_db.whats_changed = wrapper
        url = self.getURL('test.db')
        target = c_backend_wrapper.create_http_sync_target(url)
        db = c_backend_wrapper.CDatabase(':memory:')
        doc = db.create_doc_from_json(tests.simple_doc)
        c_backend_wrapper.sync_db_to_target(db, target)
        self.assertEqual(1, len(tries))
        self.assertGetDoc(mem_db, doc.doc_id, doc.rev, doc.get_json(), False)
        self.assertGetDoc(db, mem_doc.doc_id, mem_doc.rev, mem_doc.get_json(),
                          False)
Ejemplo n.º 14
0
 def test_create_index_list_on_non_ascii_field_name(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     doc = self.db.create_doc_from_json(json.dumps({u'\xe5': 'value'}))
     self.db.create_index_list('test-idx', [u'\xe5'])
     self.assertEqual([doc], self.db.get_from_index('test-idx', 'value'))
Ejemplo n.º 15
0
 def test_list_indexes_with_non_ascii_field_names(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     self.db.create_index_list('test-idx', [u'\xe5'])
     self.assertEqual([('test-idx', [u'\xe5'])], self.db.list_indexes())
Ejemplo n.º 16
0
 def test__get_generation(self):
     db = c_backend_wrapper.CDatabase(':memory:')
     self.assertEqual(0, db._get_generation())
     db.create_doc_from_json(tests.simple_doc)
     self.assertEqual(1, db._get_generation())
Ejemplo n.º 17
0
 def test__run_sql(self):
     db = c_backend_wrapper.CDatabase(':memory:')
     self.assertTrue(db._sql_is_open())
     self.assertEqual([], db._run_sql('CREATE TABLE test (id INTEGER)'))
     self.assertEqual([], db._run_sql('INSERT INTO test VALUES (1)'))
     self.assertEqual([('1', )], db._run_sql('SELECT * FROM test'))
Ejemplo n.º 18
0
 def test__is_closed(self):
     db = c_backend_wrapper.CDatabase(':memory:')
     self.assertTrue(db._sql_is_open())
     db.close()
     self.assertFalse(db._sql_is_open())
Ejemplo n.º 19
0
 def test_wildcard_matches_unicode_value(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     doc = self.db.create_doc_from_json(json.dumps({"key": u"valu\xe5"}))
     self.db.create_index_list('test-idx', ['key'])
     self.assertEqual([doc], self.db.get_from_index('test-idx', '*'))
Ejemplo n.º 20
0
 def test_exists(self):
     if c_backend_wrapper is None:
         self.fail("Could not import the c_backend_wrapper module."
                   " Was it compiled properly?")
     db = c_backend_wrapper.CDatabase(':memory:')
     self.assertEqual(':memory:', db._filename)
Ejemplo n.º 21
0
 def test_create_index_fails_if_name_taken(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     self.db.create_index_list('test-idx', ['key'])
     self.assertRaises(errors.IndexNameTakenError,
                       self.db.create_index_list, 'test-idx', ['stuff'])
Ejemplo n.º 22
0
 def test_default_replica_uid(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     self.assertIsNot(None, self.db._replica_uid)
     self.assertEqual(32, len(self.db._replica_uid))
     # casting to an int from the uid *is* the check for correct behavior.
     int(self.db._replica_uid, 16)
Ejemplo n.º 23
0
 def test_format_refuses_non_http(self):
     db = c_backend_wrapper.CDatabase(':memory:')
     target = db.get_sync_target()
     self.assertRaises(RuntimeError, c_backend_wrapper._format_sync_url,
                       target, 'replica,uid')
Ejemplo n.º 24
0
def create_c_database(test, replica_uid):
    if c_backend_wrapper is None:
        test.skipTest('c_backend_wrapper is not available')
    db = c_backend_wrapper.CDatabase(':memory:')
    db._set_replica_uid(replica_uid)
    return db
Ejemplo n.º 25
0
 def test_create_index_evaluates_it(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     doc = self.db.create_doc_from_json(tests.simple_doc)
     self.db.create_index_list('test-idx', ['key'])
     self.assertEqual([doc], self.db.get_from_index('test-idx', 'value'))
Ejemplo n.º 26
0
 def test_get_index_keys(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     self.db.create_doc_from_json(tests.simple_doc)
     self.db.create_index("key-idx", "key")
     keys = self.db.get_index_keys('key-idx')
     self.assertEqual([("value", )], keys)
Ejemplo n.º 27
0
 def test_get_from_index_2(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     doc = self.db.create_doc_from_json(tests.nested_doc)
     self.db.create_index("multi-idx", "key", "sub.doc")
     docs = self.db.get_from_index('multi-idx', 'value', 'underneath')
     self.assertEqual([doc], docs)
Ejemplo n.º 28
0
 def setUp(self):
     super(TestCSyncTarget, self).setUp()
     self.db = c_backend_wrapper.CDatabase(':memory:')
     self.st = self.db.get_sync_target()
Ejemplo n.º 29
0
 def test_create_index_does_not_fail_if_name_taken_with_same_index(self):
     self.db = c_backend_wrapper.CDatabase(':memory:')
     self.db.create_index_list('test-idx', ['key'])
     self.db.create_index_list('test-idx', ['key'])
     self.assertEqual([('test-idx', ['key'])], self.db.list_indexes())