Skip to content

Commit

Permalink
fix: add missing test cases of ac_dict and ordered option passed to *…
Browse files Browse the repository at this point in the history
…_load APIs
  • Loading branch information
ssato committed Mar 7, 2017
1 parent 0011212 commit b7c1bdd
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions tests/api.py
Expand Up @@ -14,6 +14,7 @@

import anyconfig.api as TT
import anyconfig.backends
import anyconfig.compat
import anyconfig.dicts
import anyconfig.template
import tests.common
Expand Down Expand Up @@ -69,6 +70,10 @@
NULL_CNTNR = TT.anyconfig.dicts.convert_to({})


class MyODict(anyconfig.compat.OrderedDict):
pass


def _is_file_object(obj):
try:
return isinstance(obj, file)
Expand Down Expand Up @@ -111,8 +116,8 @@ class TestBase(unittest.TestCase):
cnf = dic = dict(a=1, b=dict(b=[0, 1], c="C"), name="a")
upd = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D"), e=0)

def assert_dicts_equal(self, dic, ref):
self.assertTrue(dicts_equal(dic, ref),
def assert_dicts_equal(self, dic, ref, ordered=False):
self.assertTrue(dicts_equal(dic, ref, ordered=ordered),
"%r\nvs.\n%r" % (dic, ref))


Expand Down Expand Up @@ -288,6 +293,24 @@ def test_19_dump_and_single_load_with_validation(self):
cnf_3 = TT.single_load(cnf_2_path, ac_schema=scm_path)
self.assertTrue(cnf_3 is None) # Validation should fail.

def test_20_dump_and_single_load__w_ordered_option(self):
TT.dump(self.cnf, self.a_path)
self.assertTrue(os.path.exists(self.a_path))

# It works w/ JSON backend but some backend cannot keep the order of
# items and the tests might fail.
res = TT.single_load(self.a_path, ac_ordered=True)
self.assert_dicts_equal(res, self.cnf, ordered=True)
self.assertTrue(isinstance(res, anyconfig.compat.OrderedDict))

def test_22_dump_and_single_load__w_ac_dict_option(self):
TT.dump(self.cnf, self.a_path)
self.assertTrue(os.path.exists(self.a_path))

res = TT.single_load(self.a_path, ac_dict=MyODict)
self.assert_dicts_equal(res, self.cnf, ordered=True)
self.assertTrue(isinstance(res, MyODict))


class Test_32_single_load(unittest.TestCase):

Expand Down Expand Up @@ -457,6 +480,14 @@ def test_50_multi_load__templates(self):
self.assert_dicts_equal(res0, self.exp)
self.assert_dicts_equal(res1, self.exp)

def test_60_multi_load__w_ac_dict_option(self):
TT.dump(self.dic, self.a_path)
TT.dump(self.upd, self.b_path)

res = TT.multi_load(self.g_path, ac_dict=MyODict)
self.assert_dicts_equal(res, self.exp)
self.assertTrue(isinstance(res, MyODict))


class Test_50_load_and_dump(TestBaseWithIOMultiFiles):

Expand Down

0 comments on commit b7c1bdd

Please sign in to comment.