Skip to content

Commit a4a1040

Browse files
committed
mirrors: convert to pytest
1 parent 2ad17d3 commit a4a1040

File tree

3 files changed

+73
-81
lines changed

3 files changed

+73
-81
lines changed

mirrors/tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,20 @@ def mirrorurl(db, mirror, mirrorprotocol, country=COUNTRY,
4545
country=country)
4646
yield mirror_url
4747
mirror_url.delete()
48+
49+
50+
@pytest.fixture
51+
def create_mirrorurl(db, mirror, mirrorprotocol):
52+
mirrors = []
53+
def _create_mirrorurl(country=COUNTRY, url=URL):
54+
mirror_url = MirrorUrl.objects.create(url=url,
55+
protocol=mirrorprotocol,
56+
mirror=mirror,
57+
country=country)
58+
mirrors.append(mirror_url)
59+
return mirror_url
60+
61+
yield _create_mirrorurl
62+
63+
for mirror in mirrors:
64+
mirror.delete()

mirrors/tests/test_mirrorlist.py

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,50 @@
1-
from django.test import TestCase
2-
3-
from mirrors.tests import create_mirror_url
41
from mirrors.models import Mirror
52

3+
# TODO(jelle): add test for https/rsync mirrors
4+
5+
6+
def test_mirrorlist(client, mirrorurl):
7+
response = client.get('/mirrorlist/')
8+
assert response.status_code == 200
9+
10+
11+
def test_mirrorlist_tier_last(client, mirrorurl):
12+
last_tier = Mirror.TIER_CHOICES[-1][0]
13+
response = client.get('/mirrorlist/tier/{}/'.format(last_tier + 1))
14+
assert response.status_code == 404
15+
16+
17+
def test_mirrorlist_all(client, mirrorurl):
18+
response = client.get('/mirrorlist/all/')
19+
assert response.status_code == 200
20+
assert mirrorurl.hostname in response.content.decode()
21+
22+
23+
def test_mirrorlist_all_https(client, mirrorurl):
24+
response = client.get('/mirrorlist/all/https/')
25+
assert response.status_code == 200
26+
assert mirrorurl.hostname in response.content.decode()
27+
28+
29+
def test_mirrorlist_all_http(client, mirrorurl):
30+
# First test that without any http mirrors, we get a 404.
31+
response = client.get('/mirrorlist/all/http/')
32+
assert response.status_code == 404
33+
34+
35+
def test_mirrorlist_status(client, mirrorurl):
36+
response = client.get('/mirrorlist/?country=all&use_mirror_status=on')
37+
assert response.status_code == 200
38+
39+
40+
def test_mirrorlist_filter(client, create_mirrorurl):
41+
mirror1 = create_mirrorurl('JP', 'https://jp.org')
42+
mirror2 = create_mirrorurl()
43+
44+
# First test that we correctly see the above mirror.
45+
response = client.get('/mirrorlist/?country=JP&protocol=https')
46+
assert response.status_code == 200
47+
assert mirror1.hostname in response.content.decode()
648

7-
class MirrorListTest(TestCase):
8-
def setUp(self):
9-
self.mirror_url = create_mirror_url()
10-
11-
def tearDown(self):
12-
self.mirror_url.delete()
13-
14-
def test_mirrorlist(self):
15-
response = self.client.get('/mirrorlist/')
16-
self.assertEqual(response.status_code, 200)
17-
18-
def test_mirrorlist_tier_last(self):
19-
last_tier = Mirror.TIER_CHOICES[-1][0]
20-
response = self.client.get('/mirrorlist/tier/{}/'.format(last_tier + 1))
21-
self.assertEqual(response.status_code, 404)
22-
23-
def test_mirrorlist_all(self):
24-
response = self.client.get('/mirrorlist/all/')
25-
self.assertEqual(response.status_code, 200)
26-
self.assertIn(self.mirror_url.hostname, response.content.decode())
27-
28-
def test_mirrorlist_all_http(self):
29-
response = self.client.get('/mirrorlist/all/http/')
30-
self.assertEqual(response.status_code, 200)
31-
self.assertIn(self.mirror_url.hostname, response.content.decode())
32-
33-
def test_mirrorlist_all_https(self):
34-
# First test that without any https mirrors, we get a 404.
35-
response = self.client.get('/mirrorlist/all/https/')
36-
self.assertEqual(response.status_code, 404)
37-
38-
# Now, after adding an HTTPS mirror, we expect to succeed.
39-
https_mirror_url = create_mirror_url(
40-
name='https_mirror',
41-
protocol='https',
42-
url='https://wikipedia.org')
43-
response = self.client.get('/mirrorlist/all/https/')
44-
self.assertEqual(response.status_code, 200)
45-
https_mirror_url.delete()
46-
47-
def test_mirrorlist_filter(self):
48-
jp_mirror_url = create_mirror_url(
49-
name='jp_mirror',
50-
country='JP',
51-
protocol='https',
52-
url='https://wikipedia.jp')
53-
54-
# First test that we correctly see the above mirror.
55-
response = self.client.get('/mirrorlist/?country=JP&protocol=https')
56-
self.assertEqual(response.status_code, 200)
57-
self.assertIn(jp_mirror_url.hostname, response.content.decode())
58-
59-
# Now confirm that the US mirror did not show up.
60-
self.assertNotIn(self.mirror_url.hostname, response.content.decode())
61-
62-
jp_mirror_url.delete()
63-
64-
def test_mirrorlist_status(self):
65-
response = self.client.get('/mirrorlist/?country=all&use_mirror_status=on')
66-
self.assertEqual(response.status_code, 200)
49+
# Now confirm that the US mirror did not show up.
50+
assert not mirror2.hostname in response.content.decode()
Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
from django.test import TestCase
2-
3-
from mirrors.models import CheckLocation
4-
5-
6-
class MirrorLocationsTest(TestCase):
7-
def setUp(self):
8-
self.checklocation = CheckLocation.objects.create(hostname='arch.org',
9-
source_ip='8.8.8.8',
10-
country='US')
11-
12-
def test_mirrorlocations_json(self):
13-
response = self.client.get('/mirrors/locations/json/')
14-
self.assertEqual(response.status_code, 200)
15-
data = response.json()
16-
self.assertEqual(1, data['version'])
17-
location = data['locations'][0]['country_code']
18-
self.assertEqual('US', location)
1+
from mirrors.tests.conftest import COUNTRY
2+
3+
def test_mirrorlocations_json(client, checklocation):
4+
response = client.get('/mirrors/locations/json/')
5+
assert response.status_code == 200
6+
data = response.json()
7+
assert 1 == data['version']
8+
location = data['locations'][0]['country_code']
9+
assert COUNTRY == location

0 commit comments

Comments
 (0)