Skip to content
Snippets Groups Projects
Commit 52011c9f authored by Martin Burchell's avatar Martin Burchell
Browse files

Refactored tests to use REST API

In this case for listing terms
parent c75b4ab1
No related branches found
No related tags found
1 merge request!9Refactored REST API tests to use the API where possible
...@@ -6,7 +6,6 @@ from django.core.urlresolvers import reverse ...@@ -6,7 +6,6 @@ from django.core.urlresolvers import reverse
from rest_framework.test import APIRequestFactory from rest_framework.test import APIRequestFactory
from rest_framework import status from rest_framework import status
from taxonomies.models import Term
from ..serializers import TermSerializer from ..serializers import TermSerializer
from ..views import ( from ..views import (
TaxonomyViewSet, TaxonomyViewSet,
...@@ -33,6 +32,13 @@ def taxonomy_exists(name): ...@@ -33,6 +32,13 @@ def taxonomy_exists(name):
return name in names 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(): def count_taxonomies():
return len(get_taxonomies().data) return len(get_taxonomies().data)
...@@ -47,6 +53,18 @@ def get_taxonomies(): ...@@ -47,6 +53,18 @@ def get_taxonomies():
return response 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 @pytest.mark.django_db
def test_create_a_category(): def test_create_a_category():
category = "Test Ebola Questions" category = "Test Ebola Questions"
...@@ -82,6 +100,7 @@ def add_term(**kwargs): ...@@ -82,6 +100,7 @@ def add_term(**kwargs):
@pytest.mark.django_db @pytest.mark.django_db
def test_add_term_to_taxonomy(): def test_add_term_to_taxonomy():
# TODO: Use API to create taxonomy
taxonomy = TaxonomyFactory(name='Test Ebola Questions') taxonomy = TaxonomyFactory(name='Test Ebola Questions')
response1 = add_term(taxonomy=taxonomy.slug, name='Vaccine') response1 = add_term(taxonomy=taxonomy.slug, name='Vaccine')
...@@ -89,18 +108,20 @@ def test_add_term_to_taxonomy(): ...@@ -89,18 +108,20 @@ def test_add_term_to_taxonomy():
assert status.is_success(response1.status_code), response1.data assert status.is_success(response1.status_code), response1.data
assert status.is_success(response2.status_code), response2.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 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 @pytest.mark.django_db
def test_terms_have_long_name(): def test_terms_have_long_name():
# TODO: Use API to create taxonomy
taxonomy = TaxonomyFactory(name="Ebola Questions") taxonomy = TaxonomyFactory(name="Ebola Questions")
vacc_long_name = "Is there a vaccine?" 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( response = add_term(
taxonomy=taxonomy.slug, taxonomy=taxonomy.slug,
...@@ -110,12 +131,12 @@ def test_terms_have_long_name(): ...@@ -110,12 +131,12 @@ def test_terms_have_long_name():
assert status.is_success(response.status_code), response.data 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 @pytest.mark.django_db
def test_id_field_not_in_serialized_terms(): def test_id_field_not_in_serialized_terms():
# TODO: Use API to create term
term = TermFactory() term = TermFactory()
serialzed = TermSerializer(term).data 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