Gitlab Community Edition Instance

Commit e1274821 authored by xrotwang's avatar xrotwang Committed by Marcel Hellkamp
Browse files

Added more tests

parent e22c57a9
......@@ -10,3 +10,5 @@ MANIFEST
.idea/
.pytest_cache/
.tox/
.coverage
......@@ -2,5 +2,14 @@
markers =
with_server: Requires a CDStar server
addopts =
--cov
-m "not with_server"
[coverage:run]
source =
pycdstar3
tests
[coverage:report]
show_missing = true
from pycdstar3.client import * # noqa: F401, F403
__version__ = "3.0.dev0"
......@@ -2,9 +2,9 @@ import sys
import pycdstar3.cli
def main():
def main(): # pragma: no cover
return pycdstar3.cli.main(*sys.argv[1:])
if __name__ == '__main__':
if __name__ == '__main__': # pragma: no cover
sys.exit(main() or 0)
......@@ -8,7 +8,7 @@ __all__ = ("CDStar", "FormUpdate", "ApiError")
PATH_TYPES = (str,)
if sys.version_info >= (3, 6):
PATH_TYPES = (str, os.PathLike)
PATH_TYPES = (str, os.PathLike) # pragma: no cover
class CDStar:
......@@ -125,7 +125,7 @@ class CDStar:
>>> api.GET(vault, archive, params=[('with', 'meta'), ('with', 'files')])
"""
kw.setdefault('_expect_status', 200)
kw.setdefault('_expect_status', [200])
return self._rest('GET', *path, **kw).json()
def service_info(self):
......
......@@ -6,9 +6,20 @@ from pycdstar3.client import CDStar
@pytest.fixture
def mockserver(mocker):
class MockSession(mocker.Mock):
callstack = []
def request(self, method, *args, **kw):
path = args[0]
self.callstack.append((method, path))
if method == 'GET' and path == '/_raise':
return mocker.Mock(
ok=False,
status_code=500,
json=mocker.Mock(return_value={
'error': 'e', 'message': 'm', 'status': 's', 'detail': [('x', 'y')]}))
json = {}
if method == 'GET' and args == ('/',):
if method == 'GET' and path == '/':
json = {
'vaults': ['demo'],
'version': {
......@@ -22,9 +33,29 @@ def mockserver(mocker):
'cdstar': '3.0.0-SNAPSHOT'
}
}
elif method == 'GET' and len(args) == 1:
elif method == 'GET' and path == '/x':
json = {'public': True}
elif method == 'GET' and path == '/_tx/tx':
json = {
'isolation': 'snapshot',
'timeout': 60,
'ttl': 60,
'readonly': False,
'id': 'tx'
}
elif method == 'POST' and path == '/_tx':
json = {
'isolation': 'snapshot',
'timeout': 60,
'ttl': 60,
'readonly': False,
'id': 'tx'
}
elif method == 'POST' and path == '/_tx/deltx':
raise ValueError()
elif method == 'POST' and path == '/demo':
# create an archive:
json = {'id': 'archive', 'vault': 'demo', 'revision': '0'}
return mocker.Mock(json=mocker.Mock(return_value=json))
return CDStar('', _session=MockSession())
return CDStar('', _session=MockSession(), auth=('test', 'test'))
......@@ -10,21 +10,21 @@ import os
import pytest
from pycdstar3.client import CDStar
from pycdstar3.client import CDStar, ApiError, FormUpdate
SERVER = os.environ.get("TEST_CDSTAR", "http://127.0.0.1:8080/v3/")
VAULT = os.environ.get("TEST_VAULT", "test")
@pytest.mark.with_server
def test_service_info():
def test_service_info(): # pragma: no cover
c = CDStar(SERVER)
si = c.service_info()
assert si['version']['api'] == "3.0"
@pytest.mark.with_server
def test_tx():
def test_tx(): # pragma: no cover
c = CDStar(SERVER)
with c.begin():
assert 'id' in c.tx
......@@ -35,7 +35,7 @@ def test_tx():
@pytest.mark.with_server
def test_tx_commit():
def test_tx_commit(): # pragma: no cover
c = CDStar(SERVER)
with c.begin():
c.rollback()
......@@ -44,14 +44,14 @@ def test_tx_commit():
@pytest.mark.with_server
def test_vault_info():
def test_vault_info(): # pragma: no cover
c = CDStar(SERVER)
vi = c.vault_info(VAULT)
assert 'public' in vi
@pytest.mark.with_server
def test_crud_file():
def test_crud_file(): # pragma: no cover
c = CDStar(SERVER)
with c.begin():
......@@ -71,6 +71,11 @@ def test_crud_file():
assert "text/x-python" == fget.headers['Content-Type']
# no commit
# Mockserver tests:
def test_clone(mockserver):
assert mockserver.clone().auth
def test_service_info2(mockserver):
si = mockserver.service_info()
......@@ -81,3 +86,57 @@ def test_service_info2(mockserver):
def test_vault_info2(mockserver):
vi = mockserver.vault_info('x')
assert 'public' in vi
def test_tx2(mockserver):
with pytest.raises(RuntimeError):
# Can't commit without transaction!
mockserver.commit()
with pytest.raises(RuntimeError):
# Cant't call keepalive without transaction!
mockserver.keepalive()
with mockserver.begin():
assert 'id' in mockserver.tx
mockserver.service_info()
mockserver.keepalive()
mockserver.commit()
assert mockserver.tx is None
with mockserver.begin():
# tweak the transaction id to trigger Exception:
mockserver._tx['id'] = 'deltx'
with pytest.raises(ValueError):
mockserver.commit()
with mockserver.begin():
mockserver.rollback()
assert mockserver.tx is None
with mockserver.begin():
pass
assert mockserver._session.callstack[-1] == ('DELETE', '/_tx/tx')
with mockserver.begin(autocommit=True):
pass
assert mockserver._session.callstack[-1] == ('POST', '/_tx/tx')
with pytest.raises(RuntimeError):
with mockserver:
pass
def test_error(mockserver):
with pytest.raises(ApiError) as e:
mockserver.GET('_raise')
msg = e.value.pretty()
assert 'e (s)' in msg
def test_archive(mockserver):
with mockserver.begin():
mockserver.create_archive('demo')
with mockserver.begin():
mockserver.create_archive('demo', form=FormUpdate())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment