Files
cmshoppe/tests/test_update_health.py
T

91 lines
3.0 KiB
Python
Raw Normal View History

import json
import tempfile
import unittest
from pathlib import Path
from app import update_health
class UpdateHealthTests(unittest.TestCase):
def test_context_and_atomic_health_markers(self):
with tempfile.TemporaryDirectory() as temp_dir:
context = update_health.context_from_argv(
[
"cmshopee.exe",
"--update-transaction",
"transaction-1234",
"--update-target-version",
"2.0.0",
],
temp_dir,
"2.0.0",
)
self.assertIsNotNone(context)
update_health.write_health(context, "process_started")
update_health.write_health(context, "main_window_ready")
payload = json.loads(context.health_path.read_text(encoding="utf-8"))
self.assertEqual("main_window_ready", payload["status"])
self.assertEqual("2.0.0", payload["target_version"])
def test_context_rejects_wrong_version_or_transaction(self):
self.assertIsNone(
update_health.context_from_argv(
["app", "--update-transaction", "bad", "--update-target-version", "2.0.0"],
".",
"2.0.0",
)
)
self.assertIsNone(
update_health.context_from_argv(
[
"app",
"--update-transaction",
"transaction-1234",
"--update-target-version",
"9.0.0",
],
".",
"2.0.0",
)
)
def test_failed_release_fuse_is_version_and_hash_specific(self):
with tempfile.TemporaryDirectory() as temp_dir:
first = update_health.record_failed_release(
temp_dir,
"2.0.0",
"a" * 64,
"新版程序未就绪",
)
second = update_health.record_failed_release(
temp_dir,
"2.0.0",
"a" * 64,
"再次失败",
)
self.assertEqual(1, first["attempts"])
self.assertEqual(2, second["attempts"])
self.assertIsNotNone(
update_health.get_failed_release(temp_dir, "2.0.0", "a" * 64)
)
self.assertIsNone(
update_health.get_failed_release(temp_dir, "2.0.0", "b" * 64)
)
self.assertIsNone(
update_health.get_failed_release(temp_dir, "2.0.1", "a" * 64)
)
def test_failed_release_file_does_not_touch_data(self):
with tempfile.TemporaryDirectory() as temp_dir:
data_file = Path(temp_dir) / "data" / "cmshopee.db"
data_file.parent.mkdir()
data_file.write_bytes(b"business")
update_health.record_failed_release(temp_dir, "2.0.0", "a" * 64, "失败")
self.assertEqual(b"business", data_file.read_bytes())
if __name__ == "__main__":
unittest.main()