Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run unit tests GH Actions with tox #139

Merged
merged 8 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/tox-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run unit tests with tox

on:
- push
- pull_request

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
# Note that this list of versions should correspond with what's in tox.ini
python-version:
- '2.7'
- '3.5'
- '3.6'
- '3.7'
- '3.8'
- '3.9'

steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install tox==3.25.0 tox-gh-actions==2.9.1
- name: Run tox (excluding integration tests)
env:
NOSE_ARGS: -A integration!=1
run: tox
4 changes: 2 additions & 2 deletions requirements-testing.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-r requirements.txt

mock==1.0.1
nose==1.3.3
mock==1.0.1 ; python_version < '3.0'
nose==1.3.7
nose-parameterized==0.3.3
8 changes: 0 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
if line:
requirements.append(line.strip("\n"))

test_requirements = [
"mock",
"nose",
"nose-parameterized",
]

setup(
name="gapipy",
version=gapipy.__version__,
Expand Down Expand Up @@ -53,6 +47,4 @@
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
],
tests_require=test_requirements,
test_suite="nose.collector",
)
9 changes: 6 additions & 3 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import time
from unittest import TestCase, skip, skipUnless

import mock

from gapipy import cache

try:
from unittest import mock # Python 3
except ImportError:
import mock # Python 2

try:
from django.test import override_settings
except ImportError:
Expand Down Expand Up @@ -46,7 +49,7 @@ def test_gapi_cache_settings_required(self):
def test_clear(self):
"""Should delegate 'clear' operation to django cache client."""
self.client.clear()
self.mock_cache.clear.assert_called_once()
self.assertEqual(len(self.mock_cache.clear.mock_calls), 1)

def test_delete(self):
"""Should delegate 'delete' operation to django cache client."""
Expand Down
12 changes: 8 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import json
from mock import patch
import unittest

from gapipy.client import Client
from gapipy.query import Query
from gapipy.resources.base import Resource
from gapipy.utils import get_available_resource_classes

try:
from unittest import mock # Python 3
except ImportError:
import mock # Python 2


class ClientTestCase(unittest.TestCase):

Expand Down Expand Up @@ -37,7 +41,7 @@ class MockResource(Resource):
self.assertEqual(resource.id, 1)
self.assertEqual(resource.foo, 'bar')

@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
def test_create_interface(self, mock_request):
class MockResource(Resource):
_as_is_fields = ['id', 'foo']
Expand All @@ -55,7 +59,7 @@ class MockResource(Resource):
resource = self.gapi.create('foo', {'id': 1, 'foo': 'bar', 'context': 'abc'})
self.assertEqual(resource.id, 1)

@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
def test_create_extra_headers(self, mock_request):
"""
Test that extra HTTP headers can be passed through the `.create`
Expand All @@ -81,7 +85,7 @@ class MockResource(Resource):
additional_headers=extra_headers,
)

@patch('gapipy.query.Query.get_resource_data')
@mock.patch('gapipy.query.Query.get_resource_data')
def test_correct_client_is_associated_with_resources(self, mock_get_data):
mock_get_data.return_value = {
'id': 123
Expand Down
40 changes: 22 additions & 18 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import sys
import unittest

from mock import MagicMock, patch
from requests import HTTPError, Response

from gapipy.client import Client
Expand All @@ -15,6 +14,11 @@
PPP_TOUR_DATA, TOUR_DOSSIER_LIST_DATA, DUMMY_DEPARTURE, DUMMY_PROMOTION,
)

try:
from unittest import mock # Python 3
except ImportError:
import mock # Python 2


class QueryKeyTestCase(unittest.TestCase):

Expand Down Expand Up @@ -106,13 +110,13 @@ def setUp(self):
self.cache = self.client._cache
self.cache.clear()

@patch('gapipy.request.APIRequestor._request', return_value=PPP_TOUR_DATA)
@mock.patch('gapipy.request.APIRequestor._request', return_value=PPP_TOUR_DATA)
def test_get_instance_by_id(self, mock_request):
query = Query(self.client, Tour)
t = query.get(1234)
self.assertIsInstance(t, Tour)

@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
def test_get_instance_with_forbidden_id(self, mock_request):
response = Response()
response.status_code = 403
Expand All @@ -132,7 +136,7 @@ def test_get_instance_with_forbidden_id(self, mock_request):
context.exception.response.status_code,
response.status_code)

@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
def test_get_instance_with_non_existing_id(self, mock_request):
response = Response()
response.status_code = 404
Expand All @@ -152,7 +156,7 @@ def test_get_instance_with_non_existing_id(self, mock_request):
context.exception.response.status_code,
response.status_code)

@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
def test_get_instance_with_gone_id(self, mock_request):
response = Response()
response.status_code = 410
Expand All @@ -172,7 +176,7 @@ def test_get_instance_with_gone_id(self, mock_request):
context.exception.response.status_code,
response.status_code)

@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
def test_get_instance_by_id_with_non_404_error(self, mock_request):
response = Response()
response.status_code = 401
Expand All @@ -194,7 +198,7 @@ def test_get_instance_by_id_with_non_404_error(self, mock_request):
self.assertIsNone(
query.get(1234, httperrors_mapped_to_none=[response.status_code]))

@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
def test_filtered_query_returns_new_object(self, mock_request):
"""
Arguments passed to .filter() are stored on new (copied) Query instance
Expand All @@ -208,7 +212,7 @@ def test_filtered_query_returns_new_object(self, mock_request):
self.assertFalse(query is query1)
self.assertNotEqual(query._filters, query1._filters)

