You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_config_loader.py 1.8 kB

7 years ago
7 years ago
7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import configparser
  2. import json
  3. import os
  4. import unittest
  5. from fastNLP.loader.config_loader import ConfigSection, ConfigLoader
  6. class TestConfigLoader(unittest.TestCase):
  7. def test_case_ConfigLoader(self):
  8. def read_section_from_config(config_path, section_name):
  9. dict = {}
  10. if not os.path.exists(config_path):
  11. raise FileNotFoundError("config file {} NOT found.".format(config_path))
  12. cfg = configparser.ConfigParser()
  13. cfg.read(config_path)
  14. if section_name not in cfg:
  15. raise AttributeError("config file {} do NOT have section {}".format(
  16. config_path, section_name
  17. ))
  18. gen_sec = cfg[section_name]
  19. for s in gen_sec.keys():
  20. try:
  21. val = json.loads(gen_sec[s])
  22. dict[s] = val
  23. except Exception as e:
  24. raise AttributeError("json can NOT load {} in section {}, config file {}".format(
  25. s, section_name, config_path
  26. ))
  27. return dict
  28. test_arg = ConfigSection()
  29. ConfigLoader().load_config(os.path.join("./test/loader", "config"), {"test": test_arg})
  30. section = read_section_from_config(os.path.join("./test/loader", "config"), "test")
  31. for sec in section:
  32. if (sec not in test_arg) or (section[sec] != test_arg[sec]):
  33. raise AttributeError("ERROR")
  34. for sec in test_arg.__dict__.keys():
  35. if (sec not in section) or (section[sec] != test_arg[sec]):
  36. raise AttributeError("ERROR")
  37. try:
  38. not_exist = test_arg["NOT EXIST"]
  39. except Exception as e:
  40. pass
  41. print("pass config test!")