2015-10-03 20:24:28 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-03-27 11:59:27 +02:00
|
|
|
# Copyright 2015-2017 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):
|
|
|
|
fd, self._configfile = tempfile.mkstemp()
|
|
|
|
with os.fdopen(fd, "w") as file:
|
2017-03-27 11:59:27 +02:00
|
|
|
file.write('{"a": "1", "b": {"a": 2, "c": "text"}}')
|
2015-10-03 20:24:28 +02:00
|
|
|
config.load(self._configfile)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
config.clear()
|
|
|
|
os.remove(self._configfile)
|
|
|
|
|
|
|
|
def test_get(self):
|
2015-10-05 12:42:42 +02: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_set(self):
|
2015-10-05 12:42:42 +02: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):
|
|
|
|
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-03 20:24:28 +02:00
|
|
|
def test_interpolate(self):
|
2015-10-05 12:42:42 +02: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
|
|
|
|
2017-01-30 19:40:15 +01:00
|
|
|
|
2015-10-03 20:24:28 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|