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_saver.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import os
  2. import unittest
  3. from fastNLP.loader.config_loader import ConfigSection, ConfigLoader
  4. from fastNLP.saver.config_saver import ConfigSaver
  5. class TestConfigSaver(unittest.TestCase):
  6. def test_case_1(self):
  7. config_file_dir = "test/loader/"
  8. config_file_name = "config"
  9. config_file_path = os.path.join(config_file_dir, config_file_name)
  10. tmp_config_file_path = os.path.join(config_file_dir, "tmp_config")
  11. with open(config_file_path, "r") as f:
  12. lines = f.readlines()
  13. standard_section = ConfigSection()
  14. t_section = ConfigSection()
  15. ConfigLoader().load_config(config_file_path, {"test": standard_section, "t": t_section})
  16. config_saver = ConfigSaver(config_file_path)
  17. section = ConfigSection()
  18. section["doubles"] = 0.8
  19. section["tt"] = 0.5
  20. section["test"] = 105
  21. section["str"] = "this is a str"
  22. test_case_2_section = section
  23. test_case_2_section["double"] = 0.5
  24. for k in section.__dict__.keys():
  25. standard_section[k] = section[k]
  26. config_saver.save_config_file("test", section)
  27. config_saver.save_config_file("another-test", section)
  28. config_saver.save_config_file("one-another-test", section)
  29. config_saver.save_config_file("test-case-2", section)
  30. test_section = ConfigSection()
  31. at_section = ConfigSection()
  32. another_test_section = ConfigSection()
  33. one_another_test_section = ConfigSection()
  34. a_test_case_2_section = ConfigSection()
  35. ConfigLoader().load_config(config_file_path, {"test": test_section,
  36. "another-test": another_test_section,
  37. "t": at_section,
  38. "one-another-test": one_another_test_section,
  39. "test-case-2": a_test_case_2_section})
  40. assert test_section == standard_section
  41. assert at_section == t_section
  42. assert another_test_section == section
  43. assert one_another_test_section == section
  44. assert a_test_case_2_section == test_case_2_section
  45. config_saver.save_config_file("test", section)
  46. with open(config_file_path, "w") as f:
  47. f.writelines(lines)
  48. with open(tmp_config_file_path, "w") as f:
  49. f.write('[test]\n')
  50. f.write('this is an fault example\n')
  51. tmp_config_saver = ConfigSaver(tmp_config_file_path)
  52. try:
  53. tmp_config_saver._read_section()
  54. except Exception as e:
  55. pass
  56. os.remove(tmp_config_file_path)
  57. try:
  58. tmp_config_saver = ConfigSaver("file-NOT-exist")
  59. except Exception as e:
  60. pass
  61. def test_case_2(self):
  62. config = "[section_A]\n[section_B]\n"
  63. with open("./test.cfg", "w", encoding="utf-8") as f:
  64. f.write(config)
  65. saver = ConfigSaver("./test.cfg")
  66. section = ConfigSection()
  67. section["doubles"] = 0.8
  68. section["tt"] = [1, 2, 3]
  69. section["test"] = 105
  70. section["str"] = "this is a str"
  71. saver.save_config_file("section_A", section)
  72. os.system("rm ./test.cfg")
  73. def test_case_3(self):
  74. config = "[section_A]\ndoubles = 0.9\ntt = [1, 2, 3]\n[section_B]\n"
  75. with open("./test.cfg", "w", encoding="utf-8") as f:
  76. f.write(config)
  77. saver = ConfigSaver("./test.cfg")
  78. section = ConfigSection()
  79. section["doubles"] = 0.8
  80. section["tt"] = [1, 2, 3]
  81. section["test"] = 105
  82. section["str"] = "this is a str"
  83. saver.save_config_file("section_A", section)
  84. os.system("rm ./test.cfg")