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

Refactors test_routes as a separate module #266

Merged
merged 12 commits into from
Jan 10, 2020
17 changes: 16 additions & 1 deletion tests/unit/test_routes/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from datetime import datetime
from datetime import datetime, timedelta
from app.api.validations import (
MISSING_PARAMS, INVALID_PARAMS, MISSING_BODY, INVALID_TYPE
)
from app.models import Resource
from app.utils import get_error_code_from_status


Expand Down Expand Up @@ -50,6 +51,20 @@ def update_resource(client,
headers={'x-apikey': apikey} if not headers else headers)


def set_resource_last_updated(updated_time=(datetime.now() + timedelta(days=-7)),
id=None):
if id is not None:
row = Resource.query.get(id)
row.created_at = updated_time
row.last_updated = updated_time

else:
q = Resource.query
for row in q:
row.created_at = updated_time
row.last_updated = updated_time


def get_api_key(client):
response = client.post('api/v1/apikey', json=dict(
email="test@example.org",
Expand Down
34 changes: 30 additions & 4 deletions tests/unit/test_routes/test_resource_retreival.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

from datetime import datetime, timedelta
from .helpers import assert_correct_response
from .helpers import (
create_resource, update_resource, get_api_key, set_resource_last_updated,
assert_correct_response
)


def test_get_resources(module_client, module_db):
aaron-junot marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -118,8 +121,9 @@ def test_paid_filter_invalid_paid_parameter(module_client, module_db):
assert (False in [res.get('paid') for res in response.json['data']])

aaron-junot marked this conversation as resolved.
Show resolved Hide resolved

def test_filters(module_client, module_db):
def test_filters(module_client, module_db, fake_auth_from_oc, fake_algolia_save):
client = module_client
apikey = get_api_key(client)

# Filter by one language
response = client.get('api/v1/resources?languages=python')
Expand All @@ -144,5 +148,27 @@ def test_filters(module_client, module_db):
for resource in response.json['data']:
assert (resource.get('category') == "Back End Dev")

# TODO: Filter by updated_after
# (Need to figure out how to manually set last_updated and created_at)
# Filter by updated_after
aaron-junot marked this conversation as resolved.
Show resolved Hide resolved
filter_time = datetime.now() + timedelta(days=-1)

# Given I update all resources to be last updated 1 week ago.
aaron-junot marked this conversation as resolved.
Show resolved Hide resolved
set_resource_last_updated()
module_db.session.commit()
aaron-junot marked this conversation as resolved.
Show resolved Hide resolved

# And I create and update two different new resources.
create_resource(client, apikey)
update_resource(client, apikey)

# When I call get resources with an updated_after filter of 1 day ago.
response = client.get(f"/api/v1/resources?updated_after={filter_time}")

# Then the response should have only two resources
assert len(response.json['data']) == 2
for resource in response.json['data']:
assert (
filter_time <= datetime.strptime(resource.get('created_at'),
'%Y-%m-%d %H:%M:%S')
or
filter_time <= datetime.strptime(resource.get('last_updated'),
'%Y-%m-%d %H:%M:%S')
)