Commit ed1f4ce0 authored by Jelle van der Waa's avatar Jelle van der Waa 🚧
Browse files

test: use response.json()

Instead of importing json and loading the response.content using
json.loads use Django's response.json() method.
parent 35512840
import json
from django.test import TestCase from django.test import TestCase
from mirrors.models import CheckLocation from mirrors.models import CheckLocation
...@@ -14,7 +12,7 @@ def setUp(self): ...@@ -14,7 +12,7 @@ def setUp(self):
def test_mirrorlocations_json(self): def test_mirrorlocations_json(self):
response = self.client.get('/mirrors/locations/json/') response = self.client.get('/mirrors/locations/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(1, data['version']) self.assertEqual(1, data['version'])
location = data['locations'][0]['country_code'] location = data['locations'][0]['country_code']
self.assertEqual('US', location) self.assertEqual('US', location)
import json
from django.test import TestCase from django.test import TestCase
from mirrors.tests import create_mirror_url from mirrors.tests import create_mirror_url
...@@ -28,7 +26,7 @@ def test_details_json(self): ...@@ -28,7 +26,7 @@ def test_details_json(self):
response = self.client.get(url + 'json/') response = self.client.get(url + 'json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertNotEqual(data['urls'], []) self.assertNotEqual(data['urls'], [])
def test_url_details(self): def test_url_details(self):
......
import json
from django.test import TestCase from django.test import TestCase
from mirrors.tests import create_mirror_url from mirrors.tests import create_mirror_url
...@@ -13,7 +11,7 @@ def test_status(self): ...@@ -13,7 +11,7 @@ def test_status(self):
def test_json_endpoint(self): def test_json_endpoint(self):
response = self.client.get('/mirrors/status/json/') response = self.client.get('/mirrors/status/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(data['urls'], []) self.assertEqual(data['urls'], [])
mirror_url = create_mirror_url() mirror_url = create_mirror_url()
...@@ -21,13 +19,13 @@ def test_json_endpoint(self): ...@@ -21,13 +19,13 @@ def test_json_endpoint(self):
# Verify that the cache works # Verify that the cache works
response = self.client.get('/mirrors/status/json/') response = self.client.get('/mirrors/status/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
# Disables the cache_function's cache # Disables the cache_function's cache
with self.settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}): with self.settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}):
response = self.client.get('/mirrors/status/json/') response = self.client.get('/mirrors/status/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(len(data['urls']), 1) self.assertEqual(len(data['urls']), 1)
mirror = data['urls'][0] mirror = data['urls'][0]
...@@ -41,7 +39,7 @@ def test_json_tier(self): ...@@ -41,7 +39,7 @@ def test_json_tier(self):
response = self.client.get('/mirrors/status/tier/1/json/') response = self.client.get('/mirrors/status/tier/1/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(data['urls'], []) self.assertEqual(data['urls'], [])
mirror_url = create_mirror_url() mirror_url = create_mirror_url()
...@@ -50,7 +48,7 @@ def test_json_tier(self): ...@@ -50,7 +48,7 @@ def test_json_tier(self):
with self.settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}): with self.settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}):
response = self.client.get('/mirrors/status/json/') response = self.client.get('/mirrors/status/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertNotEqual(data['urls'], []) self.assertNotEqual(data['urls'], [])
mirror_url.delete() mirror_url.delete()
import json
import unittest import unittest
from django.core import mail from django.core import mail
...@@ -67,7 +66,7 @@ class PackageSearchJson(TestCase): ...@@ -67,7 +66,7 @@ class PackageSearchJson(TestCase):
def test_invalid(self): def test_invalid(self):
response = self.client.get('/packages/search/json/') response = self.client.get('/packages/search/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(data['limit'], 250) self.assertEqual(data['limit'], 250)
self.assertEqual(data['results'], []) self.assertEqual(data['results'], [])
self.assertEqual(data['valid'], False) self.assertEqual(data['valid'], False)
...@@ -75,7 +74,7 @@ def test_invalid(self): ...@@ -75,7 +74,7 @@ def test_invalid(self):
def test_reponame(self): def test_reponame(self):
response = self.client.get('/packages/search/json/?repository=core') response = self.client.get('/packages/search/json/?repository=core')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(len(data['results']), 5) self.assertEqual(len(data['results']), 5)
self.assertEqual(set(map(lambda r: r['pkgname'], data['results'])), self.assertEqual(set(map(lambda r: r['pkgname'], data['results'])),
{"coreutils", "glibc", "linux", "pacman", "systemd"}) {"coreutils", "glibc", "linux", "pacman", "systemd"})
...@@ -83,19 +82,19 @@ def test_reponame(self): ...@@ -83,19 +82,19 @@ def test_reponame(self):
def test_packagename(self): def test_packagename(self):
response = self.client.get('/packages/search/json/?name=linux') response = self.client.get('/packages/search/json/?name=linux')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(len(data['results']), 1) self.assertEqual(len(data['results']), 1)
def test_no_results(self): def test_no_results(self):
response = self.client.get('/packages/search/json/?name=none') response = self.client.get('/packages/search/json/?name=none')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(len(data['results']), 0) self.assertEqual(len(data['results']), 0)
def test_limit_four(self): def test_limit_four(self):
response = self.client.get('/packages/search/json/?limit=4') response = self.client.get('/packages/search/json/?limit=4')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(data['page'], 1) self.assertEqual(data['page'], 1)
self.assertEqual(data['num_pages'], 2) self.assertEqual(data['num_pages'], 2)
self.assertEqual(data['limit'], 4) self.assertEqual(data['limit'], 4)
...@@ -104,7 +103,7 @@ def test_limit_four(self): ...@@ -104,7 +103,7 @@ def test_limit_four(self):
def test_second_page(self): def test_second_page(self):
response = self.client.get('/packages/search/json/?limit=4&page=2') response = self.client.get('/packages/search/json/?limit=4&page=2')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(data['page'], 2) self.assertEqual(data['page'], 2)
self.assertEqual(data['num_pages'], 2) self.assertEqual(data['num_pages'], 2)
self.assertEqual(len(data['results']), 1) self.assertEqual(len(data['results']), 1)
...@@ -206,7 +205,7 @@ def test_packages_detail(self): ...@@ -206,7 +205,7 @@ def test_packages_detail(self):
def test_packages_json(self): def test_packages_json(self):
response = self.client.get('/packages/core/x86_64/linux/json/') response = self.client.get('/packages/core/x86_64/linux/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(data['pkgbase'], 'linux') self.assertEqual(data['pkgbase'], 'linux')
# TODO verify more of the structure # TODO verify more of the structure
...@@ -217,7 +216,7 @@ def test_packages_files(self): ...@@ -217,7 +216,7 @@ def test_packages_files(self):
def test_packages_files_json(self): def test_packages_files_json(self):
response = self.client.get('/packages/core/x86_64/linux/files/json/') response = self.client.get('/packages/core/x86_64/linux/files/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(data['pkgname'], 'linux') self.assertEqual(data['pkgname'], 'linux')
# TODO verify more of the structure # TODO verify more of the structure
......
import json
from django.test import TestCase from django.test import TestCase
from releng.models import Release from releng.models import Release
...@@ -15,7 +13,7 @@ def test_release_json(self): ...@@ -15,7 +13,7 @@ def test_release_json(self):
version = self.release.version version = self.release.version
response = self.client.get('/releng/releases/json/') response = self.client.get('/releng/releases/json/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = json.loads(response.content) data = response.json()
self.assertEqual(data['version'], 1) self.assertEqual(data['version'], 1)
release = data['releases'][0] release = data['releases'][0]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment