test_migrate_cli.py 14 KB

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