summaryrefslogtreecommitdiff
path: root/common/src
diff options
context:
space:
mode:
authorVictor Shyba <victor.shyba@gmail.com>2015-11-03 16:53:13 -0300
committerVictor Shyba <victor.shyba@gmail.com>2015-11-03 17:02:23 -0300
commit0cf6f3efd3cc7dd3361277a8f05577e823e361e5 (patch)
tree75c8344b6f7f9e168d7d8e72d0a1a87b2f09b4ad /common/src
parent397343f890dbce9b3d6e9377b54c1f804d39479c (diff)
[refactor] remove exception based logic
Creating a database was using a unnecessary complex try/except logic. Simplifying it should make the purpose more clear.
Diffstat (limited to 'common/src')
-rw-r--r--common/src/leap/soledad/common/couch/__init__.py39
-rw-r--r--common/src/leap/soledad/common/tests/test_couch.py19
-rw-r--r--common/src/leap/soledad/common/tests/test_sync.py2
3 files changed, 30 insertions, 30 deletions
diff --git a/common/src/leap/soledad/common/couch/__init__.py b/common/src/leap/soledad/common/couch/__init__.py
index 274f59e2..bd8b08b7 100644
--- a/common/src/leap/soledad/common/couch/__init__.py
+++ b/common/src/leap/soledad/common/couch/__init__.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# __init__.py
-# Copyright (C) 2013 LEAP
+# Copyright (C) 2015 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -126,6 +126,8 @@ class CouchDatabase(object):
:return: the database instance
:rtype: SoledadBackend
+
+ :raise DatabaseDoesNotExist: Raised if database does not exist.
"""
# get database from url
m = re.match('(^https?://[^/]+)/(.+)$', url)
@@ -134,12 +136,11 @@ class CouchDatabase(object):
url = m.group(1)
dbname = m.group(2)
with couch_server(url) as server:
- try:
- server[dbname]
- except ResourceNotFound:
- if not create:
+ if dbname not in server:
+ if create:
+ server.create(dbname)
+ else:
raise DatabaseDoesNotExist()
- server.create(dbname)
db = cls(url,
dbname, ensure_ddocs=ensure_ddocs,
database_security=database_security)
@@ -151,14 +152,32 @@ class CouchDatabase(object):
self._session = Session(timeout=COUCH_TIMEOUT)
self._url = url
self._dbname = dbname
- self._database = Database(
- urljoin(url, dbname),
- self._session)
- self._database.info()
+ self._database = self.get_couch_database(url, dbname)
if ensure_ddocs:
self.ensure_ddocs_on_db()
self.ensure_security_ddoc(database_security)
+ def get_couch_database(self, url, dbname):
+ """
+ Generate a couchdb.Database instance given a url and dbname.
+
+ :param url: CouchDB's server url with credentials
+ :type url: str
+ :param dbname: Database name
+ :type dbname: str
+
+ :return: couch library database instance
+ :rtype: couchdb.Database
+
+ :raise DatabaseDoesNotExist: Raised if database does not exist.
+ """
+ try:
+ return Database(
+ urljoin(url, dbname),
+ self._session)
+ except ResourceNotFound:
+ raise DatabaseDoesNotExist()
+
def ensure_ddocs_on_db(self):
"""
Ensure that the design documents used by the backend exist on the
diff --git a/common/src/leap/soledad/common/tests/test_couch.py b/common/src/leap/soledad/common/tests/test_couch.py
index be297b84..7ba50e11 100644
--- a/common/src/leap/soledad/common/tests/test_couch.py
+++ b/common/src/leap/soledad/common/tests/test_couch.py
@@ -935,7 +935,6 @@ class SoledadBackendSyncTests(
doc1 = self.db1.create_doc_from_json(simple_doc)
doc_id = doc1.doc_id
doc1_rev = doc1.rev
- # self.db1.create_index('test-idx', 'key')
new_doc = '{"key": "altval"}'
doc2 = self.db2.create_doc_from_json(new_doc, doc_id=doc_id)
doc2_rev = doc2.rev
@@ -951,12 +950,6 @@ class SoledadBackendSyncTests(
self.assertTransactionLog([doc_id, doc_id], self.db1)
self.assertGetDoc(self.db1, doc_id, doc2_rev, new_doc, True)
self.assertGetDoc(self.db2, doc_id, doc2_rev, new_doc, False)
- # soledad doesnt support index due to encryption
- # from_idx = self.db1.get_from_index('test-idx', 'altval')[0]
- # self.assertEqual(doc2.doc_id, from_idx.doc_id)
- # self.assertEqual(doc2.rev, from_idx.rev)
- # self.assertTrue(from_idx.has_conflicts)
- # self.assertEqual([], self.db1.get_from_index('test-idx', 'value'))
def test_sync_sees_remote_delete_conflicted(self):
self.db1 = self.create_database('test1', 'source')
@@ -989,7 +982,6 @@ class SoledadBackendSyncTests(
doc = self.db1.create_doc_from_json(simple_doc)
doc_id = doc.doc_id
doc1_rev = doc.rev
- # self.db1.create_index('test-idx', 'key')
self.sync(self.db1, self.db2)
content1 = '{"key": "localval"}'
content2 = '{"key": "altval"}'
@@ -1008,22 +1000,13 @@ class SoledadBackendSyncTests(
self.sync(self.db1, self.db2, trace_hook=after_whatschanged)
self.assertEqual([True], triggered)
self.assertGetDoc(self.db1, doc_id, doc2_rev2, content2, True)
- # soledad doesnt support indexing due to encryption
- # from_idx = self.db1.get_from_index('test-idx', 'altval')[0]
- # self.assertEqual(doc.doc_id, from_idx.doc_id)
- # self.assertEqual(doc.rev, from_idx.rev)
- # self.assertTrue(from_idx.has_conflicts)
- # self.assertEqual([], self.db1.get_from_index('test-idx', 'value'))
- # self.assertEqual([], self.db1.get_from_index('test-idx', 'localval'))
def test_sync_propagates_deletes(self):
self.db1 = self.create_database('test1', 'source')
self.db2 = self.create_database('test2', 'both')
doc1 = self.db1.create_doc_from_json(simple_doc)
doc_id = doc1.doc_id
- # self.db1.create_index('test-idx', 'key')
self.sync(self.db1, self.db2)
- # self.db2.create_index('test-idx', 'key')
self.db3 = self.create_database('test3', 'target')
self.sync(self.db1, self.db3)
self.db1.delete_doc(doc1)
@@ -1039,8 +1022,6 @@ class SoledadBackendSyncTests(
self.db1, doc_id, deleted_rev, None, False)
self.assertGetDocIncludeDeleted(
self.db2, doc_id, deleted_rev, None, False)
- # self.assertEqual([], self.db1.get_from_index('test-idx', 'value'))
- # self.assertEqual([], self.db2.get_from_index('test-idx', 'value'))
self.sync(self.db2, self.db3)
self.assertLastExchangeLog(
self.db3,
diff --git a/common/src/leap/soledad/common/tests/test_sync.py b/common/src/leap/soledad/common/tests/test_sync.py
index 88443179..1041367b 100644
--- a/common/src/leap/soledad/common/tests/test_sync.py
+++ b/common/src/leap/soledad/common/tests/test_sync.py
@@ -101,7 +101,7 @@ class InterruptableSyncTestCase(
user='user-uuid', server_url=self.getURL())
# ensure remote db exists before syncing
- db = couch.SoledadBackend.open_database(
+ db = couch.CouchDatabase.open_database(
urljoin(self.couch_url, 'user-user-uuid'),
create=True,
ensure_ddocs=True)