ojuso-map/apps/map/tests.py

123 lines
4.9 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.test import TestCase
from .models import CaseStudy
from django.contrib.auth.models import User
from django.urls import reverse
from .models import CaseStudyDraft
class CaseStudyDraftAPITests(TestCase):
test_user = 'testuser1'
test_pass = '12345'
def setUp(self):
test_user1 = User.objects.create_user(username=self.test_user, password=self.test_pass)
test_user1.save()
def login(self):
return self.client.login(username=self.test_user, password=self.test_pass)
def test_get_not_logged_in(self):
response = self.client.get(reverse('drafts'))
self.assertEqual(response.status_code, 403)
def test_put_not_logged_in(self):
response = self.client.put(reverse('drafts'))
self.assertEqual(response.status_code, 403)
def test_delete_not_logged_in(self):
response = self.client.delete(reverse('drafts'))
self.assertEqual(response.status_code, 403)
def test_get_and_put(self):
login = self.login()
# No draft should currently exist
response = self.client.get(reverse('drafts'))
self.assertEqual(response.status_code, 404)
self.assertEqual(response.content, b"")
# Putting a draft should succeed
response = self.client.put(reverse('drafts'), '{"test":1}')
self.assertEqual(response.status_code, 201)
# We should get it back
response = self.client.get(reverse('drafts'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], "application/json")
self.assertEqual(response.content, b'{"test":1}')
# Updating it should succeed
response = self.client.put(reverse('drafts'), '{"test":2}')
self.assertEqual(response.status_code, 200)
# We should get the update back
response = self.client.get(reverse('drafts'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], "application/json")
self.assertEqual(response.content, b'{"test":2}')
def test_delete(self):
login = self.login()
# Create a draft
response = self.client.put(reverse('drafts'), '{"test":1}')
self.assertEqual(response.status_code, 201)
# Delete it again
response = self.client.delete(reverse('drafts'))
self.assertEqual(response.status_code, 204)
# Check it's not there
response = self.client.get(reverse('drafts'))
self.assertEqual(response.status_code, 404)
class CaseStudyTests(TestCase):
def test_get_renewable_generation_detail_when_empty(self):
"""get_renewable_generation_detail() should return the empty string."""
case_study = CaseStudy()
self.assertIs(case_study.get_renewable_generation_detail(), "")
def test_get_renewable_generation_detail_with_geo(self):
"""get_renewable_generation_detail() should return just the description"""
case_study = CaseStudy(generation_technology='GEOT')
self.assertEqual(case_study.get_renewable_generation_detail(), "Geothermal electricity")
def test_get_renewable_generation_detail_with_wind(self):
"""get_renewable_generation_detail() should return the description prefixed with 'wind power'"""
case_study = CaseStudy(generation_technology='SSWE')
self.assertEqual(case_study.get_renewable_generation_detail(), "Wind energy Small-scale (less than 500kW)")
def test_get_renewable_generation_detail_with_other(self):
"""get_renewable_generation_detail() should return the detail provided in .generation_technology_other"""
case_study = CaseStudy(generation_technology='OTHR', generation_technology_other='Warp drive')
self.assertEqual(case_study.get_renewable_generation_detail(), "Warp drive")
# These tests are commented out because they are not working, but the code
# in production is. When running as a test, get_negative_case_reasons_no_other()
# is returning a list of coded options, like
# ['V', 'O', 'L', 'R', ',', 'A', 'L', 'A', 'B']
# instead of a list of text like
# ['Violation of land rights', 'Abusive labour practices']
#  I am too much of a Django newbie to know why.  Anna
# def test_get_negative_case_reasons_no_other_1(self):
# """Test with case having no 'other' entry"""
# case_study = CaseStudy(negative_case_reasons='VOLR,ALAB')
# self.assertEqual(case_study.get_negative_case_reasons_no_other(),
# [ 'Violation of land rights'
# , 'Abusive labour practices'
# ])
#
# def test_get_negative_case_reasons_no_other_2(self):
# """Test with case having an 'other' entry"""
# case_study = CaseStudy(negative_case_reasons='VOLR,ALAB,OTHR')
# self.assertEqual(case_study.get_negative_case_reasons_no_other(),
# [ 'Violation of land rights'
# , 'Abusive labour practices'
# ])