83 lines
2.4 KiB
Python
83 lines
2.4 KiB
Python
from dataclasses import dataclass
|
|
|
|
import pytest
|
|
from aiohttp import ClientConnectionError, ClientResponseError
|
|
|
|
from jarvis_core import util
|
|
from jarvis_core.util import ansi, http
|
|
|
|
|
|
async def test_hash():
|
|
hashes: dict[str, dict[str, str]] = {
|
|
"sha256": {
|
|
"hello": "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
|
|
"https://zevaryx.com/media/logo.png": "668ddf4ec8b0c7315c8a8bfdedc36b242ff8f4bba5debccd8f5fa07776234b6a",
|
|
},
|
|
"sha1": {
|
|
"hello": "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d",
|
|
"https://zevaryx.com/media/logo.png": "989f8065819c6946493797209f73ffe37103f988",
|
|
},
|
|
}
|
|
|
|
for hash_method, items in hashes.items():
|
|
for value, correct in items.items():
|
|
print(value)
|
|
assert (await util.hash(data=value, method=hash_method))[0] == correct
|
|
|
|
with pytest.raises(ClientResponseError):
|
|
await util.hash("https://zevaryx.com/known-not-to-exist")
|
|
with pytest.raises(ClientConnectionError):
|
|
await util.hash("https://known-to-not-exist.zevaryx.com")
|
|
|
|
|
|
def test_bytesize():
|
|
size = 4503599627370496
|
|
converted = util.convert_bytesize(size)
|
|
assert converted == "4.000 PB"
|
|
|
|
assert util.unconvert_bytesize(4, "PB") == size
|
|
|
|
assert util.convert_bytesize(None) == "??? B"
|
|
assert util.unconvert_bytesize(4, "B") == 4
|
|
|
|
|
|
def test_find_get():
|
|
@dataclass
|
|
class TestModel:
|
|
x: int
|
|
|
|
models = [TestModel(3), TestModel(9), TestModel(100), TestModel(-2)]
|
|
|
|
assert util.find(lambda x: x.x > 0, models).x == 3
|
|
assert util.find(lambda x: x.x > 100, models) is None
|
|
|
|
assert len(util.find_all(lambda x: x.x % 2 == 0, models)) == 2
|
|
|
|
assert util.get(models, x=3).x == 3
|
|
assert util.get(models, x=11) is None
|
|
assert util.get(models).x == 3
|
|
assert util.get(models, y=3) is None
|
|
|
|
assert len(util.get_all(models, x=9)) == 1
|
|
assert len(util.get_all(models, y=1)) == 0
|
|
assert util.get_all(models) == models
|
|
|
|
|
|
async def test_http_get_size():
|
|
url = "http://ipv4.download.thinkbroadband.com/100MB.zip"
|
|
size = 104857600
|
|
|
|
assert await http.get_size(url) == size
|
|
|
|
with pytest.raises(ValueError):
|
|
await http.get_size("invalid")
|
|
|
|
|
|
def test_ansi():
|
|
known = "\x1b[0;35;41m"
|
|
assert ansi.fmt(1, ansi.Format.NORMAL, ansi.Fore.PINK, ansi.Back.ORANGE) == known
|
|
|
|
assert 4 in ansi.Format
|
|
assert 2 not in ansi.Format
|
|
|
|
assert ansi.fmt() == ansi.RESET
|