test_migrate_cli.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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._entrypoint import _entrypoint
  20. from taipy.core._entity._migrate_cli import _MigrateCLI
  21. def test_migrate_cli_with_wrong_repository_type_arguments(caplog):
  22. with patch("sys.argv", ["prog", "migrate", "--reposiory-tyep", "filesystem"]):
  23. with pytest.raises(SystemExit):
  24. _entrypoint()
  25. assert "Unknown arguments: --reposiory-tyep. Did you mean: --repository-type?" in caplog.text
  26. def test_migrate_cli_with_wrong_skip_backup_arguments(caplog):
  27. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", "--slip-backup"]):
  28. with pytest.raises(SystemExit):
  29. _entrypoint()
  30. assert "Unknown arguments: --slip-backup. Did you mean: --skip-backup?" in caplog.text
  31. @pytest.fixture(scope="function", autouse=True)
  32. def clean_data_folder():
  33. if os.path.exists("tests/core/_entity/.data"):
  34. shutil.rmtree("tests/core/_entity/.data")
  35. if os.path.exists("tests/core/_entity/.taipy"):
  36. shutil.rmtree("tests/core/_entity/.taipy")
  37. yield
  38. def test_migrate_fs_default(caplog):
  39. _MigrateCLI.create_parser()
  40. # Test migrate with default .data folder
  41. with pytest.raises(SystemExit):
  42. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", "--skip-backup"]):
  43. _MigrateCLI.handle_command()
  44. assert "Starting entity migration from '.taipy/' folder" in caplog.text
  45. def test_migrate_fs_specified_folder(caplog, mocker):
  46. mocker.patch("taipy.core._entity._migrate._utils.version", return_value="3.1.0")
  47. _MigrateCLI.create_parser()
  48. # Copy data_sample to .data folder for testing
  49. data_sample_path = "tests/core/_entity/data_sample"
  50. data_path = "tests/core/_entity/.data"
  51. shutil.copytree(data_sample_path, data_path)
  52. # Run with --skip-backup to only test the migration
  53. with pytest.raises(SystemExit):
  54. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--skip-backup"]):
  55. _MigrateCLI.handle_command()
  56. assert f"Starting entity migration from '{data_path}' folder" in caplog.text
  57. # Compare migrated .data folder with data_sample_migrated
  58. dircmp_result = filecmp.dircmp(data_path, "tests/core/_entity/data_sample_migrated")
  59. assert not dircmp_result.diff_files and not dircmp_result.left_only and not dircmp_result.right_only
  60. for subdir in dircmp_result.subdirs.values():
  61. assert not subdir.diff_files and not subdir.left_only and not subdir.right_only
  62. def test_migrate_fs_backup_and_remove(caplog, mocker):
  63. mocker.patch("taipy.core._entity._migrate._utils.version", return_value="3.1.0")
  64. _MigrateCLI.create_parser()
  65. # Copy data_sample to .data folder for testing
  66. data_sample_path = "tests/core/_entity/data_sample"
  67. data_path = "tests/core/_entity/.data"
  68. backup_path = "tests/core/_entity/.data_backup"
  69. shutil.copytree(data_sample_path, data_path)
  70. # Remove backup when it does not exist should raise an error
  71. with pytest.raises(SystemExit) as err:
  72. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--remove-backup"]):
  73. _MigrateCLI.handle_command()
  74. assert err.value.code == 1
  75. assert f"The backup folder '{backup_path}' does not exist." in caplog.text
  76. assert not os.path.exists(backup_path)
  77. # Run without --skip-backup to create the backup folder
  78. with pytest.raises(SystemExit):
  79. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path]):
  80. _MigrateCLI.handle_command()
  81. assert f"Backed up entities from '{data_path}' to '{backup_path}' folder before migration." in caplog.text
  82. assert os.path.exists(backup_path)
  83. # Remove backup
  84. with pytest.raises(SystemExit):
  85. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--remove-backup"]):
  86. _MigrateCLI.handle_command()
  87. assert f"Removed backup entities from the backup folder '{backup_path}'." in caplog.text
  88. assert not os.path.exists(backup_path)
  89. def test_migrate_fs_backup_and_restore(caplog, mocker):
  90. mocker.patch("taipy.core._entity._migrate._utils.version", return_value="3.1.0")
  91. _MigrateCLI.create_parser()
  92. # Copy data_sample to .data folder for testing
  93. data_sample_path = "tests/core/_entity/data_sample"
  94. data_path = "tests/core/_entity/.data"
  95. backup_path = "tests/core/_entity/.data_backup"
  96. shutil.copytree(data_sample_path, data_path)
  97. # Restore backup when it does not exist should raise an error
  98. with pytest.raises(SystemExit) as err:
  99. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--restore"]):
  100. _MigrateCLI.handle_command()
  101. assert err.value.code == 1
  102. assert f"The backup folder '{backup_path}' does not exist." in caplog.text
  103. assert not os.path.exists(backup_path)
  104. # Run without --skip-backup to create the backup folder
  105. with pytest.raises(SystemExit):
  106. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path]):
  107. _MigrateCLI.handle_command()
  108. assert os.path.exists(backup_path)
  109. # restore the backup
  110. with pytest.raises(SystemExit):
  111. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", data_path, "--restore"]):
  112. _MigrateCLI.handle_command()
  113. assert f"Restored entities from the backup folder '{backup_path}' to '{data_path}'." in caplog.text
  114. assert not os.path.exists(backup_path)
  115. # Compare migrated .data folder with data_sample to ensure restoring the backup worked
  116. dircmp_result = filecmp.dircmp(data_path, "tests/core/_entity/data_sample")
  117. assert not dircmp_result.diff_files and not dircmp_result.left_only and not dircmp_result.right_only
  118. for subdir in dircmp_result.subdirs.values():
  119. assert not subdir.diff_files and not subdir.left_only and not subdir.right_only
  120. def test_migrate_fs_non_existing_folder(caplog):
  121. _MigrateCLI.create_parser()
  122. # Test migrate with a non-existing folder
  123. with pytest.raises(SystemExit) as err:
  124. with patch("sys.argv", ["prog", "migrate", "--repository-type", "filesystem", "non-existing-folder"]):
  125. _MigrateCLI.handle_command()
  126. assert err.value.code == 1
  127. assert "Folder 'non-existing-folder' does not exist." in caplog.text
  128. @patch("taipy.core._entity._migrate_cli._migrate_sql_entities")
  129. def test_migrate_sql_specified_path(_migrate_sql_entities_mock, tmp_sqlite):
  130. _MigrateCLI.create_parser()
  131. # Test the _migrate_sql_entities is called once with the correct path
  132. with pytest.raises(SystemExit):
  133. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--skip-backup"]):
  134. _MigrateCLI.handle_command()
  135. assert _migrate_sql_entities_mock.assert_called_once_with(path=tmp_sqlite)
  136. def test_migrate_sql_backup_and_remove(caplog, tmp_sqlite):
  137. _MigrateCLI.create_parser()
  138. # Create the .sqlite file to test
  139. with open(tmp_sqlite, "w") as f:
  140. f.write("")
  141. file_name, file_extension = tmp_sqlite.rsplit(".", 1)
  142. backup_sqlite = f"{file_name}_backup.{file_extension}"
  143. # Remove backup when it does not exist should raise an error
  144. with pytest.raises(SystemExit) as err:
  145. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--remove-backup"]):
  146. _MigrateCLI.handle_command()
  147. assert err.value.code == 1
  148. assert f"The backup database '{backup_sqlite}' does not exist." in caplog.text
  149. assert not os.path.exists(backup_sqlite)
  150. # Run without --skip-backup to create the backup database
  151. with pytest.raises((SystemExit, OperationalError)):
  152. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite]):
  153. _MigrateCLI.handle_command()
  154. assert os.path.exists(backup_sqlite)
  155. # Remove backup
  156. with pytest.raises(SystemExit):
  157. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--remove-backup"]):
  158. _MigrateCLI.handle_command()
  159. assert f"Removed backup entities from the backup database '{backup_sqlite}'." in caplog.text
  160. assert not os.path.exists(backup_sqlite)
  161. @pytest.mark.skipif(sys.platform == "win32", reason="Does not run on windows due to PermissionError: [WinError 32]")
  162. def test_migrate_sql_backup_and_restore(caplog, tmp_sqlite):
  163. _MigrateCLI.create_parser()
  164. # Create the .sqlite file to test
  165. with open(tmp_sqlite, "w") as f:
  166. f.write("")
  167. file_name, file_extension = tmp_sqlite.rsplit(".", 1)
  168. backup_sqlite = f"{file_name}_backup.{file_extension}"
  169. # Restore backup when it does not exist should raise an error
  170. with pytest.raises(SystemExit) as err:
  171. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--restore"]):
  172. _MigrateCLI.handle_command()
  173. assert err.value.code == 1
  174. assert f"The backup database '{backup_sqlite}' does not exist." in caplog.text
  175. assert not os.path.exists(backup_sqlite)
  176. # Run without --skip-backup to create the backup database
  177. with pytest.raises((SystemExit, OperationalError)):
  178. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite]):
  179. _MigrateCLI.handle_command()
  180. assert os.path.exists(backup_sqlite)
  181. # Restore the backup
  182. with pytest.raises(SystemExit):
  183. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--restore"]):
  184. _MigrateCLI.handle_command()
  185. assert f"Restored entities from the backup database '{backup_sqlite}' to '{tmp_sqlite}'." in caplog.text
  186. assert not os.path.exists(backup_sqlite)
  187. def test_migrate_sql_non_existing_path(caplog):
  188. _MigrateCLI.create_parser()
  189. # Test migrate without providing a path
  190. with pytest.raises(SystemExit) as err:
  191. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql"]):
  192. _MigrateCLI.handle_command()
  193. assert err.value.code == 1
  194. assert "Missing the required sqlite path." in caplog.text
  195. caplog.clear()
  196. # Test migrate with a non-existing-path.sqlite file
  197. with pytest.raises(SystemExit) as err:
  198. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", "non-existing-path.sqlite"]):
  199. _MigrateCLI.handle_command()
  200. assert err.value.code == 1
  201. assert "File 'non-existing-path.sqlite' does not exist." in caplog.text
  202. @patch("taipy.core._entity._migrate_cli._migrate_mongo_entities")
  203. def test_call_to_migrate_mongo(_migrate_mongo_entities_mock):
  204. _MigrateCLI.create_parser()
  205. with pytest.raises(SystemExit):
  206. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo"]):
  207. _MigrateCLI.handle_command()
  208. assert _migrate_mongo_entities_mock.assert_called_once_with()
  209. with pytest.raises(SystemExit):
  210. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "host", "port", "user", "password"]):
  211. _MigrateCLI.handle_command()
  212. assert _migrate_mongo_entities_mock.assert_called_once_with("host", "port", "user", "password")
  213. @mongomock.patch(servers=(("localhost", 27017),))
  214. def test_migrate_mongo_backup_and_remove(caplog):
  215. _MigrateCLI.create_parser()
  216. mongo_backup_path = ".mongo_backup"
  217. # Remove backup when it does not exist should raise an error
  218. with pytest.raises(SystemExit) as err:
  219. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--remove-backup"]):
  220. _MigrateCLI.handle_command()
  221. assert err.value.code == 1
  222. assert f"The backup folder '{mongo_backup_path}' does not exist." in caplog.text
  223. assert not os.path.exists(mongo_backup_path)
  224. # Run without --skip-backup to create the backup database
  225. with pytest.raises(SystemExit):
  226. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo"]):
  227. _MigrateCLI.handle_command()
  228. assert os.path.exists(mongo_backup_path)
  229. # Remove backup
  230. with pytest.raises(SystemExit):
  231. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--remove-backup"]):
  232. _MigrateCLI.handle_command()
  233. assert f"Removed backup entities from the backup folder '{mongo_backup_path}'." in caplog.text
  234. assert not os.path.exists(mongo_backup_path)
  235. @mongomock.patch(servers=(("localhost", 27017),))
  236. def test_migrate_mongo_backup_and_restore(caplog):
  237. _MigrateCLI.create_parser()
  238. mongo_backup_path = ".mongo_backup"
  239. # Restore backup when it does not exist should raise an error
  240. with pytest.raises(SystemExit) as err:
  241. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--restore"]):
  242. _MigrateCLI.handle_command()
  243. assert err.value.code == 1
  244. assert f"The backup folder '{mongo_backup_path}' does not exist." in caplog.text
  245. assert not os.path.exists(mongo_backup_path)
  246. # Run without --skip-backup to create the backup database
  247. with pytest.raises(SystemExit):
  248. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo"]):
  249. _MigrateCLI.handle_command()
  250. assert os.path.exists(mongo_backup_path)
  251. # Restore the backup
  252. with pytest.raises(SystemExit):
  253. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--restore"]):
  254. _MigrateCLI.handle_command()
  255. assert f"Restored entities from the backup folder '{mongo_backup_path}'." in caplog.text
  256. assert not os.path.exists(mongo_backup_path)
  257. def test_not_provide_valid_repository_type(caplog):
  258. _MigrateCLI.create_parser()
  259. with pytest.raises(SystemExit):
  260. with patch("sys.argv", ["prog", "migrate"]):
  261. _MigrateCLI.handle_command()
  262. assert "the following arguments are required: --repository-type" in caplog.text
  263. with pytest.raises(SystemExit):
  264. with patch("sys.argv", ["prog", "migrate", "--repository-type"]):
  265. _MigrateCLI.handle_command()
  266. assert "argument --repository-type: expected at least one argument" in caplog.text
  267. with pytest.raises(SystemExit):
  268. with patch("sys.argv", ["prog", "migrate", "--repository-type", "invalid-repository-type"]):
  269. _MigrateCLI.handle_command()
  270. assert "Unknown repository type invalid-repository-type" in caplog.text