From 52011c9fd1a9e576ded47cc7692e91bed140e085 Mon Sep 17 00:00:00 2001
From: Martin Burchell <martinb@aptivate.org>
Date: Wed, 29 Jul 2015 17:38:15 +0100
Subject: [PATCH] Refactored tests to use REST API

In this case for listing terms
---
 .../tests/taxonomy_and_term_create_tests.py   | 33 +++++++++++++++----
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/django/website/rest_api/tests/taxonomy_and_term_create_tests.py b/django/website/rest_api/tests/taxonomy_and_term_create_tests.py
index 8a297d20..9564271d 100644
--- a/django/website/rest_api/tests/taxonomy_and_term_create_tests.py
+++ b/django/website/rest_api/tests/taxonomy_and_term_create_tests.py
@@ -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 Term
 from ..serializers import TermSerializer
 from ..views import (
     TaxonomyViewSet,
@@ -33,6 +32,13 @@ def taxonomy_exists(name):
     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)
 
@@ -47,6 +53,18 @@ def get_taxonomies():
     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"
@@ -82,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')
@@ -89,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,
@@ -110,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
-- 
GitLab