test_migrate_cli.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. def test_migrate_sql_backup_and_restore(caplog, tmp_sqlite):
  162. _MigrateCLI.create_parser()
  163. # Create the .sqlite file to test
  164. with open(tmp_sqlite, "w") as f:
  165. f.write("")
  166. file_name, file_extension = tmp_sqlite.rsplit(".", 1)
  167. backup_sqlite = f"{file_name}_backup.{file_extension}"
  168. # Restore backup when it does not exist should raise an error
  169. with pytest.raises(SystemExit) as err:
  170. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--restore"]):
  171. _MigrateCLI.handle_command()
  172. assert err.value.code == 1
  173. assert f"The backup database '{backup_sqlite}' does not exist." in caplog.text
  174. assert not os.path.exists(backup_sqlite)
  175. # Run without --skip-backup to create the backup database
  176. with pytest.raises((SystemExit, OperationalError)):
  177. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite]):
  178. _MigrateCLI.handle_command()
  179. assert os.path.exists(backup_sqlite)
  180. # Restore the backup
  181. with pytest.raises(SystemExit):
  182. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", tmp_sqlite, "--restore"]):
  183. _MigrateCLI.handle_command()
  184. assert f"Restored entities from the backup database '{backup_sqlite}' to '{tmp_sqlite}'." in caplog.text
  185. assert not os.path.exists(backup_sqlite)
  186. def test_migrate_sql_non_existing_path(caplog):
  187. _MigrateCLI.create_parser()
  188. # Test migrate without providing a path
  189. with pytest.raises(SystemExit) as err:
  190. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql"]):
  191. _MigrateCLI.handle_command()
  192. assert err.value.code == 1
  193. assert "Missing the required sqlite path." in caplog.text
  194. caplog.clear()
  195. # Test migrate with a non-existing-path.sqlite file
  196. with pytest.raises(SystemExit) as err:
  197. with patch("sys.argv", ["prog", "migrate", "--repository-type", "sql", "non-existing-path.sqlite"]):
  198. _MigrateCLI.handle_command()
  199. assert err.value.code == 1
  200. assert "File 'non-existing-path.sqlite' does not exist." in caplog.text
  201. @patch("taipy.core._entity._migrate_cli._migrate_mongo_entities")
  202. def test_call_to_migrate_mongo(_migrate_mongo_entities_mock):
  203. _MigrateCLI.create_parser()
  204. with pytest.raises(SystemExit):
  205. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo"]):
  206. _MigrateCLI.handle_command()
  207. assert _migrate_mongo_entities_mock.assert_called_once_with()
  208. with pytest.raises(SystemExit):
  209. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "host", "port", "user", "password"]):
  210. _MigrateCLI.handle_command()
  211. assert _migrate_mongo_entities_mock.assert_called_once_with("host", "port", "user", "password")
  212. @mongomock.patch(servers=(("localhost", 27017),))
  213. def test_migrate_mongo_backup_and_remove(caplog):
  214. _MigrateCLI.create_parser()
  215. mongo_backup_path = ".mongo_backup"
  216. # Remove backup when it does not exist should raise an error
  217. with pytest.raises(SystemExit) as err:
  218. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--remove-backup"]):
  219. _MigrateCLI.handle_command()
  220. assert err.value.code == 1
  221. assert f"The backup folder '{mongo_backup_path}' does not exist." in caplog.text
  222. assert not os.path.exists(mongo_backup_path)
  223. # Run without --skip-backup to create the backup database
  224. with pytest.raises(SystemExit):
  225. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo"]):
  226. _MigrateCLI.handle_command()
  227. assert os.path.exists(mongo_backup_path)
  228. # Remove backup
  229. with pytest.raises(SystemExit):
  230. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--remove-backup"]):
  231. _MigrateCLI.handle_command()
  232. assert f"Removed backup entities from the backup folder '{mongo_backup_path}'." in caplog.text
  233. assert not os.path.exists(mongo_backup_path)
  234. @mongomock.patch(servers=(("localhost", 27017),))
  235. def test_migrate_mongo_backup_and_restore(caplog):
  236. _MigrateCLI.create_parser()
  237. mongo_backup_path = ".mongo_backup"
  238. # Restore backup when it does not exist should raise an error
  239. with pytest.raises(SystemExit) as err:
  240. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--restore"]):
  241. _MigrateCLI.handle_command()
  242. assert err.value.code == 1
  243. assert f"The backup folder '{mongo_backup_path}' does not exist." in caplog.text
  244. assert not os.path.exists(mongo_backup_path)
  245. # Run without --skip-backup to create the backup database
  246. with pytest.raises(SystemExit):
  247. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo"]):
  248. _MigrateCLI.handle_command()
  249. assert os.path.exists(mongo_backup_path)
  250. # Restore the backup
  251. with pytest.raises(SystemExit):
  252. with patch("sys.argv", ["prog", "migrate", "--repository-type", "mongo", "--restore"]):
  253. _MigrateCLI.handle_command()
  254. assert f"Restored entities from the backup folder '{mongo_backup_path}'." in caplog.text
  255. assert not os.path.exists(mongo_backup_path)
  256. def test_not_provide_valid_repository_type(caplog):
  257. _MigrateCLI.create_parser()
  258. with pytest.raises(SystemExit):
  259. with patch("sys.argv", ["prog", "migrate"]):
  260. _MigrateCLI.handle_command()
  261. assert "the following arguments are required: --repository-type" in caplog.text
  262. with pytest.raises(SystemExit):
  263. with patch("sys.argv", ["prog", "migrate", "--repository-type"]):
  264. _MigrateCLI.handle_command()
  265. assert "argument --repository-type: expected at least one argument" in caplog.text
  266. with pytest.raises(SystemExit):
  267. with patch("sys.argv", ["prog", "migrate", "--repository-type", "invalid-repository-type"]):
  268. _MigrateCLI.handle_command()
  269. assert "Unknown repository type invalid-repository-type" in caplog.text