Skip to content

Commit

Permalink
Changes namespacing + fix basic tests
Browse files Browse the repository at this point in the history
Refactor database
  • Loading branch information
llunn authored May 7, 2022
2 parents 342c738 + 2b18066 commit 7385ecd
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 70 deletions.
6 changes: 6 additions & 0 deletions couchapy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""Top-level package for Flask Torusoft API Core."""

from couchapy.couchdb import CouchDB
from couchapy.error import CouchError, InvalidKeysException
from couchapy.session import Session

__author__ = """Lee Lunn"""
__email__ = 'lee@torusoft.com'
__version__ = '0.0.2'
11 changes: 2 additions & 9 deletions couchapy/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import couchapy.decorators as couch
import couchapy.error
import couchapy.private.changes as _changes
import couchapy.private.design as _design
import couchapy.private.document as _docs
import couchapy.private.local as _local
Expand All @@ -17,6 +18,7 @@ def __init__(self, parent, **kwargs):
self.design = _design._DesignDocument(self)
self.local = _local._LocalDocuments(self)
self.docs = _docs._Documents(self)
self.changes = _changes._DatabaseChanges(self)

@couch.endpoint('/:db:', method='head')
def headers(self, couch_data, **kwargs):
Expand Down Expand Up @@ -150,12 +152,3 @@ def get_shard(self, couch_data, **kwargs):
@couch.endpoint('/:db:/_sync_shards', method='post')
def sync_shards(self, couch_data, **kwargs):
return couch_data

@couch.endpoint('/:db:/_changes', query_keys=couch.AllowedKeys.DATABASE__CHANGES__PARAMS)
def get_changes(self, couch_data, **kwargs):
return couch_data

# TODO: make note in this doc string about the lack of data_keys since it supports query keys as well as find data keys
@couch.endpoint('/:db:/_changes', method='post', query_keys=couch.AllowedKeys.DATABASE__CHANGES__PARAMS)
def get_filtered_changes(self, couch_data, **kwargs):
return couch_data
31 changes: 31 additions & 0 deletions couchapy/private/changes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import couchapy.decorators as couch


class _DatabaseChanges():
"""
Namespace class for design document related endpoints.
This class is not intended for direct use and should be accessed through the database attribute of a CouchDB instance.
"""
def __init__(self, parent, **kwargs):
# TODO: rename this to reference the host couch instance
self.parent = parent.parent
self._db = parent._db
self._predefined_segments = parent._predefined_segments

@couch.endpoint('/:db:/_changes', method='post',
data_keys=couch.AllowedKeys.DATABASE__CHANGES__DATA,
query_keys=couch.AllowedKeys.DATABASE__CHANGES__PARAMS)
def get_by_post(self, couch_data, **kwargs):
"""
See https://docs.couchdb.org/en/stable/api/database/bulk-api.html#post--db-_design_docs
"""
return couch_data

@couch.endpoint('/:db:/_changes', query_keys=couch.AllowedKeys.DATABASE__CHANGES__PARAMS)
def get(self, couch_data, **kwargs):
"""
See https://docs.couchdb.org/en/stable/api/ddoc/common.html#get--db-_design-ddoc
See https://docs.couchdb.org/en/stable/api/document/common.html#get--db-docid
"""
return couch_data
22 changes: 22 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[bumpversion]
current_version = 0.0.2
commit = True
tag = True

[bumpversion:file:setup.py]
search = version='{current_version}'
replace = version='{new_version}'

[bumpversion:file:couchapy/__init__.py]
search = __version__ = '{current_version}'
replace = __version__ = '{new_version}'

[bdist_wheel]
universal = 1

[flake8]
exclude = docs

[tool:pytest]
collect_ignore = ['setup.py']

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="couchapy",
version="0.0.1-alpha.2",
version="version='0.0.2'",
author="Lee Lunn",
author_email="lee.lunn@gmail.com",
description="Library for interacting with CouchDB",
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture(scope='session')
def httpserver_listen_address():
return ("127.0.0.1", 8000)
5 changes: 0 additions & 5 deletions tests/unit/test_couchdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
import time


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


@pytest.fixture(autouse=True)
def setup():
""" setup any state specific to the execution of the given module."""
Expand Down
19 changes: 11 additions & 8 deletions tests/unit/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import pytest_httpserver as test_server


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


@pytest.fixture(autouse=True)
def setup():
""" setup any state specific to the execution of the given module."""
Expand Down Expand Up @@ -39,7 +34,15 @@ def test_headers(httpserver: test_server.HTTPServer):

httpserver.expect_request("/_local", method="HEAD").respond_with_json({}, headers=expected, status=404)
response = couch.db.headers(uri_segments={'db': '_local'})
assert response == expected

assert 'Cache-Control' in response
assert response['Cache-Control'] == expected['Cache-Control']
assert 'Content-Length' in response
assert response['Content-Length'] == expected['Content-Length']
assert 'Content-Type' in response
assert response['Content-Type'] == expected['Content-Type']
assert 'Date' in response
assert 'Server' in response


def test_database_exists(httpserver: test_server.HTTPServer):
Expand Down Expand Up @@ -113,12 +116,12 @@ def test_delete(httpserver: test_server.HTTPServer):

for code in [202]:
httpserver.expect_oneshot_request("/somedb", method="DELETE").respond_with_json({}, status=code)
response = couch.server.delete_database()
response = couch.server.delete_database(uri_segments={'db': 'somedb'})
assert isinstance(response, couchapy.CouchError) is False

for code in [400, 401, 404, 500]:
httpserver.expect_oneshot_request("/somedb", method="DELETE").respond_with_json({}, status=code)
response = couch.server.delete_database()
response = couch.server.delete_database(uri_segments={'db': 'somedb'})
assert isinstance(response, couchapy.CouchError) is True


Expand Down
33 changes: 26 additions & 7 deletions tests/unit/test_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
from werkzeug.wrappers.response import Response


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


@pytest.fixture(autouse=True)
def setup():
""" setup any state specific to the execution of the given module."""
Expand Down Expand Up @@ -135,7 +130,16 @@ def test_headers(httpserver: test_server.HTTPServer):

httpserver.expect_request("/_local/_design/ddoc_name", method="HEAD").respond_with_json({}, headers=expected)
response = couch.db.design.headers(uri_segments={'db': '_local', 'ddoc': 'ddoc_name'})
assert response == expected

assert 'Cache-Control' in response
assert response['Cache-Control'] == expected['Cache-Control']
assert 'Content-Length' in response
assert response['Content-Length'] == expected['Content-Length']
assert 'Content-Type' in response
assert response['Content-Type'] == expected['Content-Type']
assert 'Date' in response
assert 'Server' in response



def test_info(httpserver: test_server.HTTPServer):
Expand Down Expand Up @@ -177,7 +181,22 @@ def test_attachment_headers(httpserver: test_server.HTTPServer):

httpserver.expect_request("/_local/_design/ddoc_name/attachment", method="HEAD").respond_with_json({}, headers=expected)
response = couch.db.design.attachment.headers(uri_segments={'db': '_local', 'ddoc': 'ddoc_name', 'attname': 'attachment'})
assert response == expected

assert 'Accept-Ranges' in response
assert response['Accept-Ranges'] == expected['Accept-Ranges']
assert 'Cache-Control' in response
assert response['Cache-Control'] == expected['Cache-Control']
assert 'Content-Encoding' in response
assert response['Content-Encoding'] == expected['Content-Encoding']
assert 'Content-Length' in response
assert response['Content-Length'] == expected['Content-Length']
assert 'Content-Type' in response
assert response['Content-Type'] == expected['Content-Type']
assert 'Date' in response
assert 'ETag' in response
assert response['ETag'] == expected['ETag']
assert 'Server' in response



def test_get_attachment(httpserver: test_server.HTTPServer):
Expand Down
32 changes: 25 additions & 7 deletions tests/unit/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
from werkzeug.wrappers.response import Response


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


@pytest.fixture(autouse=True)
def setup():
""" setup any state specific to the execution of the given module."""
Expand Down Expand Up @@ -40,7 +35,16 @@ def test_headers(httpserver: test_server.HTTPServer):

httpserver.expect_request("/_local/doc_name", method="HEAD").respond_with_json({}, headers=expected)
response = couch.db.docs.headers(uri_segments={'db': '_local', 'docid': 'doc_name'})
assert response == expected

assert 'Cache-Control' in response
assert response['Cache-Control'] == expected['Cache-Control']
assert 'Content-Length' in response
assert response['Content-Length'] == expected['Content-Length']
assert 'Content-Type' in response
assert response['Content-Type'] == expected['Content-Type']
assert 'Date' in response
assert 'Server' in response



def test_queries(httpserver: test_server.HTTPServer):
Expand Down Expand Up @@ -206,7 +210,21 @@ def test_attachment_headers(httpserver: test_server.HTTPServer):

httpserver.expect_request("/_local/doc_name/attachment", method="HEAD").respond_with_json({}, headers=expected)
response = couch.db.docs.attachment.headers(uri_segments={'db': '_local', 'docid': 'doc_name', 'attname': 'attachment'})
assert response == expected

assert 'Accept-Ranges' in response
assert response['Accept-Ranges'] == expected['Accept-Ranges']
assert 'Cache-Control' in response
assert response['Cache-Control'] == expected['Cache-Control']
assert 'Content-Encoding' in response
assert response['Content-Encoding'] == expected['Content-Encoding']
assert 'Content-Length' in response
assert response['Content-Length'] == expected['Content-Length']
assert 'Content-Type' in response
assert response['Content-Type'] == expected['Content-Type']
assert 'Date' in response
assert 'ETag' in response
assert response['ETag'] == expected['ETag']
assert 'Server' in response


def test_get_attachment(httpserver: test_server.HTTPServer):
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import pytest_httpserver as test_server


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


@pytest.fixture(autouse=True)
def setup():
""" setup any state specific to the execution of the given module."""
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/test_revisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import pytest_httpserver as test_server


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


@pytest.fixture(autouse=True)
def setup():
""" setup any state specific to the execution of the given module."""
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import pytest_httpserver as test_server


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


@pytest.fixture(autouse=True)
def setup():
""" setup any state specific to the execution of the given module."""
Expand Down
12 changes: 2 additions & 10 deletions tests/unit/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import pytest_httpserver as test_server


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


@pytest.fixture(autouse=True)
def setup():
""" setup any state specific to the execution of the given module."""
Expand Down Expand Up @@ -471,9 +466,6 @@ def test_get_node_server_stat(httpserver: test_server.HTTPServer):
response = couch.server.node_stat(uri_segments={'node_name': '_local', 'stat': 'couchdb/request_time'})
assert response == expected_json

response = couch.server.node_stat()
assert response == expected_json


def test_get_node_system_stats(httpserver: test_server.HTTPServer):
expected_json = {"uptime": 259, "memory": 1000}
Expand Down Expand Up @@ -678,10 +670,10 @@ def test_database_delete(httpserver: test_server.HTTPServer):

for code in [202]:
httpserver.expect_oneshot_request("/somedb", method="DELETE").respond_with_json({}, status=code)
response = couch.server.delete_database()
response = couch.server.delete_database(uri_segments={'db': 'somedb'})
assert isinstance(response, couchapy.CouchError) is False

for code in [400, 401, 404, 500]:
httpserver.expect_oneshot_request("/somedb", method="DELETE").respond_with_json({}, status=code)
response = couch.server.delete_database()
response = couch.server.delete_database(uri_segments={'db': 'somedb'})
assert isinstance(response, couchapy.CouchError) is True
5 changes: 0 additions & 5 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
from pytest_httpserver import HTTPServer


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


def test_authenticate(httpserver: HTTPServer):
json_response = {"ok": True, "name": "root", "roles": ["_admin"]}

Expand Down
3 changes: 0 additions & 3 deletions tests/unit/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import pytest_httpserver as test_server


@pytest.fixture
def httpserver_listen_address():
return ("127.0.0.1", 8000)


@pytest.fixture(autouse=True)
Expand Down

0 comments on commit 7385ecd

Please sign in to comment.