test_migrate_cli.py 15 KB

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