test_tiaf_tools.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. from logging import getLogger
  9. from tiaf_tools import run
  10. import os
  11. import json
  12. from pathlib import Path
  13. logging = getLogger("tiaf_tools")
  14. class TestTIAFToolsLocal():
  15. def test_no_args(self, caplog):
  16. # given:
  17. test_string = "You must define a repository to search in when searching locally"
  18. args = {}
  19. # when:
  20. run(args)
  21. # then:
  22. assert test_string in caplog.messages
  23. def test_local_search_in_has_invalid_file_path(self, caplog):
  24. # given:
  25. test_string = 'FileNotFoundError'
  26. args = {'search_in' : "Not a file path!"}
  27. # when:
  28. run(args)
  29. # then:
  30. assert test_string in caplog.messages
  31. def test_local_search_in_has_valid_file_path(self, caplog, tmp_path_factory):
  32. # given:
  33. test_string = 'FileNotFoundError'
  34. file = tmp_path_factory.mktemp("test")
  35. args = {'search_in': file}
  36. # when:
  37. run(args)
  38. # then:
  39. assert test_string not in caplog.messages
  40. class TestTIAFToolsLocalJSON():
  41. def test_local_search_full_address_no_action_file_does_not_exist(self, caplog, tmp_path):
  42. # given:
  43. fn = tmp_path / "test_file.txt"
  44. full_address = fn
  45. test_string = f"File not found at {full_address}"
  46. args = {'full_address' : full_address, 'file_type' : 'json'}
  47. # when:
  48. run(args)
  49. # then:
  50. assert test_string in caplog.messages
  51. def test_local_search_full_address_file_does_exist(self, caplog, tmp_path_factory):
  52. # given:
  53. fn = tmp_path_factory.mktemp("test") / "test_file.txt"
  54. fn.write_text("Testing")
  55. full_address = fn
  56. test_string = f"File found at {full_address}"
  57. args = {'full_address' : full_address, 'file_type' : 'json'}
  58. # when:
  59. run(args)
  60. # then:
  61. assert test_string in caplog.messages
  62. def test_local_full_address_action_read_file_exists(self, caplog, tmp_path_factory, mocker):
  63. # given:
  64. fn = tmp_path_factory.mktemp("test") / "test_file.txt"
  65. fn.write_text("Testing")
  66. full_address = fn
  67. test_string = f"File found at {full_address}"
  68. args = {'full_address' : full_address, 'action': 'read', 'file_type' : 'json'}
  69. mocker.patch('os.startfile')
  70. # when:
  71. run(args)
  72. # then:
  73. assert test_string in caplog.messages
  74. os.startfile.assert_called_once_with(os.path.split(full_address)[0])
  75. def test_local_full_address_action_delete(self, caplog, tmp_path_factory):
  76. # given:
  77. fn = tmp_path_factory.mktemp("test") / "test_file.txt"
  78. fn.write_text("Testing")
  79. assert os.path.exists(fn)
  80. full_address = fn
  81. args = {'full_address' : full_address, 'action': 'delete', 'file_type' : 'json'}
  82. # when:
  83. run(args)
  84. # then:
  85. assert os.path.exists(fn) == False
  86. def test_local_full_address_action_create_valid_json(self, caplog, tmp_path_factory):
  87. # given:
  88. fn = tmp_path_factory.mktemp("test") / "test_file.txt"
  89. json_list = ['test']
  90. json_str = json.dumps(json_list)
  91. fn.write_text(json_str)
  92. assert os.path.exists(fn)
  93. target_dir = tmp_path_factory.mktemp("test")
  94. full_address = f"{target_dir}/historic_data.json"
  95. args = {'full_address' : full_address, 'action': 'create', 'file_in':fn, 'file_type' : 'json'}
  96. # when:
  97. run(args)
  98. # then:
  99. assert os.path.exists(full_address)
  100. def test_local_full_address_action_create_invalid_json(self, caplog, tmp_path_factory):
  101. # given:
  102. fn = tmp_path_factory.mktemp("test") / "test_file.txt"
  103. test_string = "The historic data does not contain valid json"
  104. fn.write_text("test!")
  105. assert os.path.exists(fn)
  106. target = tmp_path_factory.mktemp("test")
  107. full_address = target
  108. args = {'full_address' : full_address, 'action': 'create', 'file_in':fn, 'file_type' : 'json'}
  109. # when:
  110. run(args)
  111. # then:
  112. assert test_string in caplog.messages
  113. assert os.path.exists(str(target)+"/historic_data.json") == False
  114. def test_local_full_address_action_create_already_exists(self, caplog, tmp_path_factory):
  115. # given:
  116. file_in = tmp_path_factory.mktemp("test") / "test_file.txt"
  117. source_json_list = ['test']
  118. source_json_str = json.dumps(source_json_list)
  119. file_in.write_text(source_json_str)
  120. assert os.path.exists(file_in)
  121. test_string = "Cancelling create, as file already exists"
  122. target = tmp_path_factory.mktemp("test") / "historic_data.json"
  123. target_json_list = ['success']
  124. target_json_str = json.dumps(target_json_list)
  125. target.write_text(target_json_str)
  126. full_address = target
  127. args = {'full_address' : full_address, 'action': 'create', 'file_in':file_in, 'file_type' : 'json'}
  128. # when:
  129. run(args)
  130. # then:
  131. assert test_string in caplog.messages
  132. assert os.path.exists(target)
  133. assert target.read_text() == target_json_str
  134. def test_local_full_address_action_update_valid_json(self, caplog, tmp_path_factory):
  135. # given:
  136. file_in = tmp_path_factory.mktemp("test") / "test_file.txt"
  137. source_json_list = ['test']
  138. source_json_str = json.dumps(source_json_list)
  139. file_in.write_text(source_json_str)
  140. assert os.path.exists(file_in)
  141. target = tmp_path_factory.mktemp("test") / "historic_data.json"
  142. target_json_list = ['failure']
  143. target_json_str = json.dumps(target_json_list)
  144. target.write_text(target_json_str)
  145. full_address = target
  146. assert os.path.exists(full_address)
  147. args = {'full_address' : full_address, 'action': 'update', 'file_in':file_in, 'file_type' : 'json'}
  148. # when:
  149. run(args)
  150. # then:
  151. out_file_str = target.read_text()
  152. assert "test" in out_file_str
  153. assert not "failure" in out_file_str
  154. def test_local_full_address_action_update_invalid_json(self, caplog, tmp_path_factory):
  155. # given:
  156. file_in = tmp_path_factory.mktemp("test") / "test_file.txt"
  157. source_json_str = "failure"
  158. file_in.write_text(source_json_str)
  159. assert os.path.exists(file_in)
  160. target = tmp_path_factory.mktemp("test") / "historic_data.json"
  161. target_json_list = ['success']
  162. target_json_str = json.dumps(target_json_list)
  163. target.write_text(target_json_str)
  164. full_address = target
  165. assert os.path.exists(full_address)
  166. test_string = "The historic data does not contain valid json"
  167. args = {'full_address' : full_address, 'action': 'update', 'file_in':file_in, 'file_type' : 'json'}
  168. # when:
  169. run(args)
  170. # then:
  171. out_file_str = target.read_text()
  172. assert "success" in out_file_str
  173. assert not "failure" in out_file_str
  174. assert test_string in caplog.messages
  175. def test_local_full_address_action_update_file_does_not_exist(self, caplog, tmp_path_factory):
  176. # given:
  177. file_in = tmp_path_factory.mktemp("test") / "test_file.txt"
  178. source_json_list = ['test']
  179. source_json_str = json.dumps(source_json_list)
  180. file_in.write_text(source_json_str)
  181. assert os.path.exists(file_in)
  182. test_string = "Cancelling update, as file does not exist"
  183. target = tmp_path_factory.mktemp("test")
  184. full_address = target.parent
  185. args = {'full_address' : full_address, 'action': 'update', 'file_in':file_in, 'file_type' : 'json'}
  186. # when:
  187. run(args)
  188. # then:
  189. assert test_string in caplog.messages
  190. assert not os.path.exists(str(target)+"/historic_data.json")
  191. def test_local_display_directory(self, caplog, tmp_path_factory):
  192. # given:
  193. address_list = []
  194. for i in range(0, 2):
  195. file = tmp_path_factory.mktemp("folder" + str(i)) / "historic_data.json"
  196. file.write_text("test_data")
  197. assert os.path.exists(file)
  198. address_list.append(file)
  199. root_dir = address_list[0].parent.parent
  200. args = {"search_in" : root_dir, 'file_type' : 'json'}
  201. # when:
  202. run(args)
  203. #then
  204. i = 0
  205. addresses = Path.glob(Path(root_dir), "**/*.json")
  206. for address in addresses:
  207. absolute = str(address.absolute())
  208. assert absolute in caplog.messages[i]
  209. i += 1
  210. def test_display_full_address(self, caplog, tmp_path_factory):
  211. # given:
  212. file = tmp_path_factory.mktemp("test") / "historic_data.json"
  213. file.write_text("test_data")
  214. assert os.path.exists(file)
  215. args = {'full_address': file, 'file_type' : 'json'}
  216. test_string = f"File found at {file}"
  217. # when:
  218. run(args)
  219. # then:
  220. assert test_string in caplog.messages