Skip to content
Snippets Groups Projects
Commit 70d2cf53 authored by Nathaniel Kofalt's avatar Nathaniel Kofalt
Browse files

Add some basic tests

parent ba0f2a2b
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@ echo "Checking for files with windows-style newlines:"
echo "Running pylint ..."
# TODO: Enable Refactor and Convention reports
pylint --reports=no --disable=C,R api
pylint --reports=no --disable=C,R,W0312 api
#echo
#
......
# New test fixtures!
#
# The intention is to slowly build these up until we like them, then port the tests to them and replace parts of conftest.py
import pytest
import requests
# The request Session object has no support for a base URL; this is a subclass to embed one.
# Dynamically generated via a pytest fixture so that we can use the upstream fixtures.
@pytest.fixture(scope="module")
def base_url_session(base_url):
class BaseUrlSession(requests.Session):
def request(self, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None):
url = base_url + url
return super(BaseUrlSession, self).request(method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
return BaseUrlSession
_apiAsAdmin = None
# A replacement for the RequestsAccessor class. Less boilerplate, no kwarg fiddling.
# This has the added benefit of re-using HTTP connections, which might speed up our testing considerably.
#
# Ref: http://stackoverflow.com/a/34491383
@pytest.fixture(scope="module")
def as_admin(base_url_session):
global _apiAsAdmin
# Create one session and reuse it.
if _apiAsAdmin is None:
s = base_url_session()
s.headers.update({
"Authorization":"scitran-user XZpXI40Uk85eozjQkU1zHJ6yZHpix+j0mo1TMeGZ4dPzIqVPVGPmyfeK"
})
s.params.update({
"root": "true"
})
_apiAsAdmin = s
return _apiAsAdmin
......@@ -7,6 +7,14 @@ import pymongo
import requests
# Pytest considers fixtures to be provided by "plugins", which are generally provided by
# files called conftest.py. This prevents us from placing module-level fixture logic in
# well-organized files. To fix this, we simply star-import from files that we need.
#
# Ref: http://pytest.org/2.2.4/plugins.html
from basics import *
from states import *
@pytest.fixture(scope="session")
def bunch():
class BunchFactory:
......
# Various wholesale app states that might be useful in your tests
import time
import pytest
# Currently a dupe from test_uploads.py.
# Could not understand why this doesn't work if I remove the original; future work needed here.
@pytest.fixture(scope="module")
def with_hierarchy_and_file_data(api_as_admin, bunch, request, data_builder):
group = data_builder.create_group('test_upload_' + str(int(time.time() * 1000)))
project = data_builder.create_project(group)
session = data_builder.create_session(project)
acquisition = data_builder.create_acquisition(session)
file_names = ['one.csv', 'two.csv']
files = {}
for i, name in enumerate(file_names):
files['file' + str(i+1)] = (name, 'some,data,to,send\nanother,row,to,send\n')
def teardown_db():
data_builder.delete_acquisition(acquisition)
data_builder.delete_session(session)
data_builder.delete_project(project)
data_builder.delete_group(group)
request.addfinalizer(teardown_db)
fixture_data = bunch.create()
fixture_data.group = group
fixture_data.project = project
fixture_data.session = session
fixture_data.acquisition = acquisition
fixture_data.files = files
return fixture_data
import json
import logging
log = logging.getLogger(__name__)
sh = logging.StreamHandler()
log.addHandler(sh)
def test_resolver_root(as_admin, with_hierarchy_and_file_data):
r = as_admin.post('/resolve', json={'path': []})
assert r.ok
result = r.json()
path = result['path']
children = result['children']
# root node should not walk
assert len(path) == 0
# should be 3 groups
assert len(children) == 3
for node in children:
assert node['node_type'] == 'group'
def test_resolver_group(as_admin, with_hierarchy_and_file_data):
r = as_admin.post('/resolve', json={'path': [ 'scitran' ]})
assert r.ok
result = r.json()
path = result['path']
children = result['children']
# group node is one down from root
assert len(path) == 1
# should be no children
assert len(children) == 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment