2015-10-03 14:51:13 +02:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2022-08-23 21:14:55 +02:00
|
|
|
|
# Copyright 2015-2022 Mike Fährmann
|
2015-10-03 14:51:13 +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
|
|
|
|
|
import sys
|
2015-10-03 14:51:13 +02:00
|
|
|
|
import unittest
|
2020-05-02 01:15:50 +02:00
|
|
|
|
|
2019-04-21 15:28:27 +02:00
|
|
|
|
import datetime
|
2018-04-14 20:56:21 +02:00
|
|
|
|
|
2020-05-02 01:15:50 +02:00
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2024-09-19 20:09:03 +02:00
|
|
|
|
from gallery_dl import text, util # noqa E402
|
2015-10-03 14:51:13 +02:00
|
|
|
|
|
2017-01-30 19:40:15 +01:00
|
|
|
|
|
2018-04-14 22:09:42 +02:00
|
|
|
|
INVALID = ((), [], {}, None, 1, 2.3)
|
2018-04-20 14:53:21 +02:00
|
|
|
|
INVALID_ALT = ((), [], {}, None, "")
|
2018-04-14 22:09:42 +02:00
|
|
|
|
|
|
|
|
|
|
2015-10-03 14:51:13 +02:00
|
|
|
|
class TestText(unittest.TestCase):
|
|
|
|
|
|
2018-04-14 22:09:42 +02:00
|
|
|
|
def test_remove_html(self, f=text.remove_html):
|
2015-10-03 14:51:13 +02:00
|
|
|
|
result = "Hello World."
|
2018-04-14 22:09:42 +02:00
|
|
|
|
|
|
|
|
|
# standard usage
|
|
|
|
|
self.assertEqual(f(""), "")
|
|
|
|
|
self.assertEqual(f("Hello World."), result)
|
|
|
|
|
self.assertEqual(f(" Hello World. "), result)
|
|
|
|
|
self.assertEqual(f("Hello<br/>World."), result)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("<div><b class='a'>Hello</b><i>World.</i></div>"), result)
|
|
|
|
|
|
|
|
|
|
# empty HTML
|
|
|
|
|
self.assertEqual(f("<div></div>"), "")
|
|
|
|
|
self.assertEqual(f(" <div> </div> "), "")
|
|
|
|
|
|
|
|
|
|
# malformed HTML
|
|
|
|
|
self.assertEqual(f("<div</div>"), "")
|
|
|
|
|
self.assertEqual(f("<div<Hello World.</div>"), "")
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value), "")
|
|
|
|
|
|
2018-05-27 15:00:41 +02:00
|
|
|
|
def test_split_html(self, f=text.split_html):
|
|
|
|
|
result = ["Hello", "World."]
|
|
|
|
|
empty = []
|
|
|
|
|
|
|
|
|
|
# standard usage
|
|
|
|
|
self.assertEqual(f(""), empty)
|
|
|
|
|
self.assertEqual(f("Hello World."), ["Hello World."])
|
2019-01-29 13:14:30 +01:00
|
|
|
|
self.assertEqual(f(" Hello World. "), ["Hello World."])
|
2018-05-27 15:00:41 +02:00
|
|
|
|
self.assertEqual(f("Hello<br/>World."), result)
|
2019-01-29 13:14:30 +01:00
|
|
|
|
self.assertEqual(f(" Hello <br/> World. "), result)
|
2018-05-27 15:00:41 +02:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("<div><b class='a'>Hello</b><i>World.</i></div>"), result)
|
|
|
|
|
|
2021-03-29 02:12:29 +02:00
|
|
|
|
# escaped HTML entities
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("<i><foo></i> <i><bar> </i>"), ["<foo>", "<bar>"])
|
|
|
|
|
|
2018-05-27 15:00:41 +02:00
|
|
|
|
# empty HTML
|
|
|
|
|
self.assertEqual(f("<div></div>"), empty)
|
|
|
|
|
self.assertEqual(f(" <div> </div> "), empty)
|
|
|
|
|
|
|
|
|
|
# malformed HTML
|
|
|
|
|
self.assertEqual(f("<div</div>"), empty)
|
|
|
|
|
self.assertEqual(f("<div<Hello World.</div>"), empty)
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value), empty)
|
|
|
|
|
|
2022-08-23 21:14:55 +02:00
|
|
|
|
def test_slugify(self, f=text.slugify):
|
|
|
|
|
self.assertEqual(f("Hello World"), "hello-world")
|
|
|
|
|
self.assertEqual(f("-HeLLo---World-"), "hello-world")
|
|
|
|
|
self.assertEqual(f("_-H#e:l#l:o+\t+W?o!rl=d-_"), "hello-world")
|
|
|
|
|
self.assertEqual(f("_Hello_World_"), "hello_world")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(f(""), "")
|
|
|
|
|
self.assertEqual(f("-"), "")
|
|
|
|
|
self.assertEqual(f("--"), "")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(f(()), "")
|
|
|
|
|
self.assertEqual(f([]), "")
|
|
|
|
|
self.assertEqual(f({}), "")
|
|
|
|
|
self.assertEqual(f(None), "none")
|
|
|
|
|
self.assertEqual(f(1), "1")
|
|
|
|
|
self.assertEqual(f(2.3), "23")
|
|
|
|
|
|
2020-05-19 21:25:07 +02:00
|
|
|
|
def test_ensure_http_scheme(self, f=text.ensure_http_scheme):
|
|
|
|
|
result = "https://example.org/filename.ext"
|
|
|
|
|
|
|
|
|
|
# standard usage
|
|
|
|
|
self.assertEqual(f(""), "")
|
|
|
|
|
self.assertEqual(f("example.org/filename.ext"), result)
|
|
|
|
|
self.assertEqual(f("/example.org/filename.ext"), result)
|
|
|
|
|
self.assertEqual(f("//example.org/filename.ext"), result)
|
|
|
|
|
self.assertEqual(f("://example.org/filename.ext"), result)
|
|
|
|
|
|
|
|
|
|
# no change
|
|
|
|
|
self.assertEqual(f(result), result)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("http://example.org/filename.ext"),
|
|
|
|
|
"http://example.org/filename.ext",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# ...
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("htp://example.org/filename.ext"),
|
|
|
|
|
"https://htp://example.org/filename.ext",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID_ALT:
|
|
|
|
|
self.assertEqual(f(value), value)
|
|
|
|
|
|
2022-03-01 02:56:51 +01:00
|
|
|
|
def test_root_from_url(self, f=text.root_from_url):
|
|
|
|
|
result = "https://example.org"
|
2024-02-29 15:05:46 +01:00
|
|
|
|
self.assertEqual(f("https://example.org") , result)
|
2022-03-01 02:56:51 +01:00
|
|
|
|
self.assertEqual(f("https://example.org/") , result)
|
|
|
|
|
self.assertEqual(f("https://example.org/path"), result)
|
|
|
|
|
self.assertEqual(f("example.org/") , result)
|
|
|
|
|
self.assertEqual(f("example.org/path/") , result)
|
|
|
|
|
|
|
|
|
|
result = "http://example.org"
|
2024-02-29 15:05:46 +01:00
|
|
|
|
self.assertEqual(f("http://example.org") , result)
|
2022-03-01 02:56:51 +01:00
|
|
|
|
self.assertEqual(f("http://example.org/") , result)
|
|
|
|
|
self.assertEqual(f("http://example.org/path/"), result)
|
|
|
|
|
self.assertEqual(f("example.org/", "http://") , result)
|
|
|
|
|
|
2018-04-14 22:09:42 +02:00
|
|
|
|
def test_filename_from_url(self, f=text.filename_from_url):
|
2015-10-03 14:51:13 +02:00
|
|
|
|
result = "filename.ext"
|
2015-11-16 02:20:22 +01:00
|
|
|
|
|
2018-04-14 22:09:42 +02:00
|
|
|
|
# standard usage
|
|
|
|
|
self.assertEqual(f(""), "")
|
|
|
|
|
self.assertEqual(f("filename.ext"), result)
|
|
|
|
|
self.assertEqual(f("/filename.ext"), result)
|
|
|
|
|
self.assertEqual(f("example.org/filename.ext"), result)
|
2019-01-31 12:23:25 +01:00
|
|
|
|
self.assertEqual(f("http://example.org/v2/filename.ext"), result)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("http://example.org/v2/filename.ext?param=value#frag"), result)
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value), "")
|
|
|
|
|
|
|
|
|
|
def test_ext_from_url(self, f=text.ext_from_url):
|
|
|
|
|
result = "ext"
|
|
|
|
|
|
|
|
|
|
# standard usage
|
|
|
|
|
self.assertEqual(f(""), "")
|
2021-03-28 03:54:12 +02:00
|
|
|
|
self.assertEqual(f("filename"), "")
|
2019-01-31 12:23:25 +01:00
|
|
|
|
self.assertEqual(f("filename.ext"), result)
|
2021-03-28 03:54:12 +02:00
|
|
|
|
self.assertEqual(f("/filename.ExT"), result)
|
2019-01-31 12:23:25 +01:00
|
|
|
|
self.assertEqual(f("example.org/filename.ext"), result)
|
2018-04-14 22:09:42 +02:00
|
|
|
|
self.assertEqual(f("http://example.org/v2/filename.ext"), result)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("http://example.org/v2/filename.ext?param=value#frag"), result)
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value), "")
|
|
|
|
|
|
|
|
|
|
def test_nameext_from_url(self, f=text.nameext_from_url):
|
2019-02-14 16:07:17 +01:00
|
|
|
|
empty = {"filename": "", "extension": ""}
|
|
|
|
|
result = {"filename": "filename", "extension": "ext"}
|
2018-04-14 22:09:42 +02:00
|
|
|
|
|
|
|
|
|
# standard usage
|
|
|
|
|
self.assertEqual(f(""), empty)
|
|
|
|
|
self.assertEqual(f("filename.ext"), result)
|
2021-03-28 03:54:12 +02:00
|
|
|
|
self.assertEqual(f("/filename.ExT"), result)
|
2018-04-14 22:09:42 +02:00
|
|
|
|
self.assertEqual(f("example.org/filename.ext"), result)
|
|
|
|
|
self.assertEqual(f("http://example.org/v2/filename.ext"), result)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("http://example.org/v2/filename.ext?param=value#frag"), result)
|
|
|
|
|
|
2021-05-02 21:15:50 +02:00
|
|
|
|
# long "extension"
|
|
|
|
|
fn = "httpswww.example.orgpath-path-path-path-path-path-path-path"
|
|
|
|
|
self.assertEqual(f(fn), {"filename": fn, "extension": ""})
|
|
|
|
|
|
2018-04-14 22:09:42 +02:00
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value), empty)
|
|
|
|
|
|
|
|
|
|
def test_extract(self, f=text.extract):
|
|
|
|
|
txt = "<a><b>"
|
2019-05-28 21:03:41 +02:00
|
|
|
|
self.assertEqual(f(txt, "<", ">"), ("a" , 3))
|
2018-04-14 22:09:42 +02:00
|
|
|
|
self.assertEqual(f(txt, "X", ">"), (None, 0))
|
|
|
|
|
self.assertEqual(f(txt, "<", "X"), (None, 0))
|
|
|
|
|
|
|
|
|
|
# 'pos' argument
|
|
|
|
|
for i in range(1, 4):
|
|
|
|
|
self.assertEqual(f(txt, "<", ">", i), ("b", 6))
|
|
|
|
|
for i in range(4, 10):
|
|
|
|
|
self.assertEqual(f(txt, "<", ">", i), (None, i))
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
2019-05-28 21:03:41 +02:00
|
|
|
|
self.assertEqual(f(value, "<" , ">") , (None, 0))
|
|
|
|
|
self.assertEqual(f(txt , value, ">") , (None, 0))
|
|
|
|
|
self.assertEqual(f(txt , "<" , value), (None, 0))
|
|
|
|
|
|
2022-11-04 21:37:36 +01:00
|
|
|
|
def test_extr(self, f=text.extr):
|
|
|
|
|
txt = "<a><b>"
|
|
|
|
|
self.assertEqual(f(txt, "X", ">"), "")
|
|
|
|
|
self.assertEqual(f(txt, "<", "X"), "")
|
|
|
|
|
self.assertEqual(f(txt, "<", ">"), "a")
|
|
|
|
|
self.assertEqual(f(txt, "><", ">"), "b")
|
|
|
|
|
|
2022-11-09 11:00:32 +01:00
|
|
|
|
# 'default' argument
|
|
|
|
|
self.assertEqual(f(txt, "<", "X", None), None)
|
|
|
|
|
self.assertEqual(f(txt, "<", "X", default=None), None)
|
|
|
|
|
self.assertEqual(f(txt, "<", "X", default=()), ())
|
|
|
|
|
|
2022-11-04 21:37:36 +01:00
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value, "<" , ">") , "")
|
|
|
|
|
self.assertEqual(f(txt , value, ">") , "")
|
|
|
|
|
self.assertEqual(f(txt , "<" , value), "")
|
|
|
|
|
|
2019-05-28 21:03:41 +02:00
|
|
|
|
def test_rextract(self, f=text.rextract):
|
|
|
|
|
txt = "<a><b>"
|
|
|
|
|
self.assertEqual(f(txt, "<", ">"), ("b" , 3))
|
|
|
|
|
self.assertEqual(f(txt, "X", ">"), (None, -1))
|
|
|
|
|
self.assertEqual(f(txt, "<", "X"), (None, -1))
|
|
|
|
|
|
|
|
|
|
# 'pos' argument
|
|
|
|
|
for i in range(10, 3, -1):
|
|
|
|
|
self.assertEqual(f(txt, "<", ">", i), ("b", 3))
|
|
|
|
|
for i in range(3, 0, -1):
|
|
|
|
|
self.assertEqual(f(txt, "<", ">", i), ("a", 0))
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value, "<" , ">") , (None, -1))
|
|
|
|
|
self.assertEqual(f(txt , value, ">") , (None, -1))
|
|
|
|
|
self.assertEqual(f(txt , "<" , value), (None, -1))
|
2018-04-14 22:09:42 +02:00
|
|
|
|
|
|
|
|
|
def test_extract_all(self, f=text.extract_all):
|
2015-11-28 01:47:17 +01:00
|
|
|
|
txt = "[c][b][a]: xyz! [d][e"
|
|
|
|
|
|
2018-04-13 19:21:32 +02:00
|
|
|
|
self.assertEqual(
|
2018-04-14 22:09:42 +02:00
|
|
|
|
f(txt, ()), ({}, 0))
|
2018-04-13 19:21:32 +02:00
|
|
|
|
self.assertEqual(
|
2018-04-14 22:09:42 +02:00
|
|
|
|
f(txt, (("C", "[", "]"), ("B", "[", "]"), ("A", "[", "]"))),
|
|
|
|
|
({"A": "a", "B": "b", "C": "c"}, 9),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 'None' as field name
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f(txt, ((None, "[", "]"), (None, "[", "]"), ("A", "[", "]"))),
|
|
|
|
|
({"A": "a"}, 9),
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f(txt, ((None, "[", "]"), (None, "[", "]"), (None, "[", "]"))),
|
|
|
|
|
({}, 9),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# failed matches
|
2018-04-13 19:21:32 +02:00
|
|
|
|
self.assertEqual(
|
2018-04-14 22:09:42 +02:00
|
|
|
|
f(txt, (("C", "[", "]"), ("X", "X", "X"), ("B", "[", "]"))),
|
|
|
|
|
({"B": "b", "C": "c", "X": None}, 6),
|
|
|
|
|
)
|
2018-04-13 19:21:32 +02:00
|
|
|
|
|
2018-04-14 22:09:42 +02:00
|
|
|
|
# 'pos' argument
|
2018-04-13 19:21:32 +02:00
|
|
|
|
self.assertEqual(
|
2018-04-14 22:09:42 +02:00
|
|
|
|
f(txt, (("B", "[", "]"), ("A", "[", "]")), pos=1),
|
|
|
|
|
({"A": "a", "B": "b"}, 9),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 'values' argument
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f(txt, (("C", "[", "]"),), values={"A": "a", "B": "b"}),
|
|
|
|
|
({"A": "a", "B": "b", "C": "c"}, 3),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
vdict = {}
|
|
|
|
|
rdict, pos = f(txt, (), values=vdict)
|
|
|
|
|
self.assertIs(vdict, rdict)
|
|
|
|
|
|
|
|
|
|
def test_extract_iter(self, f=text.extract_iter):
|
|
|
|
|
txt = "[c][b][a]: xyz! [d][e"
|
|
|
|
|
|
|
|
|
|
def g(*args):
|
|
|
|
|
return list(f(*args))
|
|
|
|
|
|
2018-04-13 19:21:32 +02:00
|
|
|
|
self.assertEqual(
|
2018-04-14 22:09:42 +02:00
|
|
|
|
g("", "[", "]"), [])
|
2018-04-13 19:21:32 +02:00
|
|
|
|
self.assertEqual(
|
2018-04-14 22:09:42 +02:00
|
|
|
|
g("[a]", "[", "]"), ["a"])
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
g(txt, "[", "]"), ["c", "b", "a", "d"])
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
g(txt, "X", "X"), [])
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
g(txt, "[", "]", 6), ["a", "d"])
|
|
|
|
|
|
2019-04-19 22:30:11 +02:00
|
|
|
|
def test_extract_from(self, f=text.extract_from):
|
|
|
|
|
txt = "[c][b][a]: xyz! [d][e"
|
|
|
|
|
|
|
|
|
|
e = f(txt)
|
|
|
|
|
self.assertEqual(e("[", "]"), "c")
|
|
|
|
|
self.assertEqual(e("[", "]"), "b")
|
|
|
|
|
self.assertEqual(e("[", "]"), "a")
|
|
|
|
|
self.assertEqual(e("[", "]"), "d")
|
|
|
|
|
self.assertEqual(e("[", "]"), "")
|
|
|
|
|
self.assertEqual(e("[", "]"), "")
|
|
|
|
|
|
|
|
|
|
e = f(txt, pos=6, default="END")
|
|
|
|
|
self.assertEqual(e("[", "]"), "a")
|
|
|
|
|
self.assertEqual(e("[", "]"), "d")
|
|
|
|
|
self.assertEqual(e("[", "]"), "END")
|
|
|
|
|
self.assertEqual(e("[", "]"), "END")
|
|
|
|
|
|
2019-06-16 21:46:26 +02:00
|
|
|
|
def test_parse_unicode_escapes(self, f=text.parse_unicode_escapes):
|
|
|
|
|
self.assertEqual(f(""), "")
|
|
|
|
|
self.assertEqual(f("foobar"), "foobar")
|
|
|
|
|
self.assertEqual(f("foo’bar"), "foo’bar")
|
|
|
|
|
self.assertEqual(f("foo\\u2019bar"), "foo’bar")
|
|
|
|
|
self.assertEqual(f("foo\\u201bar"), "foo‛ar")
|
|
|
|
|
self.assertEqual(f("foo\\u201zar"), "foo\\u201zar")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("\\u2018foo\\u2019\\u2020bar\\u00ff"),
|
|
|
|
|
"‘foo’†barÿ",
|
|
|
|
|
)
|
|
|
|
|
|
2018-04-20 14:53:21 +02:00
|
|
|
|
def test_parse_bytes(self, f=text.parse_bytes):
|
|
|
|
|
self.assertEqual(f("0"), 0)
|
|
|
|
|
self.assertEqual(f("50"), 50)
|
|
|
|
|
self.assertEqual(f("50k"), 50 * 1024**1)
|
|
|
|
|
self.assertEqual(f("50m"), 50 * 1024**2)
|
|
|
|
|
self.assertEqual(f("50g"), 50 * 1024**3)
|
|
|
|
|
self.assertEqual(f("50t"), 50 * 1024**4)
|
|
|
|
|
self.assertEqual(f("50p"), 50 * 1024**5)
|
|
|
|
|
|
|
|
|
|
# fractions
|
|
|
|
|
self.assertEqual(f("123.456"), 123)
|
|
|
|
|
self.assertEqual(f("123.567"), 124)
|
|
|
|
|
self.assertEqual(f("0.5M"), round(0.5 * 1024**2))
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID_ALT:
|
|
|
|
|
self.assertEqual(f(value), 0)
|
|
|
|
|
self.assertEqual(f("NaN"), 0)
|
|
|
|
|
self.assertEqual(f("invalid"), 0)
|
|
|
|
|
self.assertEqual(f(" 123 kb "), 0)
|
|
|
|
|
|
|
|
|
|
def test_parse_int(self, f=text.parse_int):
|
|
|
|
|
self.assertEqual(f(0), 0)
|
|
|
|
|
self.assertEqual(f("0"), 0)
|
|
|
|
|
self.assertEqual(f(123), 123)
|
|
|
|
|
self.assertEqual(f("123"), 123)
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID_ALT:
|
|
|
|
|
self.assertEqual(f(value), 0)
|
|
|
|
|
self.assertEqual(f("123.456"), 0)
|
|
|
|
|
self.assertEqual(f("zzz"), 0)
|
|
|
|
|
self.assertEqual(f([1, 2, 3]), 0)
|
|
|
|
|
self.assertEqual(f({1: 2, 3: 4}), 0)
|
|
|
|
|
|
|
|
|
|
# 'default' argument
|
|
|
|
|
default = "default"
|
|
|
|
|
for value in INVALID_ALT:
|
|
|
|
|
self.assertEqual(f(value, default), default)
|
|
|
|
|
self.assertEqual(f("zzz", default), default)
|
|
|
|
|
|
2019-01-29 13:14:30 +01:00
|
|
|
|
def test_parse_float(self, f=text.parse_float):
|
|
|
|
|
self.assertEqual(f(0), 0.0)
|
|
|
|
|
self.assertEqual(f("0"), 0.0)
|
|
|
|
|
self.assertEqual(f(123), 123.0)
|
|
|
|
|
self.assertEqual(f("123"), 123.0)
|
|
|
|
|
self.assertEqual(f(123.456), 123.456)
|
|
|
|
|
self.assertEqual(f("123.456"), 123.456)
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID_ALT:
|
|
|
|
|
self.assertEqual(f(value), 0.0)
|
|
|
|
|
self.assertEqual(f("zzz"), 0.0)
|
|
|
|
|
self.assertEqual(f([1, 2, 3]), 0.0)
|
|
|
|
|
self.assertEqual(f({1: 2, 3: 4}), 0.0)
|
|
|
|
|
|
|
|
|
|
# 'default' argument
|
|
|
|
|
default = "default"
|
|
|
|
|
for value in INVALID_ALT:
|
|
|
|
|
self.assertEqual(f(value, default), default)
|
|
|
|
|
self.assertEqual(f("zzz", default), default)
|
|
|
|
|
|
2018-04-14 22:09:42 +02:00
|
|
|
|
def test_parse_query(self, f=text.parse_query):
|
|
|
|
|
# standard usage
|
|
|
|
|
self.assertEqual(f(""), {})
|
|
|
|
|
self.assertEqual(f("foo=1"), {"foo": "1"})
|
|
|
|
|
self.assertEqual(f("foo=1&bar=2"), {"foo": "1", "bar": "2"})
|
|
|
|
|
|
|
|
|
|
# missing value
|
|
|
|
|
self.assertEqual(f("bar"), {})
|
|
|
|
|
self.assertEqual(f("foo=1&bar"), {"foo": "1"})
|
|
|
|
|
self.assertEqual(f("foo=1&bar&baz=3"), {"foo": "1", "baz": "3"})
|
2018-04-13 19:21:32 +02:00
|
|
|
|
|
|
|
|
|
# keys with identical names
|
2018-04-14 22:09:42 +02:00
|
|
|
|
self.assertEqual(f("foo=1&foo=2"), {"foo": "1"})
|
2018-04-13 19:21:32 +02:00
|
|
|
|
self.assertEqual(
|
2018-04-14 22:09:42 +02:00
|
|
|
|
f("foo=1&bar=2&foo=3&bar=4"),
|
2018-04-13 19:21:32 +02:00
|
|
|
|
{"foo": "1", "bar": "2"},
|
|
|
|
|
)
|
|
|
|
|
|
2018-04-14 22:09:42 +02:00
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value), {})
|
2018-04-13 19:21:32 +02:00
|
|
|
|
|
2024-10-01 20:15:09 +02:00
|
|
|
|
def test_parse_query_list(self, f=text.parse_query_list):
|
|
|
|
|
# standard usage
|
|
|
|
|
self.assertEqual(f(""), {})
|
|
|
|
|
self.assertEqual(f("foo=1"), {"foo": "1"})
|
|
|
|
|
self.assertEqual(f("foo=1&bar=2"), {"foo": "1", "bar": "2"})
|
|
|
|
|
|
|
|
|
|
# missing value
|
|
|
|
|
self.assertEqual(f("bar"), {})
|
|
|
|
|
self.assertEqual(f("foo=1&bar"), {"foo": "1"})
|
|
|
|
|
self.assertEqual(f("foo=1&bar&baz=3"), {"foo": "1", "baz": "3"})
|
|
|
|
|
|
|
|
|
|
# keys with identical names
|
|
|
|
|
self.assertEqual(f("foo=1&foo=2"), {"foo": ["1", "2"]})
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("foo=1&bar=2&foo=3&bar=4&foo=5"),
|
|
|
|
|
{"foo": ["1", "3", "5"], "bar": ["2", "4"]},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# invalid arguments
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value), {})
|
|
|
|
|
|
2019-04-21 15:28:27 +02:00
|
|
|
|
def test_parse_timestamp(self, f=text.parse_timestamp):
|
2024-09-19 20:09:03 +02:00
|
|
|
|
null = util.datetime_utcfromtimestamp(0)
|
|
|
|
|
value = util.datetime_utcfromtimestamp(1555816235)
|
2019-04-21 15:28:27 +02:00
|
|
|
|
|
|
|
|
|
self.assertEqual(f(0) , null)
|
|
|
|
|
self.assertEqual(f("0") , null)
|
|
|
|
|
self.assertEqual(f(1555816235) , value)
|
|
|
|
|
self.assertEqual(f("1555816235"), value)
|
|
|
|
|
|
|
|
|
|
for value in INVALID_ALT:
|
|
|
|
|
self.assertEqual(f(value), None)
|
|
|
|
|
self.assertEqual(f(value, "foo"), "foo")
|
|
|
|
|
|
2019-05-08 00:00:00 +02:00
|
|
|
|
def test_parse_datetime(self, f=text.parse_datetime):
|
2024-09-19 20:09:03 +02:00
|
|
|
|
null = util.datetime_utcfromtimestamp(0)
|
2019-05-08 00:00:00 +02:00
|
|
|
|
|
2019-05-09 21:53:17 +02:00
|
|
|
|
self.assertEqual(f("1970-01-01T00:00:00+00:00"), null)
|
|
|
|
|
self.assertEqual(f("1970-01-01T00:00:00+0000") , null)
|
|
|
|
|
self.assertEqual(f("1970.01.01", "%Y.%m.%d") , null)
|
2019-05-08 00:00:00 +02:00
|
|
|
|
|
2019-05-09 21:53:17 +02:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("2019-05-07T21:25:02+09:00"),
|
|
|
|
|
datetime.datetime(2019, 5, 7, 12, 25, 2),
|
|
|
|
|
)
|
2019-05-08 00:00:00 +02:00
|
|
|
|
self.assertEqual(
|
2019-05-08 18:00:40 +02:00
|
|
|
|
f("2019-05-07T21:25:02+0900"),
|
2019-05-08 00:00:00 +02:00
|
|
|
|
datetime.datetime(2019, 5, 7, 12, 25, 2),
|
|
|
|
|
)
|
2020-06-17 21:40:16 +02:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("2019-05-07T21:25:02.753+0900", "%Y-%m-%dT%H:%M:%S.%f%z"),
|
|
|
|
|
datetime.datetime(2019, 5, 7, 12, 25, 2),
|
|
|
|
|
)
|
2020-04-11 02:05:00 +02:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("2019-05-07T21:25:02", "%Y-%m-%dT%H:%M:%S", utcoffset=9),
|
|
|
|
|
datetime.datetime(2019, 5, 7, 12, 25, 2),
|
|
|
|
|
)
|
2019-05-25 23:22:26 +02:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
f("2019-05-07 21:25:02"),
|
|
|
|
|
"2019-05-07 21:25:02",
|
|
|
|
|
)
|
2019-05-08 00:00:00 +02:00
|
|
|
|
|
|
|
|
|
for value in INVALID:
|
|
|
|
|
self.assertEqual(f(value), None)
|
|
|
|
|
self.assertEqual(f("1970.01.01"), "1970.01.01")
|
|
|
|
|
|
2017-01-30 19:40:15 +01:00
|
|
|
|
|
2024-02-27 02:10:05 +01:00
|
|
|
|
if __name__ == "__main__":
|
2015-10-03 14:51:13 +02:00
|
|
|
|
unittest.main()
|