@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
def test_filtered_query(self, mock_request):
"""
Arguments passed to .filter() are stored on new (copied) Query instance
Expand Down Expand Up @@ -241,7 +245,7 @@ def test_filtered_query(self, mock_request):
query.count()
self.assertEqual(len(query._filters), 2)

@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
def test_query_persist_filter_on_count(self, mock_request):
query = Query(self.client, Tour)
my_query = query.filter(tour_dossier_code='PPP')
Expand All @@ -261,26 +265,26 @@ def test_listing_non_listable_resource_fails(self):
with self.assertRaisesRegex(ValueError, message):
Query(self.client, Activity).count()

@patch('gapipy.request.APIRequestor._request', return_value=DUMMY_PROMOTION)
@mock.patch('gapipy.request.APIRequestor._request', return_value=DUMMY_PROMOTION)
def test_can_retrieve_single_non_listable_resource(self, mock_request):
Query(self.client, Activity).get(1234)
mock_request.assert_called_once_with(
'/activities/1234', 'GET', additional_headers=None)

@patch('gapipy.request.APIRequestor._request', return_value=DUMMY_DEPARTURE)
@mock.patch('gapipy.request.APIRequestor._request', return_value=DUMMY_DEPARTURE)
def test_can_retrieve_single_subresource_without_parent(self, mock_request):
Query(self.client, Departure).get(1234)
mock_request.assert_called_once_with(
'/departures/1234', 'GET', additional_headers=None)

@patch('gapipy.request.APIRequestor._request', return_value=TOUR_DOSSIER_LIST_DATA)
@mock.patch('gapipy.request.APIRequestor._request', return_value=TOUR_DOSSIER_LIST_DATA)
def test_count(self, mock_request):
query = Query(self.client, TourDossier)
count = query.count()
self.assertIsInstance(count, int)
self.assertEqual(count, 3)

@patch('gapipy.request.APIRequestor._request', return_value=TOUR_DOSSIER_LIST_DATA)
@mock.patch('gapipy.request.APIRequestor._request', return_value=TOUR_DOSSIER_LIST_DATA)
def test_fetch_all(self, mock_request):

query = Query(self.client, TourDossier).all()
Expand All @@ -296,7 +300,7 @@ def test_fetch_all(self, mock_request):
mock_request.assert_called_once_with(
'/tour_dossiers', 'GET', params={})

@patch('gapipy.request.APIRequestor._request', return_value=TOUR_DOSSIER_LIST_DATA)
@mock.patch('gapipy.request.APIRequestor._request', return_value=TOUR_DOSSIER_LIST_DATA)
def test_fetch_all_with_limit(self, mock_request):

query = Query(self.client, TourDossier).all(limit=2)
Expand Down Expand Up @@ -341,7 +345,7 @@ def setUp(self):
self.cache = self.client._cache
self.cache.clear()

@patch('gapipy.request.APIRequestor._request', return_value=PPP_TOUR_DATA)
@mock.patch('gapipy.request.APIRequestor._request', return_value=PPP_TOUR_DATA)
def test_resources_are_cached(self, mock_request):
query = Query(self.client, Tour)

Expand All @@ -367,10 +371,10 @@ def test_cached_get_does_not_set(self):
query = Query(self.client, Tour)

# act like we already have the data in our cache
mock_cache_get = MagicMock(return_value=PPP_TOUR_DATA)
mock_cache_get = mock.MagicMock(return_value=PPP_TOUR_DATA)
self.cache.get = mock_cache_get

mock_cache_set = MagicMock()
mock_cache_set = mock.MagicMock()
self.cache.set = mock_cache_set

query.get(21346)
Expand All @@ -383,7 +387,7 @@ class MockResource(Resource):
_resource_name = 'mocks'


@patch('gapipy.request.APIRequestor._request')
@mock.patch('gapipy.request.APIRequestor._request')
class UpdateCreateResourceTestCase(unittest.TestCase):

def setUp(self):
Expand Down
Loading