[tests] switch to pytest
This commit is contained in:
parent
b2e92d816d
commit
39fa7788b4
37 changed files with 1009 additions and 2259 deletions
|
@ -1,51 +1,57 @@
|
|||
import unittest
|
||||
import pytest
|
||||
import json
|
||||
import urllib.request
|
||||
|
||||
import util.location
|
||||
|
||||
|
||||
class location(unittest.TestCase):
|
||||
def setUp(self):
|
||||
patcher = unittest.mock.patch("util.location.urllib.request")
|
||||
self.addCleanup(patcher.stop)
|
||||
self.request = patcher.start()
|
||||
util.location.reset()
|
||||
@pytest.fixture
|
||||
def urllib_req(mocker):
|
||||
util.location.reset()
|
||||
return mocker.patch("util.location.urllib.request")
|
||||
|
||||
self.primary = {
|
||||
"country": "Middle Earth",
|
||||
"longitude": "10.0",
|
||||
"latitude": "20.5",
|
||||
"ip": "127.0.0.1",
|
||||
}
|
||||
self.secondary = {
|
||||
"country_name": "Rivia",
|
||||
"longitude": "-10.0",
|
||||
"latitude": "-23",
|
||||
"ip": "127.0.0.6",
|
||||
}
|
||||
|
||||
def test_primary_provider(self):
|
||||
self.request.urlopen.return_value.read.return_value = json.dumps(self.primary)
|
||||
util.location.country()
|
||||
self.assertEqual(self.primary["country"], util.location.country())
|
||||
self.assertEqual(
|
||||
(self.primary["latitude"], self.primary["longitude"]),
|
||||
util.location.coordinates(),
|
||||
)
|
||||
self.assertEqual(self.primary["ip"], util.location.public_ip())
|
||||
@pytest.fixture
|
||||
def primaryLocation():
|
||||
return {
|
||||
"country": "Middle Earth",
|
||||
"longitude": "10.0",
|
||||
"latitude": "20.5",
|
||||
"ip": "127.0.0.1",
|
||||
}
|
||||
|
||||
def test_secondary_provider(self):
|
||||
urlopen = unittest.mock.MagicMock()
|
||||
urlopen.read.return_value = json.dumps(self.secondary)
|
||||
self.request.urlopen.side_effect = [RuntimeError(), urlopen]
|
||||
|
||||
self.assertEqual(self.secondary["country_name"], util.location.country())
|
||||
self.assertEqual(
|
||||
(self.secondary["latitude"], self.secondary["longitude"]),
|
||||
util.location.coordinates(),
|
||||
)
|
||||
self.assertEqual(self.secondary["ip"], util.location.public_ip())
|
||||
@pytest.fixture
|
||||
def secondaryLocation():
|
||||
return {
|
||||
"country_name": "Rivia",
|
||||
"longitude": "-10.0",
|
||||
"latitude": "-23",
|
||||
"ip": "127.0.0.6",
|
||||
}
|
||||
|
||||
|
||||
def test_primary_provider(urllib_req, primaryLocation):
|
||||
urllib_req.urlopen.return_value.read.return_value = json.dumps(primaryLocation)
|
||||
|
||||
assert util.location.country() == primaryLocation["country"]
|
||||
assert util.location.coordinates() == (
|
||||
primaryLocation["latitude"],
|
||||
primaryLocation["longitude"],
|
||||
)
|
||||
assert util.location.public_ip() == primaryLocation["ip"]
|
||||
|
||||
|
||||
def test_secondary_provider(mocker, urllib_req, secondaryLocation):
|
||||
urlopen = mocker.MagicMock()
|
||||
urlopen.read.return_value = json.dumps(secondaryLocation)
|
||||
urllib_req.urlopen.side_effect = [RuntimeError(), urlopen]
|
||||
|
||||
assert util.location.country() == secondaryLocation["country_name"]
|
||||
assert util.location.coordinates() == (
|
||||
secondaryLocation["latitude"],
|
||||
secondaryLocation["longitude"],
|
||||
)
|
||||
assert util.location.public_ip() == secondaryLocation["ip"]
|
||||
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue