[tests] switch to pytest
This commit is contained in:
parent
b2e92d816d
commit
39fa7788b4
37 changed files with 1009 additions and 2259 deletions
|
@ -1,42 +1,50 @@
|
|||
import unittest
|
||||
import pytest
|
||||
|
||||
from util.algorithm import *
|
||||
|
||||
@pytest.fixture
|
||||
def someData():
|
||||
return {"a": 100, "b": 200, "c": [1, 2, 3]}
|
||||
|
||||
class algorithm(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.someData = {"a": 100, "b": 200, "c": [1, 2, 3]}
|
||||
self.differentData = {"x": 20, "y": "bla", "z": ["a", "b"]}
|
||||
self.moreData = {"n": 100}
|
||||
self.overlapData = {"a": 200, "c": [1, 2, 4]}
|
||||
@pytest.fixture
|
||||
def differentData():
|
||||
return {"x": 20, "y": "bla", "z": ["a", "b"]}
|
||||
|
||||
def test_merge_with_empty(self):
|
||||
self.assertEqual(self.someData, merge(self.someData, {}))
|
||||
self.assertEqual(None, merge(self.someData, None))
|
||||
@pytest.fixture
|
||||
def moreData():
|
||||
return {"n": 100}
|
||||
|
||||
def test_merge_no_overwrite(self):
|
||||
result = merge(self.someData, self.differentData)
|
||||
for k in self.someData:
|
||||
self.assertEqual(result[k], self.someData[k])
|
||||
@pytest.fixture
|
||||
def overlapData():
|
||||
return {"a": 200, "c": [1, 2, 4]}
|
||||
|
||||
def test_merge_with_empty(someData):
|
||||
assert merge(someData, {}) == someData
|
||||
assert merge(someData, None) == None
|
||||
|
||||
def test_merge_no_overwrite(someData, differentData):
|
||||
result = merge(someData, differentData)
|
||||
for k in someData:
|
||||
assert someData[k] == result[k]
|
||||
for k in self.differentData:
|
||||
self.assertEqual(result[k], self.differentData[k])
|
||||
assert differentData[k] == result[k]
|
||||
|
||||
def test_merge_multiple(self):
|
||||
result = merge(self.someData, self.differentData, self.moreData)
|
||||
for k in self.someData:
|
||||
self.assertEqual(result[k], self.someData[k])
|
||||
for k in self.differentData:
|
||||
self.assertEqual(result[k], self.differentData[k])
|
||||
for k in self.moreData:
|
||||
self.assertEqual(result[k], self.moreData[k])
|
||||
def test_merge_multiple(someData, differentData, moreData):
|
||||
result = merge(someData, differentData, moreData)
|
||||
for k in someData:
|
||||
assert someData[k] == result[k]
|
||||
for k in differentData:
|
||||
assert differentData[k] == result[k]
|
||||
for k in moreData:
|
||||
assert moreData[k] == result[k]
|
||||
|
||||
def merge_overlap(self):
|
||||
result = merge(self.someData, self.overlapData)
|
||||
for k in self.someData:
|
||||
def merge_overlap(someData, overlapData):
|
||||
result = merge(someData, overlapData)
|
||||
for k in someData:
|
||||
if not k in self.overlapData:
|
||||
self.assertEqual(result[k], self.someData[k])
|
||||
assert someData[k] == result[k]
|
||||
for k in self.overlapData:
|
||||
self.assertEqual(result[k], self.overlapData[k])
|
||||
assert overlapData[k] == result[k]
|
||||
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -1,43 +1,27 @@
|
|||
import unittest
|
||||
import pytest
|
||||
|
||||
import util.cli
|
||||
|
||||
def test_valid_command():
|
||||
assert util.cli.execute("echo test") == "test\n"
|
||||
|
||||
class cli(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.nonExistentCommand = "i-do-not-exist"
|
||||
self.validCommand = "echo test"
|
||||
self.validCommandOutput = "test\n"
|
||||
self.utfCommand = "echo ÖPmŧß"
|
||||
self.utfCommandOutput = "ÖPmŧß\n"
|
||||
def test_utf_command():
|
||||
rv = util.cli.execute("echo ÖPmŧß")
|
||||
assert util.cli.execute("echo ÖPmŧß") == "ÖPmŧß\n"
|
||||
|
||||
def test_valid_command(self):
|
||||
rv = util.cli.execute(self.validCommand)
|
||||
self.assertEqual(self.validCommandOutput, rv)
|
||||
def test_invalid_command():
|
||||
with pytest.raises(RuntimeError):
|
||||
util.cli.execute("i-do-not-exist")
|
||||
|
||||
def test_utf_command(self):
|
||||
rv = util.cli.execute(self.utfCommand)
|
||||
self.assertEqual(self.utfCommandOutput, rv)
|
||||
def test_command_exit_code():
|
||||
with pytest.raises(RuntimeError):
|
||||
util.cli.execute("cat i-do-not-exist")
|
||||
|
||||
def test_invalid_command(self):
|
||||
with self.assertRaises(RuntimeError):
|
||||
util.cli.execute(self.nonExistentCommand)
|
||||
def test_command_exit_code_no_error():
|
||||
util.cli.execute("cat i-do-not-exist", ignore_errors=True)
|
||||
|
||||
def test_command_exit_code(self):
|
||||
with self.assertRaises(RuntimeError):
|
||||
util.cli.execute("cat {}".format(self.nonExistentCommand))
|
||||
|
||||
def test_command_exit_code_no_error(self):
|
||||
try:
|
||||
util.cli.execute(
|
||||
"cat {}".format(self.nonExistentCommand), ignore_errors=True
|
||||
)
|
||||
except Exception:
|
||||
self.fail("exception was thrown")
|
||||
|
||||
def test_async(self):
|
||||
rv = util.cli.execute(self.validCommand, wait=False)
|
||||
self.assertEqual("", rv)
|
||||
def test_async():
|
||||
assert util.cli.execute("echo test", wait=False) == ""
|
||||
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -1,103 +1,113 @@
|
|||
import unittest
|
||||
import pytest
|
||||
|
||||
from util.format import *
|
||||
|
||||
|
||||
class format(unittest.TestCase):
|
||||
def test_int_from_string(self):
|
||||
self.assertEqual(100, asint("100"))
|
||||
self.assertEqual(-100, asint("-100"))
|
||||
self.assertEqual(0, asint("0"))
|
||||
def test_int_from_string():
|
||||
assert asint("100") == 100
|
||||
assert asint("-100") == -100
|
||||
assert asint("0") == 0
|
||||
|
||||
def test_int_from_none(self):
|
||||
self.assertEqual(0, asint(None))
|
||||
|
||||
def test_int_from_int(self):
|
||||
self.assertEqual(100, asint(100))
|
||||
self.assertEqual(-100, asint(-100))
|
||||
self.assertEqual(0, asint(0))
|
||||
def test_int_from_none():
|
||||
assert asint(None) == 0
|
||||
|
||||
def test_int_minimum(self):
|
||||
self.assertEqual(100, asint(100, minimum=10))
|
||||
self.assertEqual(100, asint(100, minimum=100))
|
||||
self.assertEqual(10, asint(5, minimum=10))
|
||||
|
||||
def test_int_maximum(self):
|
||||
self.assertEqual(100, asint(100, maximum=200))
|
||||
self.assertEqual(100, asint(100, maximum=100))
|
||||
self.assertEqual(100, asint(200, maximum=100))
|
||||
def test_int_from_int():
|
||||
assert asint(100) == 100
|
||||
assert asint(-100) == -100
|
||||
assert asint(0) == 0
|
||||
|
||||
def test_true_from_str(self):
|
||||
self.assertTrue(asbool("true"))
|
||||
self.assertTrue(asbool(True))
|
||||
self.assertTrue(asbool("t"))
|
||||
self.assertTrue(asbool("1"))
|
||||
self.assertTrue(asbool("yes"))
|
||||
self.assertTrue(asbool("y"))
|
||||
self.assertTrue(asbool("on"))
|
||||
|
||||
def test_false_from_str(self):
|
||||
self.assertFalse(asbool("false"))
|
||||
self.assertFalse(asbool(False))
|
||||
self.assertFalse(asbool("f"))
|
||||
self.assertFalse(asbool("0"))
|
||||
self.assertFalse(asbool("no"))
|
||||
self.assertFalse(asbool("n"))
|
||||
self.assertFalse(asbool("off"))
|
||||
self.assertFalse(asbool(None))
|
||||
def test_int_minimum():
|
||||
assert asint(100, minimum=10) == 100
|
||||
assert asint(100, minimum=100) == 100
|
||||
assert asint(5, minimum=10) == 10
|
||||
|
||||
def test_list_from_None(self):
|
||||
self.assertEqual([], aslist(None))
|
||||
|
||||
def test_list_from_list(self):
|
||||
self.assertEqual([1, 2, 3], aslist([1, 2, 3]))
|
||||
def test_int_maximum():
|
||||
assert asint(100, maximum=200) == 100
|
||||
assert asint(100, maximum=100) == 100
|
||||
assert asint(200, maximum=100) == 100
|
||||
|
||||
def test_list_from_str(self):
|
||||
self.assertEqual(["12", "13", "14"], aslist("12,13,14"))
|
||||
|
||||
def test_byteformat(self):
|
||||
self.assertEqual("500.00B", byte(500))
|
||||
self.assertEqual("1.00KiB", byte(1024))
|
||||
self.assertEqual("1KiB", byte(1024, "{:.0f}"))
|
||||
self.assertEqual("1.50KiB", byte(1024 + 512))
|
||||
self.assertEqual("2.50MiB", byte(1024 * 1024 * 2 + 1024 * 512))
|
||||
self.assertEqual("4.50GiB", byte(1024 * 1024 * 1024 * 4 + 1024 * 1024 * 512))
|
||||
self.assertEqual("2048.00GiB", byte(1024 * 1024 * 1024 * 1024 * 2))
|
||||
def test_true_from_str():
|
||||
assert asbool("true") == True
|
||||
assert asbool(True) == True
|
||||
assert asbool("t") == True
|
||||
assert asbool("1") == True
|
||||
assert asbool("yes") == True
|
||||
assert asbool("y") == True
|
||||
assert asbool("on") == True
|
||||
|
||||
def test_duration(self):
|
||||
self.assertEqual("04:20:00", duration(4 * 60 * 60 + 20 * 60))
|
||||
self.assertEqual("04:20:00h", duration(4 * 60 * 60 + 20 * 60, unit=True))
|
||||
self.assertEqual(
|
||||
"04:20h", duration(4 * 60 * 60 + 20 * 60, compact=True, unit=True)
|
||||
)
|
||||
|
||||
self.assertEqual("20:00", duration(20 * 60))
|
||||
self.assertEqual("20:00m", duration(20 * 60, unit=True))
|
||||
self.assertEqual("20:00m", duration(20 * 60, compact=True, unit=True))
|
||||
def test_false_from_str():
|
||||
assert asbool("false") == False
|
||||
assert asbool(False) == False
|
||||
assert asbool("f") == False
|
||||
assert asbool("0") == False
|
||||
assert asbool("no") == False
|
||||
assert asbool("n") == False
|
||||
assert asbool("off") == False
|
||||
assert asbool(None) == False
|
||||
|
||||
self.assertEqual("00:20", duration(20))
|
||||
self.assertEqual("00:20m", duration(20, unit=True))
|
||||
self.assertEqual("00:20m", duration(20, compact=True, unit=True))
|
||||
|
||||
self.assertEqual("n/a", duration(-1))
|
||||
def test_list_from_None():
|
||||
assert aslist(None) == []
|
||||
|
||||
def test_seconds(self):
|
||||
self.assertEqual(10, seconds(10))
|
||||
self.assertEqual(10, seconds("10"))
|
||||
|
||||
self.assertEqual(300, seconds("5m"))
|
||||
self.assertEqual(320, seconds("5m20s"))
|
||||
def test_list_from_list():
|
||||
assert aslist([1, 2, 3] == [1, 2, 3])
|
||||
|
||||
self.assertEqual(4 * 3600, seconds("4h"))
|
||||
self.assertEqual(4 * 3600 + 5 * 60 + 22, seconds("4h5m22s"))
|
||||
|
||||
self.assertEqual(4 * 3600 + 5 * 60, seconds("4h5m"))
|
||||
def test_list_from_str():
|
||||
assert aslist("12,13,14") == ["12", "13", "14"]
|
||||
|
||||
def test_temperature(self):
|
||||
self.assertEqual("10°C", astemperature(10))
|
||||
self.assertEqual("10°C", astemperature(10, "metric"))
|
||||
self.assertEqual("-100°F", astemperature(-100, "imperial"))
|
||||
self.assertEqual("-100°K", astemperature("-100", "kelvin"))
|
||||
|
||||
def test_byteformat():
|
||||
assert byte(500) == "500.00B"
|
||||
assert byte(1024) == "1.00KiB"
|
||||
assert byte(1024, "{:.0f}") == "1KiB"
|
||||
assert byte(1024 + 512) == "1.50KiB"
|
||||
assert byte(1024 * 1024 * 2 + 1024 * 512) == "2.50MiB"
|
||||
assert byte(1024 * 1024 * 1024 * 4 + 1024 * 1024 * 512) == "4.50GiB"
|
||||
assert byte(1024 * 1024 * 1024 * 1024 * 2) == "2048.00GiB"
|
||||
|
||||
|
||||
def test_duration():
|
||||
assert duration(4 * 60 * 60 + 20 * 60) == "04:20:00"
|
||||
assert duration(4 * 60 * 60 + 20 * 60, unit=True) == "04:20:00h"
|
||||
assert duration(4 * 60 * 60 + 20 * 60, compact=True, unit=True) == "04:20h"
|
||||
|
||||
assert duration(20 * 60) == "20:00"
|
||||
assert duration(20 * 60, unit=True) == "20:00m"
|
||||
assert duration(20 * 60, compact=True, unit=True) == "20:00m"
|
||||
|
||||
assert duration(20) == "00:20"
|
||||
assert duration(20, unit=True) == "00:20m"
|
||||
assert duration(20, compact=True, unit=True) == "00:20m"
|
||||
|
||||
assert duration(-1) == "n/a"
|
||||
|
||||
|
||||
def test_seconds():
|
||||
assert seconds(10) == 10
|
||||
assert seconds("10") == 10
|
||||
|
||||
assert seconds("5m") == 300
|
||||
assert seconds("5m20s") == 320
|
||||
|
||||
assert seconds("4h") == 4 * 3600
|
||||
assert seconds("4h5m22s") == 4 * 3600 + 5 * 60 + 22
|
||||
|
||||
assert seconds("4h5m") == 4 * 3600 + 5 * 60
|
||||
|
||||
|
||||
def test_temperature():
|
||||
assert astemperature(10) == "10°C"
|
||||
assert astemperature(10, "metric") == "10°C"
|
||||
assert astemperature(-100, "imperial") == "-100°F"
|
||||
assert astemperature(-100, "kelvin") == "-100°K"
|
||||
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,62 +1,48 @@
|
|||
import unittest
|
||||
import pytest
|
||||
|
||||
import util.store
|
||||
|
||||
|
||||
class store(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.store = util.store.Store()
|
||||
@pytest.fixture
|
||||
def emptyStore():
|
||||
return util.store.Store()
|
||||
|
||||
self.unusedKey = "someRandomUnusedKey"
|
||||
self.someKey = "someRandomKey"
|
||||
self.someOtherKey = "anotherRandomKey"
|
||||
self.someValue = "someRandomValue"
|
||||
self.someOtherValue = "anotherRandomValue"
|
||||
|
||||
def test_get_of_unset_key(self):
|
||||
self.assertEqual(
|
||||
None, self.store.get(self.unusedKey), "default value expected to be None"
|
||||
)
|
||||
self.assertEqual(
|
||||
self.someValue,
|
||||
self.store.get(self.unusedKey, self.someValue),
|
||||
"wrong user-provided default value returned",
|
||||
)
|
||||
@pytest.fixture
|
||||
def store():
|
||||
return util.store.Store()
|
||||
|
||||
def test_get_of_set_key(self):
|
||||
self.assertNotEqual(self.someValue, None)
|
||||
|
||||
self.store.set(self.someKey, self.someValue)
|
||||
self.assertEqual(
|
||||
self.someValue,
|
||||
self.store.get(self.someKey),
|
||||
"unexpected value for existing key",
|
||||
)
|
||||
def test_get_of_unset_key(emptyStore):
|
||||
assert emptyStore.get("any-key") == None
|
||||
assert emptyStore.get("any-key", "default-value") == "default-value"
|
||||
|
||||
def test_overwrite_set(self):
|
||||
self.assertNotEqual(self.someValue, None)
|
||||
self.assertNotEqual(self.someOtherValue, self.someValue)
|
||||
|
||||
self.store.set(self.someKey, self.someValue)
|
||||
self.store.set(self.someKey, self.someOtherValue)
|
||||
self.assertEqual(
|
||||
self.someOtherValue,
|
||||
self.store.get(self.someKey),
|
||||
"unexpected value for existing key",
|
||||
)
|
||||
def test_get_of_set_key(store):
|
||||
store.set("key", "value")
|
||||
assert store.get("key") == "value"
|
||||
|
||||
def test_unused_keys(self):
|
||||
self.assertNotEqual(self.someKey, self.someOtherKey)
|
||||
|
||||
self.store.set(self.someKey, self.someValue)
|
||||
self.store.set(self.someOtherKey, self.someOtherValue)
|
||||
def test_overwrite_set(store):
|
||||
store.set("key", "value 1")
|
||||
store.set("key", "value 2")
|
||||
|
||||
self.assertEqual(
|
||||
sorted(self.store.unused_keys()), sorted([self.someKey, self.someOtherKey])
|
||||
)
|
||||
assert store.get("key") == "value 2"
|
||||
|
||||
self.store.get(self.someKey)
|
||||
self.assertEqual(self.store.unused_keys(), [self.someOtherKey])
|
||||
|
||||
def test_unused_keys(store):
|
||||
store.set("key 1", "value x")
|
||||
store.set("key 2", "value y")
|
||||
|
||||
assert store.unused_keys() == sorted(["key 1", "key 2"])
|
||||
|
||||
store.get("key 2")
|
||||
|
||||
assert store.unused_keys() == ["key 1"]
|
||||
|
||||
store.get("key 1")
|
||||
|
||||
assert store.unused_keys() == []
|
||||
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue