1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00
gallery-dl/test/test_config.py

110 lines
3.8 KiB
Python
Raw Normal View History

2015-10-03 20:24:28 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2019-11-23 23:50:16 +01:00
# Copyright 2015-2019 Mike Fährmann
2015-10-03 20:24:28 +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.
import unittest
import gallery_dl.config as config
import os
import tempfile
2017-01-30 19:40:15 +01:00
2015-10-03 20:24:28 +02:00
class TestConfig(unittest.TestCase):
def setUp(self):
2019-11-23 23:50:16 +01:00
config.set((), "a", "1")
config.set((), "b", {
"a": 2,
"c": "text",
})
2015-10-03 20:24:28 +02:00
def tearDown(self):
config.clear()
def test_get(self):
2019-11-23 23:50:16 +01:00
self.assertEqual(config.get(() , "a") , "1")
self.assertEqual(config.get(("b",) , "c") , "text")
self.assertEqual(config.get(() , "d") , None)
self.assertEqual(config.get(("e", "f"), "g", 123), 123)
2015-10-03 20:24:28 +02:00
def test_interpolate(self):
2019-11-23 23:50:16 +01:00
self.assertEqual(config.interpolate(() , "a") , "1")
self.assertEqual(config.interpolate(("b",), "a") , "1")
self.assertEqual(config.interpolate(("b",), "c", "2"), "text")
self.assertEqual(config.interpolate(("b",), "d", "2"), "2")
config.set((), "d", 123)
self.assertEqual(config.interpolate(("b",), "d", "2"), 123)
self.assertEqual(config.interpolate(("d",), "d", "2"), 123)
2015-10-03 20:24:28 +02:00
def test_set(self):
2019-11-23 23:50:16 +01:00
config.set(("b",) , "c", [1, 2, 3])
config.set(("e", "f"), "g", value=234)
self.assertEqual(config.get(("b",) , "c"), [1, 2, 3])
self.assertEqual(config.get(("e", "f"), "g"), 234)
2015-10-03 20:24:28 +02:00
2015-10-07 00:58:43 +02:00
def test_setdefault(self):
2019-11-23 23:50:16 +01:00
config.setdefault(("b",) , "c", [1, 2, 3])
config.setdefault(("e", "f"), "g", value=234)
self.assertEqual(config.get(("b",) , "c"), "text")
self.assertEqual(config.get(("e", "f"), "g"), 234)
2015-10-07 00:58:43 +02:00
def test_unset(self):
2019-11-23 23:50:16 +01:00
config.unset(() , "a")
config.unset(("b",), "c")
config.unset(("c",), "d")
self.assertEqual(config.get(() , "a"), None)
self.assertEqual(config.get(("b",), "a"), 2)
self.assertEqual(config.get(("b",), "c"), None)
def test_apply(self):
options = (
2019-11-23 23:50:16 +01:00
(("b",) , "c", [1, 2, 3]),
(("e", "f"), "g", 234),
)
2019-11-23 23:50:16 +01:00
self.assertEqual(config.get(("b",) , "c"), "text")
self.assertEqual(config.get(("e", "f"), "g"), None)
with config.apply(options):
2019-11-23 23:50:16 +01:00
self.assertEqual(config.get(("b",) , "c"), [1, 2, 3])
self.assertEqual(config.get(("e", "f"), "g"), 234)
self.assertEqual(config.get(("b",) , "c"), "text")
self.assertEqual(config.get(("e", "f"), "g"), None)
def test_load(self):
with tempfile.TemporaryDirectory() as base:
path1 = os.path.join(base, "cfg1")
with open(path1, "w") as file:
file.write('{"a": "1", "b": {"a": 2, "c": "text"}}')
path2 = os.path.join(base, "cfg2")
with open(path2, "w") as file:
file.write('{"a": "7", "b": {"a": 8, "e": "foo"}}')
config.load((path1,))
self.assertEqual(config.get(() , "a"), "1")
self.assertEqual(config.get(("b",), "a"), 2)
self.assertEqual(config.get(("b",), "c"), "text")
config.load((path2,))
self.assertEqual(config.get(() , "a"), "7")
self.assertEqual(config.get(("b",), "a"), 8)
self.assertEqual(config.get(("b",), "c"), "text")
self.assertEqual(config.get(("b",), "e"), "foo")
config.clear()
config.load((path1, path2))
self.assertEqual(config.get(() , "a"), "7")
self.assertEqual(config.get(("b",), "a"), 8)
self.assertEqual(config.get(("b",), "c"), "text")
self.assertEqual(config.get(("b",), "e"), "foo")
2015-10-03 20:24:28 +02:00
2017-01-30 19:40:15 +01:00
2015-10-03 20:24:28 +02:00
if __name__ == '__main__':
unittest.main()