def test_none(self): doc1 = Document({_ID: 'A', 'value': 'val1'}) doc2 = Document({_ID: 'A', 'value': None}) # equality with None self.assertEqual(doc2, doc2) # inequality with None self.assertGreater(doc1, doc2) self.assertLess(doc2, doc1) # inequality with None doc self.assertLess(None, doc1) self.assertLess(None, doc2)
def test_put_if_needed(self): """put_if_needed doesn't put a second time""" doc = Document({_ID: 'A', 'value': 'val1'}) # put the doc self.assertEqual(1, self.server.put_if_needed(doc)) # doc is already present self.assertEqual(0, self.server.put_if_needed(doc))
def test_copy(self): doc = Document({_ID: 'A', 'value': 'val1'}) self.server.put(doc) doc['another'] = 'foo' doc2 = self.server.get('A') self.assertTrue('another' not in doc2) self.assertTrue('another' in doc)
def test_overlapping_sync(self): """Overlapping documents from datastore""" # server makes object A v1 self.server.put(Document({_ID: 'A', 'value': 'val1'})) self.server.put(Document({_ID: 'C', 'value': 'val3'})) # client makes object B v1 self.client.put(Document({_ID: 'B', 'value': 'val2'})) self.client.put(Document({_ID: 'C', 'value': 'val4'})) # sync leaves both server and client with A val1, B val2, C val4 self.client.sync_both_directions(self.server) self.assertEqual(Document({ _ID: 'A', 'value': 'val1', _REV: 1 }), self.client.get('A')) self.assertEqual(Document({ _ID: 'B', 'value': 'val2', _REV: 1 }), self.client.get('B')) self.assertEqual(Document({ _ID: 'C', 'value': 'val4', _REV: 2 }), self.client.get('C')) self.assertEqual(Document({ _ID: 'A', 'value': 'val1', _REV: 1 }), self.server.get('A')) self.assertEqual(Document({ _ID: 'B', 'value': 'val2', _REV: 1 }), self.server.get('B')) self.assertEqual(Document({ _ID: 'C', 'value': 'val4', _REV: 2 }), self.server.get('C'))
def _some_datastore_mods(datastore, items): num_steps = random.randint(2, 30) for idx in range(num_steps): # pick item item = random.choice(items) if random.uniform(0, 1) < 0.3: datastore.delete(item) else: val = random.randint(0, 1000) datastore.put(Document({_ID: item, 'value': val}))
def test_delete(self): doc = Document({_ID: 'A', 'value': 'val1'}) self.server.put(doc) doc1 = self.server.get('A') self.assertTrue(self.server.get('A')) self.server.delete('A') doc2 = self.server.get('A') import logging logging.info("doc2 %s" % doc2) self.assertEqual(True, doc2['_deleted']) self.assertGreater(doc2[_REV], doc1[_REV])
def test_nonoverlapping(self): """Non-overlapping documents from datastore""" # server makes object A v1 self.server.put(Document({_ID: 'A', 'value': 'val1'})) # client makes object B v1 self.client.put(Document({_ID: 'B', 'value': 'val2'})) # sync leaves both server and client with A val1, B val2 self.client.sync_both_directions(self.server) self.assertEqual(Document({ _ID: 'A', 'value': 'val1', _REV: 1 }), self.client.get('A')) self.assertEqual(Document({ _ID: 'B', 'value': 'val2', _REV: 1 }), self.client.get('B')) self.assertEqual(Document({ _ID: 'A', 'value': 'val1', _REV: 1 }), self.server.get('A')) self.assertEqual(Document({ _ID: 'B', 'value': 'val2', _REV: 1 }), self.server.get('B')) # counter is at the highest existing doc version server_seq, server_docs = self.server.get_docs_since(0, 1000) self.assertEqual(self.server.sequence_id, server_seq) self.assertEqual(self.server.sequence_id, max(doc[_REV] for doc in server_docs)) client_seq, client_docs = self.client.get_docs_since(0, 1000) self.assertEqual(self.client.sequence_id, client_seq) self.assertEqual(self.client.sequence_id, max(doc[_REV] for doc in client_docs))
def main(): parser = argparse.ArgumentParser(description='Test REST server.') parser.add_argument('--server-url', '-s', dest='server_url', required=True, help='URL of the server') args = parser.parse_args() base_url = "http://" + args.server_url # server_url is a function that returns base + rest server_url = lambda url: base_url + url # Create table1 resp = requests.post(server_url('table1')) assert resp.status_code == 201 # Check for table1 resp = requests.get(server_url('table1')) assert resp.status_code == 200 ct = resp.headers['content-type'] assert ct == 'text/html; charset=utf-8', f"content type '{ct}'" assert resp.text == "" # Check for non-existent table2 resp = requests.get(server_url('table2')) assert resp.status_code == 404 # Check for docs in table1 resp = requests.get(server_url('table1/docs')) assert resp.status_code == 200 ct = resp.headers['content-type'] assert ct == 'application/json', f"content type '{ct}'" js = resp.json() assert js['documents'] == [] assert js['current_sequence_id'] == 0 # Put three docs in table1 d1 = {"_id": '1', "var1": "value1"} d2 = {"_id": '2', "var1": "value2"} d3 = {"_id": '3', "var1": "value3"} data = [d1, d2, d3] resp = requests.post(server_url('table1/docs'), json=data) assert resp.status_code == 200 ct = resp.headers['content-type'] assert ct == 'application/json', f"content type '{ct}'" js = resp.json() assert js['num_docs_put'] == 3 # Put the same three docs in table1, num_docs_put==0 resp = requests.post(server_url('table1/docs'), json=data) assert resp.status_code == 200 js = resp.json() assert js['num_docs_put'] == 0 # Check three docs in table1 resp = requests.get(server_url('table1/docs')) assert resp.status_code == 200 ct = resp.headers['content-type'] assert ct == 'application/json', f"content type '{ct}'" js = resp.json() assert len(js['documents']) == 3, f'js is {js}' assert js['current_sequence_id'] == 3, f'js is {js}' # server assigned revision numbers: d1['_rev'] = 1 d2['_rev'] = 2 d3['_rev'] = 3 docs = js['documents'] assert d1 in docs assert d2 in docs assert d3 in docs # Put docs in a local datastore ds = MemoryDatastore('datastore') d1a = {"_id": '1', "var1": "value1a"} d4 = {"_id": '4', "var1": "value4"} d5 = {"_id": '5', "var1": "value5"} for doc in [d1a, d4, d5]: ds.put(Document(doc)) # Sync local datastore with remote table1 remote_ds = RestClientSourceDatastore(base_url, 'table1') ds.sync_both_directions(remote_ds) # Check that table1 and table2 have the same things local_seq, local_docs = ds.get_docs_since(0, 10) remote_seq, remote_docs = remote_ds.get_docs_since(0, 10) assert local_seq == remote_seq assert len(local_docs) == len(remote_docs) for local_doc in local_docs: assert local_doc in remote_docs for remote_doc in remote_docs: assert remote_doc in local_docs
def test_compare(self): doc = Document({_ID: 'A', 'value': 'val1'}) self.assertEqual(doc, doc) doc2 = Document({_ID: 'A', 'value': 'val2'}) self.assertGreater(doc2, doc) self.assertLess(doc, doc2)