-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from JunAishima/use-pytest-for-testing
Use pytest for testing
- Loading branch information
Showing
7 changed files
with
173 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ jobs: | |
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.7", "3.8", "3.9"] | ||
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"] | ||
dependencies: ["pip", "conda"] | ||
fail-fast: false | ||
|
||
|
@@ -29,9 +29,6 @@ jobs: | |
- name: Checkout the code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Start MongoDB | ||
uses: supercharge/[email protected] | ||
|
||
- name: Set up Python ${{ matrix.python-version }} with conda | ||
uses: conda-incubator/setup-miniconda@v2 | ||
with: | ||
|
@@ -56,7 +53,7 @@ jobs: | |
if: matrix.dependencies == 'conda' | ||
run: | | ||
set -vxeo pipefail | ||
conda install -y -c conda-forge six mongoquery doct jsonschema mock pymongo pytest pyyaml requests tornado ujson | ||
conda install -y -c conda-forge six mongoquery doct jsonschema mock mongomock pymongo pytest pyyaml requests tornado ujson | ||
- name: Install the package | ||
run: | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import time as ttime | ||
import uuid | ||
|
||
import pytest | ||
import subprocess | ||
import sys | ||
import uuid | ||
from analysisstore.client.commands import AnalysisClient | ||
import contextlib | ||
|
||
testing_config = dict( | ||
host="localhost", | ||
port=7601, | ||
timezone="US/Eastern", | ||
service_port=7601, | ||
database="astoretest{0}".format(str(uuid.uuid4())), | ||
mongo_uri="mongodb://localhost", | ||
mongo_host="localhost", | ||
mongo_port=27017, | ||
testing=True, | ||
log_file_prefix="testing", | ||
) | ||
|
||
|
||
@contextlib.contextmanager | ||
def astore_startup(): | ||
ps = subprocess.Popen( | ||
[ | ||
sys.executable, | ||
"-c", | ||
f"from analysisstore.ignition import start_server; start_server(config={testing_config}) ", | ||
], | ||
) | ||
ttime.sleep(1.3) # make sure the process is started | ||
yield ps | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def astore_server(): | ||
with astore_startup() as astore_fixture: | ||
yield | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def astore_client(): | ||
c = AnalysisClient( | ||
{"host": testing_config["mongo_host"], "port": testing_config["service_port"]} | ||
) | ||
return c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,101 @@ | ||
from ..client.commands import AnalysisClient | ||
from .testing import TESTING_CONFIG | ||
from .conftest import testing_config | ||
import pytest | ||
from .testing import astore_setup, astore_teardown | ||
import time | ||
import requests | ||
from doct import Document | ||
import uuid | ||
|
||
|
||
class TestConnClient: | ||
def setup_class(self): | ||
self.proc = astore_setup() | ||
self.conn = AnalysisClient(TESTING_CONFIG) | ||
|
||
def test_conn_switch(self): | ||
w_conf = dict(host='wrong_host', | ||
port=0) | ||
tmp_conn = AnalysisClient(w_conf) | ||
tmp_conn.host == w_conf['host'] | ||
tmp_conn.port == 0 | ||
pytest.raises(requests.exceptions.ConnectionError, | ||
tmp_conn.connection_status) | ||
|
||
def test_urls(self): | ||
"""Catch potentially annoying and difficult to debug typos""" | ||
base_test_url = 'http://{}:{}/'.format(TESTING_CONFIG['host'], | ||
TESTING_CONFIG['port']) | ||
self.conn._host_url == base_test_url | ||
self.conn.aheader_url == base_test_url + 'analysis_header' | ||
self.conn.atail_url == base_test_url + 'analysis_tail' | ||
self.conn.dref_url == base_test_url + 'data_reference' | ||
self.conn.dref_header_url == base_test_url + 'data_reference_header' | ||
|
||
def test_doc_or_uid_to_uid(self): | ||
m_uid=str(uuid.uuid4()) | ||
test_dict = {'name': 'test_doc', 'uid': m_uid} | ||
m_uid == self.conn._doc_or_uid_to_uid(test_dict) | ||
|
||
def test_post_fact(self): | ||
pld = {'data': 'bogus'} | ||
sig = 'bogus' | ||
res = self.conn._post_factory(signature=sig, | ||
payload=pld) | ||
res['payload'] == pld | ||
res['signature'] == sig | ||
|
||
def test_query_fact(self): | ||
pld = {'data': 'bogus'} | ||
sig = 'bogus' | ||
res = self.conn._query_factory(signature=sig, | ||
payload=pld) | ||
res['payload'] == pld | ||
res['signature'] == sig | ||
|
||
def test_header_insert(self): | ||
pytest.raises(TypeError, self.conn.insert_analysis_header) | ||
m_uid = str(uuid.uuid4()) | ||
rid = self.conn.insert_analysis_header(uid=m_uid, time=time.time(), | ||
provenance={'version': 1.1}, | ||
custom=False) | ||
rid == m_uid | ||
|
||
def generate_ahdr(self): | ||
hid = self.conn.insert_analysis_header(uid=str(uuid.uuid4()), | ||
time=time.time(), | ||
provenance={'version': 1.1}, | ||
custom=False) | ||
return hid | ||
|
||
def generate_dref(self, hdr_id): | ||
did = self.conn.insert_analysis_tail(uid=str(uuid.uuid4()), | ||
analysis_header=hdr_id, | ||
time=time.time(), | ||
exit_status='test') | ||
return did | ||
|
||
def test_tail_insert(self): | ||
pytest.raises(TypeError, self.conn.insert_analysis_tail) | ||
t_uid = str(uuid.uuid4()) | ||
t = self.conn.insert_analysis_tail(uid=t_uid, | ||
analysis_header=self.generate_ahdr(), | ||
time=time.time(), exit_status='test') | ||
t_uid == t | ||
|
||
|
||
|
||
def test_dref_header_insert(self): | ||
pytest.raises(TypeError, self.conn.insert_data_reference_header) | ||
dh_uid = str(uuid.uuid4()) | ||
dh_id = self.conn.insert_data_reference_header(analysis_header=self.generate_ahdr(), | ||
time=time.time(), | ||
uid=dh_uid, | ||
data_keys={}) | ||
dh_id == dh_uid | ||
|
||
|
||
def test_dref_insert(self): | ||
pass | ||
|
||
def test_header_find(self): | ||
pass | ||
|
||
def test_tail_find(self): | ||
pass | ||
|
||
def test_dref_header_find(self): | ||
pass | ||
|
||
def test_dref_find(self): | ||
pass | ||
|
||
def teardown_class(self): | ||
astore_teardown(self.proc) | ||
def test_conn_switch(astore_server, astore_client): | ||
w_conf = dict(host="wrong_host", port=0) | ||
tmp_conn = AnalysisClient(w_conf) | ||
tmp_conn.host == w_conf["host"] | ||
tmp_conn.port == 0 | ||
pytest.raises(requests.exceptions.ConnectionError, tmp_conn.connection_status) | ||
|
||
|
||
def test_urls(astore_client): | ||
"""Catch potentially annoying and difficult to debug typos""" | ||
base_test_url = "http://{}:{}/".format( | ||
testing_config["host"], testing_config["port"] | ||
) | ||
astore_client._host_url == base_test_url | ||
astore_client.aheader_url == base_test_url + "analysis_header" | ||
astore_client.atail_url == base_test_url + "analysis_tail" | ||
astore_client.dref_url == base_test_url + "data_reference" | ||
astore_client.dref_header_url == base_test_url + "data_reference_header" | ||
|
||
|
||
def test_doc_or_uid_to_uid(astore_server, astore_client): | ||
m_uid = str(uuid.uuid4()) | ||
test_dict = {"name": "test_doc", "uid": m_uid} | ||
m_uid == astore_client._doc_or_uid_to_uid(test_dict) | ||
|
||
|
||
def test_post_fact(astore_server, astore_client): | ||
pld = {"data": "bogus"} | ||
sig = "bogus" | ||
res = astore_client._post_factory(signature=sig, payload=pld) | ||
res["payload"] == pld | ||
res["signature"] == sig | ||
|
||
|
||
def test_query_fact(astore_server, astore_client): | ||
pld = {"data": "bogus"} | ||
sig = "bogus" | ||
res = astore_client._query_factory(signature=sig, payload=pld) | ||
res["payload"] == pld | ||
res["signature"] == sig | ||
|
||
|
||
def test_header_insert(astore_server, astore_client): | ||
pytest.raises(TypeError, astore_client.insert_analysis_header) | ||
m_uid = str(uuid.uuid4()) | ||
rid = astore_client.insert_analysis_header( | ||
uid=m_uid, time=time.time(), provenance={"version": 1.1}, custom=False | ||
) | ||
rid == m_uid | ||
|
||
|
||
def generate_ahdr(astore_client): | ||
hid = astore_client.insert_analysis_header( | ||
uid=str(uuid.uuid4()), | ||
time=time.time(), | ||
provenance={"version": 1.1}, | ||
custom=False, | ||
) | ||
return hid | ||
|
||
|
||
def generate_dref(astore_client, hdr_id): | ||
did = astore_client.insert_analysis_tail( | ||
uid=str(uuid.uuid4()), | ||
analysis_header=hdr_id, | ||
time=time.time(), | ||
exit_status="test", | ||
) | ||
return did | ||
|
||
|
||
def test_tail_insert(astore_server, astore_client): | ||
pytest.raises(TypeError, astore_client.insert_analysis_tail) | ||
t_uid = str(uuid.uuid4()) | ||
t = astore_client.insert_analysis_tail( | ||
uid=t_uid, | ||
analysis_header=generate_ahdr(astore_client), | ||
time=time.time(), | ||
exit_status="test", | ||
) | ||
t_uid == t | ||
|
||
|
||
def test_dref_header_insert(astore_server, astore_client): | ||
pytest.raises(TypeError, astore_client.insert_data_reference_header) | ||
dh_uid = str(uuid.uuid4()) | ||
dh_id = astore_client.insert_data_reference_header( | ||
analysis_header=generate_ahdr(astore_client), | ||
time=time.time(), | ||
uid=dh_uid, | ||
data_keys={}, | ||
) | ||
dh_id == dh_uid |
Oops, something went wrong.