test_migrate_cli.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import filecmp
  12. import os
  13. import shutil
  14. import sys
  15. from sqlite3 import OperationalError
  16. from unittest.mock import patch
  17. import mongomock
  18. import pytest
  19. from taipy.core._entity._migrate_cli import _MigrateCLI
  20. @pytest.fixture(scope="function", autouse=True)
  21. def clean_data_folder():
  22. if os.path.exists("tests/core/_entity/.data"):
  23. shutil.rmtree("tests/core/_entity/.data")
  24. yield
  25. def test_migrate_fs_default(caplog):
  26. _MigrateCLI.create_parser()
  27. # Test migrate with default .data folder
  28. with pytest.raises(SystemExit):
  29. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", "--skip-backup"]):
  30. _MigrateCLI.parse_arguments()
  31. assert "Starting entity migration from '.taipy/' folder" in caplog.text
  32. def test_migrate_fs_specified_folder(caplog):
  33. _MigrateCLI.create_parser()
  34. # Copy data_sample to .data folder for testing
  35. data_sample_path = "tests/core/_entity/data_sample"
  36. data_path = "tests/core/_entity/.data"
  37. shutil.copytree(data_sample_path, data_path)
  38. # Run with --skip-backup to only test the migration
  39. with pytest.raises(SystemExit):
  40. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--skip-backup"]):
  41. _MigrateCLI.parse_arguments()
  42. assert f"Starting entity migration from '{data_path}' folder" in caplog.text
  43. # Compare migrated .data folder with data_sample_migrated
  44. dircmp_result = filecmp.dircmp(data_path, "tests/core/_entity/data_sample_migrated")
  45. assert not dircmp_result.diff_files and not dircmp_result.left_only and not dircmp_result.right_only
  46. for subdir in dircmp_result.subdirs.values():
  47. assert not subdir.diff_files and not subdir.left_only and not subdir.right_only
  48. def test_migrate_fs_backup_and_remove(caplog):
  49. _MigrateCLI.create_parser()
  50. # Copy data_sample to .data folder for testing
  51. data_sample_path = "tests/core/_entity/data_sample"
  52. data_path = "tests/core/_entity/.data"
  53. backup_path = "tests/core/_entity/.data_backup"
  54. shutil.copytree(data_sample_path, data_path)
  55. # Remove backup when it does not exist should raise an error
  56. with pytest.raises(SystemExit) as err:
  57. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--remove-backup"]):
  58. _MigrateCLI.parse_arguments()
  59. assert err.value.code == 1
  60. assert f"The backup folder '{backup_path}' does not exist." in caplog.text
  61. assert not os.path.exists(backup_path)
  62. # Run without --skip-backup to create the backup folder
  63. with pytest.raises(SystemExit):
  64. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path]):
  65. _MigrateCLI.parse_arguments()
  66. assert f"Backed up entities from '{data_path}' to '{backup_path}' folder before migration." in caplog.text
  67. assert os.path.exists(backup_path)
  68. # Remove backup
  69. with pytest.raises(SystemExit):
  70. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--remove-backup"]):
  71. _MigrateCLI.parse_arguments()
  72. assert f"Removed backup entities from the backup folder '{backup_path}'." in caplog.text
  73. assert not os.path.exists(backup_path)
  74. def test_migrate_fs_backup_and_restore(caplog):
  75. _MigrateCLI.create_parser()
  76. # Copy data_sample to .data folder for testing
  77. data_sample_path = "tests/core/_entity/data_sample"
  78. data_path = "tests/core/_entity/.data"
  79. backup_path = "tests/core/_entity/.data_backup"
  80. shutil.copytree(data_sample_path, data_path)
  81. # Restore backup when it does not exist should raise an error
  82. with pytest.raises(SystemExit) as err:
  83. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--restore"]):
  84. _MigrateCLI.parse_arguments()
  85. assert err.value.code == 1
  86. assert f"The backup folder '{backup_path}' does not exist." in caplog.text
  87. assert not os.path.exists(backup_path)
  88. # Run without --skip-backup to create the backup folder
  89. with pytest.raises(SystemExit):
  90. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path]):
  91. _MigrateCLI.parse_arguments()
  92. assert os.path.exists(backup_path)
  93. # restore the backup
  94. with pytest.raises(SystemExit):
  95. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--restore"]):
  96. _MigrateCLI.parse_arguments()
  97. assert f"Restored entities from the backup folder '{backup_path}' to '{data_path}'." in caplog.text
  98. assert not os.path.exists(backup_path)
  99. # Compare migrated .data folder with data_sample to ensure restoring the backup worked
  100. dircmp_result = filecmp.dircmp(data_path, "tests/core/_entity/data_sample")
  101. assert not dircmp_result.diff_files and not dircmp_result.left_only and not dircmp_result.right_only
  102. for subdir in dircmp_result.subdirs.values():
  103. assert not subdir.diff_files and not subdir.left_only and not subdir.right_only
  104. def test_migrate_fs_non_existing_folder(caplog):
  105. _MigrateCLI.create_parser()
  106. # Test migrate with a non-existing folder
  107. with pytest.raises(SystemExit) as err:
  108. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", "non-existing-folder"]):
  109. _MigrateCLI.parse_arguments()
  110. assert err.value.code == 1
  111. assert "Folder 'non-existing-folder' does not exist." in caplog.text
  112. @patch("taipy.core._entity._migrate_cli._migrate_sql_entities")
  113. def test_migrate_sql_specified_path(_migrate_sql_entities_mock, tmp_sqlite):
  114. _MigrateCLI.create_parser()
  115. # Test the _migrate_sql_entities is called once with the correct path
  116. with pytest.raises(SystemExit):
  117. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--skip-backup"]):
  118. _MigrateCLI.parse_arguments()
  119. assert _migrate_sql_entities_mock.assert_called_once_with(path=tmp_sqlite)
  120. def test_migrate_sql_backup_and_remove(caplog, tmp_sqlite):
  121. _MigrateCLI.create_parser()
  122. # Create the .sqlite file to test
  123. with open(tmp_sqlite, "w") as f:
  124. f.write("")
  125. file_name, file_extension = tmp_sqlite.rsplit(".", 1)
  126. backup_sqlite = f"{file_name}_backup.{file_extension}"
  127. # Remove backup when it does not exist should raise an error
  128. with pytest.raises(SystemExit) as err:
  129. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--remove-backup"]):
  130. _MigrateCLI.parse_arguments()
  131. assert err.value.code == 1
  132. assert f"The backup database '{backup_sqlite}' does not exist." in caplog.text
  133. assert not os.path.exists(backup_sqlite)
  134. # Run without --skip-backup to create the backup database
  135. with pytest.raises((SystemExit, OperationalError)):
  136. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite]):
  137. _MigrateCLI.parse_arguments()
  138. assert os.path.exists(backup_sqlite)
  139. # Remove backup
  140. with pytest.raises(SystemExit):
  141. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--remove-backup"]):
  142. _MigrateCLI.parse_arguments()
  143. assert f"Removed backup entities from the backup database '{backup_sqlite}'." in caplog.text
  144. assert not os.path.exists(backup_sqlite)
  145. @pytest.mark.skipif(sys.platform == "win32", reason="Does not run on windows due to PermissionError: [WinError 32]")
  146. def test_migrate_sql_backup_and_restore(caplog, tmp_sqlite):
  147. _MigrateCLI.create_parser()
  148. # Create the .sqlite file to test
  149. with open(tmp_sqlite, "w") as f:
  150. f.write("")
  151. file_name, file_extension = tmp_sqlite.rsplit(".", 1)
  152. backup_sqlite = f"{file_name}_backup.{file_extension}"
  153. # Restore backup when it does not exist should raise an error
  154. with pytest.raises(SystemExit) as err:
  155. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--restore"]):
  156. _MigrateCLI.parse_arguments()
  157. assert err.value.code == 1
  158. assert f"The backup database '{backup_sqlite}' does not exist." in caplog.text
  159. assert not os.path.exists(backup_sqlite)
  160. # Run without --skip-backup to create the backup database
  161. with pytest.raises((SystemExit, OperationalError)):
  162. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite]):
  163. _MigrateCLI.parse_arguments()
  164. assert os.path.exists(backup_sqlite)
  165. # Restore the backup
  166. with pytest.raises(SystemExit):
  167. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--restore"]):
  168. _MigrateCLI.parse_arguments()
  169. assert f"Restored entities from the backup database '{backup_sqlite}' to '{tmp_sqlite}'." in caplog.text
  170. assert not os.path.exists(backup_sqlite)
  171. def test_migrate_sql_non_existing_path(caplog):
  172. _MigrateCLI.create_parser()
  173. # Test migrate without providing a path
  174. with pytest.raises(SystemExit) as err:
  175. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql"]):
  176. _MigrateCLI.parse_arguments()
  177. assert err.value.code == 1
  178. assert "Missing the required sqlite path." in caplog.text
  179. caplog.clear()
  180. # Test migrate with a non-existing-path.sqlite file
  181. with pytest.raises(SystemExit) as err:
  182. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", "non-existing-path.sqlite"]):
  183. _MigrateCLI.parse_arguments()
  184. assert err.value.code == 1
  185. assert "File 'non-existing-path.sqlite' does not exist." in caplog.text
  186. @patch("taipy.core._entity._migrate_cli._migrate_mongo_entities")
  187. def test_call_to_migrate_mongo(_migrate_mongo_entities_mock):
  188. _MigrateCLI.create_parser()
  189. with pytest.raises(SystemExit):
  190. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo"]):
  191. _MigrateCLI.parse_arguments()
  192. assert _migrate_mongo_entities_mock.assert_called_once_with()
  193. with pytest.raises(SystemExit):
  194. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "host", "port", "user", "password"]):
  195. _MigrateCLI.parse_arguments()
  196. assert _migrate_mongo_entities_mock.assert_called_once_with("host", "port", "user", "password")
  197. @mongomock.patch(servers=(("localhost", 27017),))
  198. def test_migrate_mongo_backup_and_remove(caplog):
  199. _MigrateCLI.create_parser()
  200. mongo_backup_path = ".mongo_backup"
  201. # Remove backup when it does not exist should raise an error
  202. with pytest.raises(SystemExit) as err:
  203. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--remove-backup"]):
  204. _MigrateCLI.parse_arguments()
  205. assert err.value.code == 1
  206. assert f"The backup folder '{mongo_backup_path}' does not exist." in caplog.text
  207. assert not os.path.exists(mongo_backup_path)
  208. # Run without --skip-backup to create the backup database
  209. with pytest.raises(SystemExit):
  210. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo"]):
  211. _MigrateCLI.parse_arguments()
  212. assert os.path.exists(mongo_backup_path)
  213. # Remove backup
  214. with pytest.raises(SystemExit):
  215. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--remove-backup"]):
  216. _MigrateCLI.parse_arguments()
  217. assert f"Removed backup entities from the backup folder '{mongo_backup_path}'." in caplog.text
  218. assert not os.path.exists(mongo_backup_path)
  219. @mongomock.patch(servers=(("localhost", 27017),))
  220. def test_migrate_mongo_backup_and_restore(caplog):
  221. _MigrateCLI.create_parser()
  222. mongo_backup_path = ".mongo_backup"
  223. # Restore backup when it does not exist should raise an error
  224. with pytest.raises(SystemExit) as err:
  225. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--restore"]):
  226. _MigrateCLI.parse_arguments()
  227. assert err.value.code == 1
  228. assert f"The backup folder '{mongo_backup_path}' does not exist." in caplog.text
  229. assert not os.path.exists(mongo_backup_path)
  230. # Run without --skip-backup to create the backup database
  231. with pytest.raises(SystemExit):
  232. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo"]):
  233. _MigrateCLI.parse_arguments()
  234. assert os.path.exists(mongo_backup_path)
  235. # Restore the backup
  236. with pytest.raises(SystemExit):
  237. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--restore"]):
  238. _MigrateCLI.parse_arguments()
  239. assert f"Restored entities from the backup folder '{mongo_backup_path}'." in caplog.text
  240. assert not os.path.exists(mongo_backup_path)
  241. def test_not_provide_valid_repository_type(caplog):
  242. _MigrateCLI.create_parser()
  243. with pytest.raises(SystemExit):
  244. with patch("sys.argv", ["prog", "migrate"]):
  245. _MigrateCLI.parse_arguments()
  246. assert "the following arguments are required: --repository-type" in caplog.text
  247. with pytest.raises(SystemExit):
  248. with patch("sys.argv", ["prog", "migrate", "--repository-type"]):
  249. _MigrateCLI.parse_arguments()
  250. assert "argument --repository-type: expected at least one argument" in caplog.text
  251. with pytest.raises(SystemExit):
  252. with patch("sys.argv", ["prog", "migrate", "--repository-type", "invalid-repository-type"]):
  253. _MigrateCLI.parse_arguments()
  254. assert "Unknown repository type invalid-repository-type" in caplog.text