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 4.0 kB

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