test_migrate_cli.py 14 KB

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