Skip to content
This repository has been archived by the owner on Nov 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #19 from the-scouts/testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
AA-Turner authored Feb 6, 2021
2 parents 7c5700d + 5b0628e commit 9637086
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: testing

on:
pull_request:
push:
branches: [master]

jobs:
testing:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Install pytest-cov, codecov
run: |
pip install pytest-cov codecov
- name: Test with pytest
run: |
python -m pytest --cov=./
- name: Upload to CodeCov
uses: codecov/codecov-action@v1
3 changes: 3 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ dependencies:
- email-validator
- phonenumbers # phonenumberslite?

# Testing
- pytest

# Development (Tools, Style, Linting)
- ipython
- black
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pydantic
email-validator
phonenumbers

# testing
pytest

# dev
ipython
black
Expand Down
83 changes: 83 additions & 0 deletions tests/test_compass_core_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import pytest

from compass.core import errors


class TestErrors:
def test_compass_error(self):
# Given
data = "message"

# Then
with pytest.raises(errors.CompassError, match=data):
# When
raise errors.CompassError(data)

def test_compass_authentication_error(self):
# Given
data = "message"

# Then
with pytest.raises(errors.CompassAuthenticationError, match=data):
# When
raise errors.CompassAuthenticationError(data)

def test_compass_report_error(self):
# Given
data = "message"

# Then
with pytest.raises(errors.CompassReportError, match=data):
# When
raise errors.CompassReportError(data)

def test_compass_report_permission_error(self):
# Given
data = "message"

# Then
with pytest.raises(errors.CompassReportPermissionError, match=data):
# When
raise errors.CompassReportPermissionError(data)

def test_compass_authentication_error_inheritance(self):
# Given
data = "message"

# When
try:
raise errors.CompassAuthenticationError(data)
# Then
except errors.CompassError as err:
assert str(err) == data

def test_compass_report_error_inheritance(self):
# Given
data = "message"

# When
try:
raise errors.CompassReportError(data)
# Then
except errors.CompassError as err:
assert str(err) == data

def test_compass_report_permission_error_inheritance(self):
# Given
data = "message"

# Dual inheritance:

# When
try:
raise errors.CompassReportPermissionError(data)
# Then
except PermissionError as err:
assert str(err) == data

# When
try:
raise errors.CompassReportPermissionError(data)
# Then
except errors.CompassError as err:
assert str(err) == data
62 changes: 62 additions & 0 deletions tests/test_compass_core_interface_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from pytest import MonkeyPatch
import requests

from compass.core.interface_base import InterfaceBase
from compass.core.settings import Settings


class TestInterfaceBase:
def test_get(self, monkeypatch: MonkeyPatch):
# Given
data = "https://example.org"
reqs = Settings.total_requests

# When
s = requests.Session()
monkeypatch.setattr(s, "get", lambda *args, **kwargs: dict(args=args, kwargs=kwargs))
result = InterfaceBase(s)._get(data) # pylint: disable=protected-access

# Then
assert result["args"][0] == data
assert reqs + 1 == Settings.total_requests

def test_post(self, monkeypatch: MonkeyPatch):
# Given
data = "https://example.org"
json = {"abc": 123, "xyz": 321}
reqs = Settings.total_requests

# When
s = requests.Session()
monkeypatch.setattr(s, "post", lambda *args, **kwargs: dict(args=args, kwargs=kwargs))
result = InterfaceBase(s)._post(data, json=json) # pylint: disable=protected-access

# Then
assert result["args"][0] == data
assert result["kwargs"]["json"] == json
assert reqs + 1 == Settings.total_requests

def test_head(self, monkeypatch: MonkeyPatch):
# Given
data = "https://example.org"
reqs = Settings.total_requests

# When
s = requests.Session()
monkeypatch.setattr(s, "head", lambda *args, **kwargs: dict(args=args, kwargs=kwargs))
result = InterfaceBase(s)._head(data) # pylint: disable=protected-access

# Then
assert result["args"][0] == data
assert reqs + 1 == Settings.total_requests

def test_update_headers(self):
# Given
data = {"header_one": 123, "abc_xyz": 321}

# When
ib = InterfaceBase(requests.Session())
ib._update_headers(data) # pylint: disable=protected-access

# Then
assert data.items() <= ib.s.headers.items()
56 changes: 56 additions & 0 deletions tests/test_compass_core_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from urllib.parse import urlparse

import _strptime

from compass.core.settings import Settings


class TestSettings:
def test_base_url(self):
# Given
data = Settings.base_url

# When
result = urlparse(data)

# Then
assert isinstance(data, str)
assert bool(result.scheme) and bool(result.hostname)

def test_date_format(self):
# Given
data = Settings.date_format

# When
result = _strptime._TimeRE_cache.pattern(data) # pylint: disable=protected-access

# Then
assert isinstance(data, str)
assert bool(result)

def test_org_number(self):
# Given When
data = Settings.org_number

# Then
assert isinstance(data, int)
assert 0 < data < 100_000_000 # IDs are between 1 and 99,999,999

def test_wcf_json_endpoint(self):
# Given When
data = Settings.wcf_json_endpoint

assert isinstance(data, str)
assert data[0] == "/"
assert ".svc" in data

def test_web_service_path(self):
# Given
data = Settings.web_service_path

# When
result = urlparse(data)

# Then
assert isinstance(data, str)
assert bool(result.scheme) and bool(result.hostname) and bool(result.path)
Loading

0 comments on commit 9637086

Please sign in to comment.