2017-03-28 13:12:44 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-03-19 15:14:55 +01:00
|
|
|
# Copyright 2015-2022 Mike Fährmann
|
2017-03-28 13:12:44 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
2020-05-02 01:15:50 +02:00
|
|
|
import os
|
2017-08-13 14:31:22 +02:00
|
|
|
import sys
|
2020-05-02 01:15:50 +02:00
|
|
|
import unittest
|
|
|
|
|
2020-01-25 22:57:08 +01:00
|
|
|
import io
|
2018-03-14 13:17:34 +01:00
|
|
|
import random
|
|
|
|
import string
|
2021-01-09 01:45:46 +01:00
|
|
|
import datetime
|
2020-01-25 22:57:08 +01:00
|
|
|
import http.cookiejar
|
2017-08-13 14:31:22 +02:00
|
|
|
|
2020-05-02 01:15:50 +02:00
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from gallery_dl import util, text, exception # noqa E402
|
2019-02-14 11:15:19 +01:00
|
|
|
|
2017-08-13 14:31:22 +02:00
|
|
|
|
|
|
|
class TestRange(unittest.TestCase):
|
|
|
|
|
2018-10-07 21:34:25 +02:00
|
|
|
def test_parse_range(self, f=util.RangePredicate.parse_range):
|
2017-08-13 14:31:22 +02:00
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f(""),
|
2017-08-13 14:31:22 +02:00
|
|
|
[])
|
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f("1-2"),
|
2017-08-13 14:31:22 +02:00
|
|
|
[(1, 2)])
|
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f("-"),
|
2017-08-13 14:31:22 +02:00
|
|
|
[(1, sys.maxsize)])
|
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f("-2,4,6-8,10-"),
|
2017-08-13 14:31:22 +02:00
|
|
|
[(1, 2), (4, 4), (6, 8), (10, sys.maxsize)])
|
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f(" - 3 , 4- 4, 2-6"),
|
2017-08-13 14:31:22 +02:00
|
|
|
[(1, 3), (4, 4), (2, 6)])
|
|
|
|
|
2018-10-07 21:34:25 +02:00
|
|
|
def test_optimize_range(self, f=util.RangePredicate.optimize_range):
|
2017-08-13 14:31:22 +02:00
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f([]),
|
2017-08-13 14:31:22 +02:00
|
|
|
[])
|
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f([(2, 4)]),
|
2017-08-13 14:31:22 +02:00
|
|
|
[(2, 4)])
|
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f([(2, 4), (6, 8), (10, 12)]),
|
2017-08-13 14:31:22 +02:00
|
|
|
[(2, 4), (6, 8), (10, 12)])
|
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f([(2, 4), (4, 6), (5, 8)]),
|
2017-08-13 14:31:22 +02:00
|
|
|
[(2, 8)])
|
|
|
|
self.assertEqual(
|
2018-10-07 21:34:25 +02:00
|
|
|
f([(1, 1), (2, 2), (3, 6), (8, 9)]),
|
2017-08-13 14:31:22 +02:00
|
|
|
[(1, 6), (8, 9)])
|
|
|
|
|
2017-09-06 17:57:19 +02:00
|
|
|
|
|
|
|
class TestPredicate(unittest.TestCase):
|
|
|
|
|
2017-08-13 14:31:22 +02:00
|
|
|
def test_range_predicate(self):
|
2017-09-06 17:57:19 +02:00
|
|
|
dummy = None
|
|
|
|
|
2018-10-07 21:34:25 +02:00
|
|
|
pred = util.RangePredicate(" - 3 , 4- 4, 2-6")
|
2017-08-13 14:31:22 +02:00
|
|
|
for i in range(6):
|
2017-09-06 17:57:19 +02:00
|
|
|
self.assertTrue(pred(dummy, dummy))
|
2017-08-13 14:31:22 +02:00
|
|
|
with self.assertRaises(exception.StopExtraction):
|
2017-09-06 17:57:19 +02:00
|
|
|
bool(pred(dummy, dummy))
|
2017-08-13 14:31:22 +02:00
|
|
|
|
2018-10-07 21:34:25 +02:00
|
|
|
pred = util.RangePredicate("1, 3, 5")
|
2017-09-06 17:57:19 +02:00
|
|
|
self.assertTrue(pred(dummy, dummy))
|
|
|
|
self.assertFalse(pred(dummy, dummy))
|
|
|
|
self.assertTrue(pred(dummy, dummy))
|
|
|
|
self.assertFalse(pred(dummy, dummy))
|
|
|
|
self.assertTrue(pred(dummy, dummy))
|
2017-08-13 14:31:22 +02:00
|
|
|
with self.assertRaises(exception.StopExtraction):
|
2017-09-06 17:57:19 +02:00
|
|
|
bool(pred(dummy, dummy))
|
2017-08-13 14:31:22 +02:00
|
|
|
|
2018-10-07 21:34:25 +02:00
|
|
|
pred = util.RangePredicate("")
|
2017-08-13 14:31:22 +02:00
|
|
|
with self.assertRaises(exception.StopExtraction):
|
2017-09-06 17:57:19 +02:00
|
|
|
bool(pred(dummy, dummy))
|
|
|
|
|
|
|
|
def test_unique_predicate(self):
|
|
|
|
dummy = None
|
|
|
|
pred = util.UniquePredicate()
|
2018-02-20 18:14:27 +01:00
|
|
|
|
|
|
|
# no duplicates
|
2017-09-06 17:57:19 +02:00
|
|
|
self.assertTrue(pred("1", dummy))
|
|
|
|
self.assertTrue(pred("2", dummy))
|
|
|
|
self.assertFalse(pred("1", dummy))
|
|
|
|
self.assertFalse(pred("2", dummy))
|
|
|
|
self.assertTrue(pred("3", dummy))
|
|
|
|
self.assertFalse(pred("3", dummy))
|
|
|
|
|
2018-02-20 18:14:27 +01:00
|
|
|
# duplicates for "text:"
|
|
|
|
self.assertTrue(pred("text:123", dummy))
|
|
|
|
self.assertTrue(pred("text:123", dummy))
|
|
|
|
self.assertTrue(pred("text:123", dummy))
|
|
|
|
|
2019-03-06 19:46:54 +01:00
|
|
|
def test_filter_predicate(self):
|
|
|
|
url = ""
|
|
|
|
|
|
|
|
pred = util.FilterPredicate("a < 3")
|
|
|
|
self.assertTrue(pred(url, {"a": 2}))
|
|
|
|
self.assertFalse(pred(url, {"a": 3}))
|
|
|
|
|
|
|
|
with self.assertRaises(SyntaxError):
|
|
|
|
util.FilterPredicate("(")
|
|
|
|
|
|
|
|
with self.assertRaises(exception.FilterError):
|
|
|
|
util.FilterPredicate("a > 1")(url, {"a": None})
|
|
|
|
|
|
|
|
with self.assertRaises(exception.FilterError):
|
|
|
|
util.FilterPredicate("b > 1")(url, {"a": 2})
|
|
|
|
|
2017-09-06 17:57:19 +02:00
|
|
|
def test_build_predicate(self):
|
|
|
|
pred = util.build_predicate([])
|
|
|
|
self.assertIsInstance(pred, type(lambda: True))
|
|
|
|
|
|
|
|
pred = util.build_predicate([util.UniquePredicate()])
|
|
|
|
self.assertIsInstance(pred, util.UniquePredicate)
|
|
|
|
|
|
|
|
pred = util.build_predicate([util.UniquePredicate(),
|
|
|
|
util.UniquePredicate()])
|
2021-07-19 02:43:23 +02:00
|
|
|
self.assertIs(pred.func, util.chain_predicates)
|
2017-03-28 13:12:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestISO639_1(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_code_to_language(self):
|
2017-08-08 19:22:04 +02:00
|
|
|
d = "default"
|
|
|
|
self._run_test(util.code_to_language, {
|
|
|
|
("en",): "English",
|
|
|
|
("FR",): "French",
|
2021-05-22 16:07:04 +02:00
|
|
|
("ja",): "Japanese",
|
2017-08-08 19:22:04 +02:00
|
|
|
("xx",): None,
|
|
|
|
("" ,): None,
|
|
|
|
(None,): None,
|
|
|
|
("en", d): "English",
|
|
|
|
("FR", d): "French",
|
|
|
|
("xx", d): d,
|
|
|
|
("" , d): d,
|
|
|
|
(None, d): d,
|
|
|
|
})
|
2017-03-28 13:12:44 +02:00
|
|
|
|
|
|
|
def test_language_to_code(self):
|
2017-08-08 19:22:04 +02:00
|
|
|
d = "default"
|
|
|
|
self._run_test(util.language_to_code, {
|
|
|
|
("English",): "en",
|
|
|
|
("fRENch",): "fr",
|
2021-05-22 16:07:04 +02:00
|
|
|
("Japanese",): "ja",
|
2017-08-08 19:22:04 +02:00
|
|
|
("xx",): None,
|
|
|
|
("" ,): None,
|
|
|
|
(None,): None,
|
|
|
|
("English", d): "en",
|
|
|
|
("fRENch", d): "fr",
|
|
|
|
("xx", d): d,
|
|
|
|
("" , d): d,
|
|
|
|
(None, d): d,
|
|
|
|
})
|
|
|
|
|
|
|
|
def _run_test(self, func, tests):
|
|
|
|
for args, result in tests.items():
|
|
|
|
self.assertEqual(func(*args), result)
|
2017-03-28 13:12:44 +02:00
|
|
|
|
|
|
|
|
2020-01-25 22:57:08 +01:00
|
|
|
class TestCookiesTxt(unittest.TestCase):
|
|
|
|
|
2022-05-06 13:21:29 +02:00
|
|
|
def test_cookiestxt_load(self):
|
2020-01-25 22:57:08 +01:00
|
|
|
|
|
|
|
def _assert(content, expected):
|
2022-05-06 13:21:29 +02:00
|
|
|
jar = http.cookiejar.CookieJar()
|
|
|
|
util.cookiestxt_load(io.StringIO(content, None), jar)
|
|
|
|
for c, e in zip(jar, expected):
|
2020-01-25 22:57:08 +01:00
|
|
|
self.assertEqual(c.__dict__, e.__dict__)
|
|
|
|
|
|
|
|
_assert("", [])
|
|
|
|
_assert("\n\n\n", [])
|
|
|
|
_assert("$ Comment", [])
|
|
|
|
_assert("# Comment", [])
|
|
|
|
_assert(" # Comment \n\n $ Comment ", [])
|
|
|
|
_assert(
|
|
|
|
".example.org\tTRUE\t/\tTRUE\t0\tname\tvalue",
|
|
|
|
[self._cookie("name", "value", ".example.org")],
|
|
|
|
)
|
|
|
|
_assert(
|
|
|
|
".example.org\tTRUE\t/\tTRUE\t\tname\t",
|
|
|
|
[self._cookie("name", "", ".example.org")],
|
|
|
|
)
|
2022-03-19 15:14:55 +01:00
|
|
|
_assert(
|
|
|
|
"\tTRUE\t/\tTRUE\t\tname\t",
|
|
|
|
[self._cookie("name", "", "")],
|
|
|
|
)
|
2020-01-25 22:57:08 +01:00
|
|
|
_assert(
|
|
|
|
"# Netscape HTTP Cookie File\n"
|
|
|
|
"\n"
|
|
|
|
"# default\n"
|
|
|
|
".example.org TRUE / FALSE 0 n1 v1\n"
|
|
|
|
".example.org TRUE / TRUE 2145945600 n2 v2\n"
|
|
|
|
".example.org TRUE /path FALSE 0 n3\n"
|
|
|
|
"\n"
|
|
|
|
" # # extra # # \n"
|
|
|
|
"www.example.org FALSE / FALSE n4 \n"
|
|
|
|
"www.example.org FALSE /path FALSE 100 n5 v5\n",
|
|
|
|
[
|
|
|
|
self._cookie(
|
|
|
|
"n1", "v1", ".example.org", True, "/", False),
|
|
|
|
self._cookie(
|
|
|
|
"n2", "v2", ".example.org", True, "/", True, 2145945600),
|
|
|
|
self._cookie(
|
|
|
|
"n3", None, ".example.org", True, "/path", False),
|
|
|
|
self._cookie(
|
|
|
|
"n4", "" , "www.example.org", False, "/", False),
|
|
|
|
self._cookie(
|
|
|
|
"n5", "v5", "www.example.org", False, "/path", False, 100),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
2022-05-06 13:21:29 +02:00
|
|
|
util.cookiestxt_load("example.org\tTRUE\t/\tTRUE\t0\tname",
|
|
|
|
http.cookiejar.CookieJar())
|
2020-01-25 22:57:08 +01:00
|
|
|
|
2022-05-06 13:21:29 +02:00
|
|
|
def test_cookiestxt_store(self):
|
2020-01-25 22:57:08 +01:00
|
|
|
|
|
|
|
def _assert(cookies, expected):
|
|
|
|
fp = io.StringIO(newline=None)
|
2022-05-06 13:21:29 +02:00
|
|
|
util.cookiestxt_store(fp, cookies)
|
2020-01-25 22:57:08 +01:00
|
|
|
self.assertMultiLineEqual(fp.getvalue(), expected)
|
|
|
|
|
|
|
|
_assert([], "# Netscape HTTP Cookie File\n\n")
|
|
|
|
_assert(
|
|
|
|
[self._cookie("name", "value", ".example.org")],
|
|
|
|
"# Netscape HTTP Cookie File\n\n"
|
|
|
|
".example.org\tTRUE\t/\tTRUE\t0\tname\tvalue\n",
|
|
|
|
)
|
|
|
|
_assert(
|
|
|
|
[
|
|
|
|
self._cookie(
|
|
|
|
"n1", "v1", ".example.org", True, "/", False),
|
|
|
|
self._cookie(
|
|
|
|
"n2", "v2", ".example.org", True, "/", True, 2145945600),
|
|
|
|
self._cookie(
|
|
|
|
"n3", None, ".example.org", True, "/path", False),
|
|
|
|
self._cookie(
|
|
|
|
"n4", "" , "www.example.org", False, "/", False),
|
|
|
|
self._cookie(
|
|
|
|
"n5", "v5", "www.example.org", False, "/path", False, 100),
|
2022-03-19 15:14:55 +01:00
|
|
|
self._cookie(
|
|
|
|
"n6", "v6", "", False),
|
2020-01-25 22:57:08 +01:00
|
|
|
],
|
|
|
|
"# Netscape HTTP Cookie File\n"
|
|
|
|
"\n"
|
|
|
|
".example.org TRUE / FALSE 0 n1 v1\n"
|
|
|
|
".example.org TRUE / TRUE 2145945600 n2 v2\n"
|
|
|
|
".example.org TRUE /path FALSE 0 n3\n"
|
|
|
|
"www.example.org FALSE / FALSE 0 n4 \n"
|
|
|
|
"www.example.org FALSE /path FALSE 100 n5 v5\n",
|
|
|
|
)
|
|
|
|
|
|
|
|
def _cookie(self, name, value, domain, domain_specified=True,
|
|
|
|
path="/", secure=True, expires=None):
|
|
|
|
return http.cookiejar.Cookie(
|
|
|
|
0, name, value, None, False,
|
|
|
|
domain, domain_specified, domain.startswith("."),
|
|
|
|
path, False, secure, expires, False, None, None, {},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-08-13 14:31:22 +02:00
|
|
|
class TestOther(unittest.TestCase):
|
|
|
|
|
2018-03-14 13:17:34 +01:00
|
|
|
def test_bencode(self):
|
|
|
|
self.assertEqual(util.bencode(0), "")
|
|
|
|
self.assertEqual(util.bencode(123), "123")
|
|
|
|
self.assertEqual(util.bencode(123, "01"), "1111011")
|
|
|
|
self.assertEqual(util.bencode(123, "BA"), "AAAABAA")
|
|
|
|
|
2017-08-13 14:31:22 +02:00
|
|
|
def test_bdecode(self):
|
|
|
|
self.assertEqual(util.bdecode(""), 0)
|
|
|
|
self.assertEqual(util.bdecode("123"), 123)
|
|
|
|
self.assertEqual(util.bdecode("1111011", "01"), 123)
|
|
|
|
self.assertEqual(util.bdecode("AAAABAA", "BA"), 123)
|
|
|
|
|
2018-03-14 13:17:34 +01:00
|
|
|
def test_bencode_bdecode(self):
|
|
|
|
for _ in range(100):
|
|
|
|
value = random.randint(0, 1000000)
|
|
|
|
for alphabet in ("01", "0123456789", string.ascii_letters):
|
|
|
|
result = util.bdecode(util.bencode(value, alphabet), alphabet)
|
|
|
|
self.assertEqual(result, value)
|
|
|
|
|
2017-12-03 01:38:24 +01:00
|
|
|
def test_advance(self):
|
|
|
|
items = range(5)
|
|
|
|
|
|
|
|
self.assertCountEqual(
|
|
|
|
util.advance(items, 0), items)
|
|
|
|
self.assertCountEqual(
|
|
|
|
util.advance(items, 3), range(3, 5))
|
|
|
|
self.assertCountEqual(
|
|
|
|
util.advance(items, 9), [])
|
|
|
|
self.assertCountEqual(
|
|
|
|
util.advance(util.advance(items, 1), 2), range(3, 5))
|
|
|
|
|
2020-10-29 23:26:11 +01:00
|
|
|
def test_unique(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
list(util.unique("")), "")
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
list(util.unique("AABBCC")), "ABC")
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
list(util.unique("ABABABCAABBCC")), "ABC")
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
list(util.unique([1, 2, 1, 3, 2, 1])), [1, 2, 3])
|
|
|
|
|
2021-03-02 23:01:38 +01:00
|
|
|
def test_unique_sequence(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
list(util.unique_sequence("")), "")
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
list(util.unique_sequence("AABBCC")), "ABC")
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
list(util.unique_sequence("ABABABCAABBCC")), "ABABABCABC")
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
list(util.unique_sequence([1, 2, 1, 3, 2, 1])), [1, 2, 1, 3, 2, 1])
|
|
|
|
|
2022-03-30 10:36:52 +02:00
|
|
|
def test_contains(self):
|
|
|
|
c = [1, "2", 3, 4, "5", "foo"]
|
|
|
|
self.assertTrue(util.contains(c, 1))
|
|
|
|
self.assertTrue(util.contains(c, "foo"))
|
|
|
|
self.assertTrue(util.contains(c, [1, 3, "5"]))
|
|
|
|
self.assertTrue(util.contains(c, ["a", "b", "5"]))
|
|
|
|
self.assertFalse(util.contains(c, "bar"))
|
|
|
|
self.assertFalse(util.contains(c, [2, 5, "bar"]))
|
|
|
|
|
2022-04-08 14:40:26 +02:00
|
|
|
s = "1 2 3 asd qwe y(+)c f(+)(-) bar"
|
2022-04-07 15:35:54 +02:00
|
|
|
self.assertTrue(util.contains(s, "y(+)c"))
|
2022-03-30 10:36:52 +02:00
|
|
|
self.assertTrue(util.contains(s, ["asd", "qwe", "yxc"]))
|
|
|
|
self.assertTrue(util.contains(s, ["sdf", "dfg", "qwe"]))
|
|
|
|
self.assertFalse(util.contains(s, "tag1"))
|
|
|
|
self.assertFalse(util.contains(s, ["tag1", "tag2", "tag3"]))
|
|
|
|
|
2022-04-08 14:40:26 +02:00
|
|
|
s = "1, 2, 3, asd, qwe, y(+)c, f(+)(-), bar"
|
|
|
|
self.assertTrue(util.contains(s, "y(+)c", ", "))
|
|
|
|
self.assertTrue(util.contains(s, ["sdf", "dfg", "qwe"], ", "))
|
|
|
|
self.assertFalse(util.contains(s, "tag1", ", "))
|
|
|
|
|
2018-04-12 17:07:12 +02:00
|
|
|
def test_raises(self):
|
2019-10-28 15:06:17 +01:00
|
|
|
func = util.raises(Exception)
|
2019-05-13 11:48:20 +02:00
|
|
|
with self.assertRaises(Exception):
|
|
|
|
func()
|
2018-04-12 17:07:12 +02:00
|
|
|
|
2019-10-28 15:06:17 +01:00
|
|
|
func = util.raises(ValueError)
|
2019-05-13 11:48:20 +02:00
|
|
|
with self.assertRaises(ValueError):
|
2019-10-28 15:06:17 +01:00
|
|
|
func(1)
|
2019-05-13 11:48:20 +02:00
|
|
|
with self.assertRaises(ValueError):
|
2019-10-28 15:06:17 +01:00
|
|
|
func(2)
|
2019-05-13 11:48:20 +02:00
|
|
|
with self.assertRaises(ValueError):
|
2019-10-28 15:06:17 +01:00
|
|
|
func(3)
|
2018-04-12 17:07:12 +02:00
|
|
|
|
2021-05-04 18:00:38 +02:00
|
|
|
def test_identity(self):
|
|
|
|
for value in (123, "foo", [1, 2, 3], (1, 2, 3), {1: 2}, None):
|
|
|
|
self.assertIs(util.identity(value), value)
|
|
|
|
|
|
|
|
def test_noop(self):
|
|
|
|
self.assertEqual(util.noop(), None)
|
|
|
|
|
2021-06-03 22:34:58 +02:00
|
|
|
def test_compile_expression(self):
|
|
|
|
expr = util.compile_expression("1 + 2 * 3")
|
|
|
|
self.assertEqual(expr(), 7)
|
|
|
|
self.assertEqual(expr({"a": 1, "b": 2, "c": 3}), 7)
|
|
|
|
self.assertEqual(expr({"a": 9, "b": 9, "c": 9}), 7)
|
|
|
|
|
|
|
|
expr = util.compile_expression("a + b * c")
|
|
|
|
self.assertEqual(expr({"a": 1, "b": 2, "c": 3}), 7)
|
|
|
|
self.assertEqual(expr({"a": 9, "b": 9, "c": 9}), 90)
|
|
|
|
|
|
|
|
with self.assertRaises(NameError):
|
|
|
|
expr()
|
|
|
|
with self.assertRaises(NameError):
|
|
|
|
expr({"a": 2})
|
|
|
|
|
|
|
|
with self.assertRaises(SyntaxError):
|
|
|
|
util.compile_expression("")
|
|
|
|
with self.assertRaises(SyntaxError):
|
|
|
|
util.compile_expression("x++")
|
|
|
|
|
|
|
|
expr = util.compile_expression("1 and abort()")
|
|
|
|
with self.assertRaises(exception.StopExtraction):
|
|
|
|
expr()
|
|
|
|
|
2021-12-18 23:04:33 +01:00
|
|
|
def test_build_duration_func(self, f=util.build_duration_func):
|
|
|
|
|
|
|
|
def test_single(df, v):
|
|
|
|
for _ in range(10):
|
|
|
|
self.assertEqual(df(), v)
|
|
|
|
|
|
|
|
def test_range(df, lower, upper):
|
|
|
|
for __ in range(10):
|
|
|
|
v = df()
|
|
|
|
self.assertGreaterEqual(v, lower)
|
|
|
|
self.assertLessEqual(v, upper)
|
|
|
|
|
2022-04-27 20:28:14 +02:00
|
|
|
for v in (0, 0.0, "", None, (), []):
|
|
|
|
self.assertIsNone(f(v))
|
|
|
|
|
|
|
|
for v in (0, 0.0, "", None, (), []):
|
|
|
|
test_single(f(v, 1.0), 1.0)
|
|
|
|
|
2021-12-18 23:04:33 +01:00
|
|
|
test_single(f(3), 3)
|
|
|
|
test_single(f(3.0), 3.0)
|
|
|
|
test_single(f("3"), 3)
|
|
|
|
test_single(f("3.0-"), 3)
|
|
|
|
test_single(f(" 3 -"), 3)
|
|
|
|
|
|
|
|
test_range(f((2, 4)), 2, 4)
|
|
|
|
test_range(f([2, 4]), 2, 4)
|
|
|
|
test_range(f("2-4"), 2, 4)
|
|
|
|
test_range(f(" 2.0 - 4 "), 2, 4)
|
|
|
|
|
2021-11-23 19:23:02 +01:00
|
|
|
def test_extractor_filter(self):
|
|
|
|
# empty
|
|
|
|
func = util.build_extractor_filter("")
|
|
|
|
self.assertEqual(func(TestExtractor) , True)
|
|
|
|
self.assertEqual(func(TestExtractorParent), True)
|
|
|
|
self.assertEqual(func(TestExtractorAlt) , True)
|
|
|
|
|
|
|
|
# category
|
|
|
|
func = util.build_extractor_filter("test_category")
|
|
|
|
self.assertEqual(func(TestExtractor) , False)
|
|
|
|
self.assertEqual(func(TestExtractorParent), False)
|
|
|
|
self.assertEqual(func(TestExtractorAlt) , True)
|
|
|
|
|
|
|
|
# subcategory
|
|
|
|
func = util.build_extractor_filter("*:test_subcategory")
|
|
|
|
self.assertEqual(func(TestExtractor) , False)
|
|
|
|
self.assertEqual(func(TestExtractorParent), True)
|
|
|
|
self.assertEqual(func(TestExtractorAlt) , False)
|
|
|
|
|
|
|
|
# basecategory
|
|
|
|
func = util.build_extractor_filter("test_basecategory")
|
|
|
|
self.assertEqual(func(TestExtractor) , False)
|
|
|
|
self.assertEqual(func(TestExtractorParent), False)
|
|
|
|
self.assertEqual(func(TestExtractorAlt) , False)
|
|
|
|
|
|
|
|
# category-subcategory pair
|
|
|
|
func = util.build_extractor_filter("test_category:test_subcategory")
|
|
|
|
self.assertEqual(func(TestExtractor) , False)
|
|
|
|
self.assertEqual(func(TestExtractorParent), True)
|
|
|
|
self.assertEqual(func(TestExtractorAlt) , True)
|
|
|
|
|
|
|
|
# combination
|
|
|
|
func = util.build_extractor_filter(
|
|
|
|
["test_category", "*:test_subcategory"])
|
|
|
|
self.assertEqual(func(TestExtractor) , False)
|
|
|
|
self.assertEqual(func(TestExtractorParent), False)
|
|
|
|
self.assertEqual(func(TestExtractorAlt) , False)
|
|
|
|
|
|
|
|
# whitelist
|
|
|
|
func = util.build_extractor_filter(
|
|
|
|
"test_category:test_subcategory", negate=False)
|
|
|
|
self.assertEqual(func(TestExtractor) , True)
|
|
|
|
self.assertEqual(func(TestExtractorParent), False)
|
|
|
|
self.assertEqual(func(TestExtractorAlt) , False)
|
|
|
|
|
|
|
|
func = util.build_extractor_filter(
|
|
|
|
["test_category:test_subcategory", "*:test_subcategory_parent"],
|
|
|
|
negate=False)
|
|
|
|
self.assertEqual(func(TestExtractor) , True)
|
|
|
|
self.assertEqual(func(TestExtractorParent), True)
|
|
|
|
self.assertEqual(func(TestExtractorAlt) , False)
|
|
|
|
|
2021-01-11 22:12:40 +01:00
|
|
|
def test_generate_token(self):
|
|
|
|
tokens = set()
|
|
|
|
for _ in range(100):
|
|
|
|
token = util.generate_token()
|
|
|
|
tokens.add(token)
|
|
|
|
self.assertEqual(len(token), 16 * 2)
|
|
|
|
self.assertRegex(token, r"^[0-9a-f]+$")
|
|
|
|
self.assertGreaterEqual(len(tokens), 99)
|
|
|
|
|
|
|
|
token = util.generate_token(80)
|
|
|
|
self.assertEqual(len(token), 80 * 2)
|
|
|
|
self.assertRegex(token, r"^[0-9a-f]+$")
|
|
|
|
|
2021-07-26 01:55:02 +02:00
|
|
|
def test_format_value(self):
|
2021-09-28 23:07:55 +02:00
|
|
|
self.assertEqual(util.format_value(0) , "0")
|
|
|
|
self.assertEqual(util.format_value(1) , "1")
|
|
|
|
self.assertEqual(util.format_value(12) , "12")
|
|
|
|
self.assertEqual(util.format_value(123) , "123")
|
|
|
|
self.assertEqual(util.format_value(1234) , "1.23k")
|
|
|
|
self.assertEqual(util.format_value(12345) , "12.34k")
|
|
|
|
self.assertEqual(util.format_value(123456) , "123.45k")
|
|
|
|
self.assertEqual(util.format_value(1234567) , "1.23M")
|
|
|
|
self.assertEqual(util.format_value(12345678) , "12.34M")
|
|
|
|
self.assertEqual(util.format_value(123456789) , "123.45M")
|
|
|
|
self.assertEqual(util.format_value(1234567890), "1.23G")
|
2021-07-26 01:55:02 +02:00
|
|
|
|
2017-08-13 14:31:22 +02:00
|
|
|
def test_combine_dict(self):
|
|
|
|
self.assertEqual(
|
|
|
|
util.combine_dict({}, {}),
|
|
|
|
{})
|
|
|
|
self.assertEqual(
|
|
|
|
util.combine_dict({1: 1, 2: 2}, {2: 4, 4: 8}),
|
|
|
|
{1: 1, 2: 4, 4: 8})
|
|
|
|
self.assertEqual(
|
|
|
|
util.combine_dict(
|
|
|
|
{1: {11: 22, 12: 24}, 2: {13: 26, 14: 28}},
|
|
|
|
{1: {11: 33, 13: 39}, 2: "str"}),
|
|
|
|
{1: {11: 33, 12: 24, 13: 39}, 2: "str"})
|
|
|
|
self.assertEqual(
|
|
|
|
util.combine_dict(
|
|
|
|
{1: {2: {3: {4: {"1": "a", "2": "b"}}}}},
|
|
|
|
{1: {2: {3: {4: {"1": "A", "3": "C"}}}}}),
|
|
|
|
{1: {2: {3: {4: {"1": "A", "2": "b", "3": "C"}}}}})
|
|
|
|
|
2019-02-14 11:15:19 +01:00
|
|
|
def test_transform_dict(self):
|
|
|
|
d = {}
|
|
|
|
util.transform_dict(d, str)
|
|
|
|
self.assertEqual(d, {})
|
|
|
|
|
|
|
|
d = {1: 123, 2: "123", 3: True, 4: None}
|
|
|
|
util.transform_dict(d, str)
|
|
|
|
self.assertEqual(
|
|
|
|
d, {1: "123", 2: "123", 3: "True", 4: "None"})
|
|
|
|
|
|
|
|
d = {1: 123, 2: "123", 3: "foo", 4: {11: 321, 12: "321", 13: "bar"}}
|
|
|
|
util.transform_dict(d, text.parse_int)
|
|
|
|
self.assertEqual(
|
|
|
|
d, {1: 123, 2: 123, 3: 0, 4: {11: 321, 12: 321, 13: 0}})
|
|
|
|
|
2019-11-21 16:57:39 +01:00
|
|
|
def test_filter_dict(self):
|
|
|
|
d = {}
|
|
|
|
r = util.filter_dict(d)
|
|
|
|
self.assertEqual(r, d)
|
|
|
|
self.assertIsNot(r, d)
|
|
|
|
|
|
|
|
d = {"foo": 123, "bar": [], "baz": None}
|
|
|
|
r = util.filter_dict(d)
|
|
|
|
self.assertEqual(r, d)
|
|
|
|
self.assertIsNot(r, d)
|
|
|
|
|
|
|
|
d = {"foo": 123, "_bar": [], "__baz__": None}
|
|
|
|
r = util.filter_dict(d)
|
|
|
|
self.assertEqual(r, {"foo": 123})
|
|
|
|
|
2022-08-01 14:08:44 +02:00
|
|
|
def test_enumerate_reversed(self):
|
|
|
|
|
|
|
|
seq = [11, 22, 33]
|
|
|
|
result = [(3, 33), (2, 22), (1, 11)]
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
for i in seq:
|
|
|
|
yield i
|
|
|
|
|
|
|
|
def gen_2():
|
|
|
|
yield from seq
|
|
|
|
|
|
|
|
def assertEqual(it1, it2):
|
|
|
|
ae = self.assertEqual
|
|
|
|
for i1, i2 in zip(it1, it2, strict=True):
|
|
|
|
ae(i1, i2)
|
|
|
|
|
|
|
|
assertEqual(
|
|
|
|
util.enumerate_reversed(seq), [(2, 33), (1, 22), (0, 11)])
|
|
|
|
assertEqual(
|
|
|
|
util.enumerate_reversed(seq, 1), result)
|
|
|
|
assertEqual(
|
|
|
|
util.enumerate_reversed(seq, 2), [(4, 33), (3, 22), (2, 11)])
|
|
|
|
|
|
|
|
assertEqual(
|
|
|
|
util.enumerate_reversed(gen(), 0, len(seq)),
|
|
|
|
[(2, 33), (1, 22), (0, 11)])
|
|
|
|
assertEqual(
|
|
|
|
util.enumerate_reversed(gen(), 1, len(seq)), result)
|
|
|
|
assertEqual(
|
|
|
|
util.enumerate_reversed(gen_2(), 1, len(seq)), result)
|
|
|
|
assertEqual(
|
|
|
|
util.enumerate_reversed(gen_2(), 2, len(seq)),
|
|
|
|
[(4, 33), (3, 22), (2, 11)])
|
|
|
|
|
2019-02-14 11:15:19 +01:00
|
|
|
def test_number_to_string(self, f=util.number_to_string):
|
|
|
|
self.assertEqual(f(1) , "1")
|
|
|
|
self.assertEqual(f(1.0) , "1.0")
|
|
|
|
self.assertEqual(f("1.0") , "1.0")
|
|
|
|
self.assertEqual(f([1]) , [1])
|
|
|
|
self.assertEqual(f({1: 2}), {1: 2})
|
|
|
|
self.assertEqual(f(True) , True)
|
|
|
|
self.assertEqual(f(None) , None)
|
|
|
|
|
2019-03-06 19:46:54 +01:00
|
|
|
def test_to_string(self, f=util.to_string):
|
|
|
|
self.assertEqual(f(1) , "1")
|
|
|
|
self.assertEqual(f(1.0) , "1.0")
|
|
|
|
self.assertEqual(f("1.0"), "1.0")
|
|
|
|
|
|
|
|
self.assertEqual(f("") , "")
|
|
|
|
self.assertEqual(f(None) , "")
|
|
|
|
self.assertEqual(f(0) , "")
|
|
|
|
|
|
|
|
self.assertEqual(f(["a"]), "a")
|
|
|
|
self.assertEqual(f([1]) , "1")
|
|
|
|
self.assertEqual(f(["a", "b", "c"]), "a, b, c")
|
|
|
|
self.assertEqual(f([1, 2, 3]), "1, 2, 3")
|
|
|
|
|
2022-03-23 22:20:37 +01:00
|
|
|
def test_datetime_to_timestamp(self, f=util.datetime_to_timestamp):
|
|
|
|
self.assertEqual(f(util.EPOCH), 0.0)
|
|
|
|
self.assertEqual(f(datetime.datetime(2010, 1, 1)), 1262304000.0)
|
|
|
|
self.assertEqual(f(datetime.datetime(2010, 1, 1, 0, 0, 0, 128000)),
|
|
|
|
1262304000.128000)
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
f(None)
|
|
|
|
|
|
|
|
def test_datetime_to_timestamp_string(
|
|
|
|
self, f=util.datetime_to_timestamp_string):
|
2021-06-25 22:35:45 +02:00
|
|
|
self.assertEqual(f(util.EPOCH), "0")
|
|
|
|
self.assertEqual(f(datetime.datetime(2010, 1, 1)), "1262304000")
|
|
|
|
self.assertEqual(f(None), "")
|
|
|
|
|
2019-02-14 11:15:19 +01:00
|
|
|
def test_universal_none(self):
|
|
|
|
obj = util.NONE
|
|
|
|
|
|
|
|
self.assertFalse(obj)
|
|
|
|
self.assertEqual(str(obj), str(None))
|
|
|
|
self.assertEqual(repr(obj), repr(None))
|
|
|
|
self.assertIs(obj.attr, obj)
|
|
|
|
self.assertIs(obj["key"], obj)
|
|
|
|
|
2017-08-13 14:31:22 +02:00
|
|
|
|
2021-11-23 19:23:02 +01:00
|
|
|
class TestExtractor():
|
|
|
|
category = "test_category"
|
|
|
|
subcategory = "test_subcategory"
|
|
|
|
basecategory = "test_basecategory"
|
|
|
|
|
|
|
|
|
|
|
|
class TestExtractorParent(TestExtractor):
|
|
|
|
category = "test_category"
|
|
|
|
subcategory = "test_subcategory_parent"
|
|
|
|
|
|
|
|
|
|
|
|
class TestExtractorAlt(TestExtractor):
|
|
|
|
category = "test_category_alt"
|
|
|
|
subcategory = "test_subcategory"
|
|
|
|
|
|
|
|
|
2017-03-28 13:12:44 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|