Skip to content
Snippets Groups Projects
Commit a8a08c39 authored by Alice Heaton's avatar Alice Heaton :speech_balloon:
Browse files

Merge branch 'rest_api_test' into develop

parents 40c091f5 a1e12bf8
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,10 @@ from rest_api.views import ItemViewSet
def create_item(**kwargs):
request = APIRequestFactory().post('/items', kwargs)
view = ItemViewSet.as_view(actions={'post': 'create'})
return view(request)
response = view(request)
assert status.is_success(response.status_code), response.data
return response
@pytest.mark.django_db
......
......@@ -2,12 +2,13 @@ from __future__ import unicode_literals, absolute_import
import pytest
from data_layer.tests.factories import ItemFactory
from data_layer.models import Item
from rest_framework.test import APIRequestFactory
from rest_framework import status
from ..views import ItemViewSet
from .item_create_view_tests import create_item
from .item_list_view_tests import get as list_items
def delete_item(id):
request = APIRequestFactory().delete('/')
......@@ -15,14 +16,20 @@ def delete_item(id):
return view(request, pk=id)
def count_items():
items = list_items().data
return len(items)
@pytest.mark.django_db
def test_delete_item():
ItemFactory(body="test1")
item = ItemFactory()
assert Item.objects.count() == 2
response = delete_item(item.id)
create_item(body="test1")
item = create_item(body="test2")
assert count_items() == 2
response = delete_item(item.data['id'])
assert status.is_success(response.status_code)
assert Item.objects.count() == 1
assert Item.objects.get().body == "test1"
assert count_items() == 1
[item] = list_items().data
assert item['body'] == "test1"
......@@ -6,6 +6,8 @@ from taxonomies.tests.factories import TermFactory
from ..views import ItemViewSet
from .item_create_view_tests import create_item
def get(data=None):
view = ItemViewSet.as_view(actions={'get': 'list'})
......@@ -22,7 +24,7 @@ def test_get_items_returns_empty_if_no_items():
@pytest.mark.django_db
def test_get_items_returns_all_items():
item = ItemFactory(body='test')
create_item(body='test')
items = get().data
......@@ -33,8 +35,8 @@ def test_get_items_returns_all_items():
@pytest.mark.django_db
def test_filter_by_body():
ItemFactory(body="one")
ItemFactory(body="two")
create_item(body="one")
create_item(body="two")
payload = get(data={'body': 'one'}).data
......@@ -44,8 +46,12 @@ def test_filter_by_body():
@pytest.mark.django_db
def test_filter_by_id_list():
ItemFactory()
item_ids = [ItemFactory().id for i in range(10)]
create_item(body='initial item')
item_ids = []
for i in range(10):
item = create_item(body='item %d' % i).data
item_ids.append(item['id'])
payload = get(data={'ids': item_ids}).data
......@@ -54,6 +60,8 @@ def test_filter_by_id_list():
@pytest.mark.django_db
def test_item_listed_with_associated_terms():
# TODO: Refactor to use the REST API when we can add
# multiple terms to an item
item = ItemFactory()
terms = [TermFactory() for i in range(3)]
for term in terms:
......
......@@ -6,7 +6,6 @@ from django.core.urlresolvers import reverse
from rest_framework.test import APIRequestFactory
from rest_framework import status
from taxonomies.models import Taxonomy, Term
from ..serializers import TermSerializer
from ..views import (
TaxonomyViewSet,
......@@ -25,20 +24,61 @@ def create_category(name):
return view(request, pk=id)
def taxonomy_exists(name):
taxonomies = get_taxonomies().data
names = [t['name'] for t in taxonomies]
return name in names
def term_exists(long_name, slug):
terms = get_terms(slug).data
names = [t['long_name'] for t in terms]
return long_name in names
def count_taxonomies():
return len(get_taxonomies().data)
def get_taxonomies():
url = reverse('taxonomy-list')
request = APIRequestFactory().get(url)
view = TaxonomyViewSet.as_view(actions={'get': 'list'})
response = view(request)
assert status.is_success(response.status_code), response.data
return response
def get_terms(taxonomy_slug):
url = reverse('term-list')
request = APIRequestFactory().get(url,
data={'taxonomy': taxonomy_slug})
view = TermViewSet.as_view(actions={'get': 'list'})
response = view(request)
assert status.is_success(response.status_code), response.data
return response
@pytest.mark.django_db
def test_create_a_category():
category = "Test Ebola Questions"
old_count = Taxonomy.objects.count()
assert not Taxonomy.objects.filter(name=category).exists()
old_count = count_taxonomies()
assert not taxonomy_exists(category)
response = create_category(category)
assert status.is_success(response.status_code), response.data
new_count = Taxonomy.objects.count()
new_count = count_taxonomies()
assert new_count - old_count == 1
assert Taxonomy.objects.filter(name=category).exists()
assert taxonomy_exists(category)
# TODO: write test for getting taxonomies and terms, so we can re-write all
......@@ -60,6 +100,7 @@ def add_term(**kwargs):
@pytest.mark.django_db
def test_add_term_to_taxonomy():
# TODO: Use API to create taxonomy
taxonomy = TaxonomyFactory(name='Test Ebola Questions')
response1 = add_term(taxonomy=taxonomy.slug, name='Vaccine')
......@@ -67,18 +108,20 @@ def test_add_term_to_taxonomy():
assert status.is_success(response1.status_code), response1.data
assert status.is_success(response2.status_code), response2.data
terms = Term.objects.filter(taxonomy=taxonomy)
terms = get_terms(taxonomy.slug).data
assert len(terms) == 2
assert all(term.taxonomy.name == taxonomy.name for term in terms)
assert all(term['taxonomy'] == taxonomy.slug for term in terms)
@pytest.mark.django_db
def test_terms_have_long_name():
# TODO: Use API to create taxonomy
taxonomy = TaxonomyFactory(name="Ebola Questions")
vacc_long_name = "Is there a vaccine?"
assert not Term.objects.filter(long_name=vacc_long_name).exists()
assert not term_exists(vacc_long_name, taxonomy.slug)
response = add_term(
taxonomy=taxonomy.slug,
......@@ -88,12 +131,12 @@ def test_terms_have_long_name():
assert status.is_success(response.status_code), response.data
assert Term.objects.filter(long_name=vacc_long_name).exists()
assert term_exists(vacc_long_name, taxonomy.slug)
@pytest.mark.django_db
def test_id_field_not_in_serialized_terms():
# TODO: Use API to create term
term = TermFactory()
serialzed = TermSerializer(term).data
......
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