Skip to content

Commit

Permalink
Rename test files and test variables to denote test cases more precis…
Browse files Browse the repository at this point in the history
…ely.
  • Loading branch information
mansenfranzen committed Oct 10, 2016
1 parent 29862d4 commit 88dd3a8
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 45 deletions.
2 changes: 0 additions & 2 deletions tests/material/include_dummy.yaml

This file was deleted.

1 change: 0 additions & 1 deletion tests/material/include_dummy_lvl2.yaml

This file was deleted.

File renamed without changes.
2 changes: 2 additions & 0 deletions tests/material/parser_include.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Key1: Value1
Key2: $include parser_flatten_dict.yaml
File renamed without changes.
1 change: 1 addition & 0 deletions tests/material/parser_include_recursive.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lvl1: $include parser_include.yaml
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
72 changes: 37 additions & 35 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,101 @@

import pytest
import confipy.converter
import confipy.notation
import confipy.reader
import confipy.parser
import os


def get_file(path):
cur_dir = os.path.dirname(__file__)
return os.path.join(cur_dir, path)


test_flattened_dict = {("level1", "level2", "level3", "key1"): "value1",
("level1", "level2", "level3", "key2"): "value2"}

test_incl = {("Key1",): "Value1",
("Key2", "level1", "level2", "level3", "key1"): "value1",
("Key2", "level1", "level2", "level3", "key2"): "value2"}

test_incl_lvl2 = {("Lvl1", "Key1",): "Value1",
("Lvl1", "Key2", "level1", "level2", "level3",
"key1"): "value1",
("Lvl1", "Key2", "level1", "level2", "level3",
"key2"): "value2"}
test_incl_recursive = {("Lvl1", "Key1",): "Value1",
("Lvl1", "Key2", "level1", "level2", "level3",
"key1"): "value1",
("Lvl1", "Key2", "level1", "level2", "level3",
"key2"): "value2"}

level3 = {"level3":{"key1":"value1", "key2": "value2"}}
test_incl_lvl2_unflattend = {"Lvl1":{"Key1":"Value1",
"Key2":{"level1": {"level2":level3}}}}
level3 = {"level3": {"key1": "value1", "key2": "value2"}}
test_incl_lvl2_unflattend = {"Lvl1": {"Key1": "Value1",
"Key2": {"level1": {"level2": level3}}}}

test_subs = {("key1",): "value1", ("key2",): "value2",
("list1",): ["value1value2", "prevalue1suf"],
("lvl1", "lvl2", "key1"): "value3rd", ("check",): "value3rd.txt"}


def test_flatten_dict():
cfg = confipy.reader.read_config(get_file("material/skeleton_dummy.yaml"))
skeleton = confipy.converter._flat_dict(cfg)
test_file = get_file("material/parser_flatten_dict.yaml")
cfg = confipy.reader.read_config(test_file)
flattened = confipy.converter._flat_dict(cfg)

assert skeleton == test_flattened_dict
assert flattened == test_flattened_dict


def test_include():
file = get_file("material/include_dummy.yaml")
cfg = confipy.reader.read_config(file)
skeleton = confipy.converter._flat_dict(cfg)
included = confipy.parser.include(skeleton, file)
test_file = get_file("material/parser_include.yaml")
cfg = confipy.reader.read_config(test_file)
flattened = confipy.converter._flat_dict(cfg)
included = confipy.parser.include(flattened, test_file)

assert included == test_incl


def test_include_lvl2():
file = get_file("material/include_dummy_lvl2.yaml")
cfg = confipy.reader.read_config(file)
skeleton = confipy.converter._flat_dict(cfg)
included = confipy.parser.include(skeleton, file)
def test_include_recursive():
test_file = get_file("material/parser_include_recursive.yaml")
cfg = confipy.reader.read_config(test_file)
flattened = confipy.converter._flat_dict(cfg)
included = confipy.parser.include(flattened, test_file)

assert included == test_incl_lvl2
assert included == test_incl_recursive


def test_include_fail():
file = get_file("material/include_fail.yaml")
file = get_file("material/parser_include_raiseError.yaml")
cfg = confipy.reader.read_config(file)
skeleton = confipy.converter._flat_dict(cfg)
flattened = confipy.converter._flat_dict(cfg)

with pytest.raises(AssertionError):
included = confipy.parser.include(skeleton, file)
included = confipy.parser.include(flattened, file)


def test_substitute():
file = get_file("material/substitute_dummy.yaml")
cfg = confipy.reader.read_config(file)
skeleton = confipy.converter._flat_dict(cfg)
subs = confipy.parser.substitute(skeleton)
test_file = get_file("material/parser_substitute.yaml")
cfg = confipy.reader.read_config(test_file)
flattened = confipy.converter._flat_dict(cfg)
subs = confipy.parser.substitute(flattened)

assert subs == test_subs


def test_unflatten():
unflatten = confipy.converter._unflat_dict(test_incl_lvl2)
unflatten = confipy.converter._unflat_dict(test_incl_recursive)
assert unflatten == test_incl_lvl2_unflattend


def test_unflatten_dot():
def_type = confipy.converter.DotNotation
unflatten = confipy.converter._unflat_dict(test_incl_lvl2,
default_type=def_type)
unflatten = confipy.converter._unflat_dict(test_incl_recursive,
notation="dot")

assert isinstance(unflatten, def_type) == True
assert isinstance(unflatten, confipy.notation.DotNotation) == True
assert unflatten.Lvl1.Key1 == "Value1"


if __name__ == "__main__":
test_flatten_dict()
test_include()
test_include_lvl2()
test_include_recursive()
test_include_fail()
test_substitute()
test_unflatten()
test_unflatten_dot()

14 changes: 7 additions & 7 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_file(path):


def test_cfg_reader():
cfg = confipy.reader.read_config(get_file("material/dummy.cfg"))
cfg = confipy.reader.read_config(get_file("material/reader_cfg.cfg"))

# as lists are not supported here, they need to be converted
list_int = list(map(int, cfg["DummySection1"]["list_int"].split(",")))
Expand All @@ -39,7 +39,7 @@ def test_cfg_reader():


def test_ini_reader():
cfg = confipy.reader.read_config(get_file("material/dummy.ini"))
cfg = confipy.reader.read_config(get_file("material/reader_ini.ini"))

# as lists are not supported here, they need to be converted
list_int = list(map(int, cfg["DummySection1"]["list_int"].split(",")))
Expand All @@ -52,31 +52,31 @@ def test_ini_reader():


def test_yaml_reader():
cfg = confipy.reader.read_config(get_file("material/dummy.yaml"))
cfg = confipy.reader.read_config(get_file("material/reader_yaml.yaml"))
assert cfg == test_dict


def test_json_reader():
cfg = confipy.reader.read_config(get_file("material/dummy.json"))
cfg = confipy.reader.read_config(get_file("material/reader_json.json"))
assert cfg == test_dict


def test_auto_read():
with open(get_file("material/dummy.yaml"), "r") as file:
with open(get_file("material/reader_yaml.yaml"), "r") as file:
cfg = confipy.reader.read_config(file)

assert cfg == test_dict


with open(get_file("material/dummy.json"), "r") as file:
with open(get_file("material/reader_json.json"), "r") as file:
cfg = confipy.reader.read_config(file)

assert cfg == test_dict


def test_auto_read_fail():
with pytest.raises(IOError):
with open(get_file("material/dummy_bad.cfg"), "r") as file:
with open(get_file("material/reader_raiseError.cfg"), "r") as file:
cfg = confipy.reader.read_config(file)


Expand Down

0 comments on commit 88dd3a8

Please sign in to comment.