diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-07-03 20:56:34 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-07-03 20:56:34 +0300 |
| commit | c79cf21cd27dfed67d064b515a58ad6ef9aa499a (patch) | |
| tree | 14076b6f7ac4f4be4e3ad8c851e4926f6bd7c44f /tests | |
| parent | 887b26f923f01d46ee30d0f0397c5d2da907e03f (diff) | |
| download | fungus-launcher-c79cf21cd27dfed67d064b515a58ad6ef9aa499a.tar.gz fungus-launcher-c79cf21cd27dfed67d064b515a58ad6ef9aa499a.tar.xz | |
remove everything related to writing to monolithic games.json
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/conftest.py | 5 | ||||
| -rw-r--r-- | tests/test_game_model.py | 125 |
2 files changed, 12 insertions, 118 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 64039b7..0181757 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -67,9 +67,10 @@ def sample_games(): @pytest.fixture() def populated_games_file(games_file, sample_games): """Write sample games to a JSON file and return the path.""" - from fungus.game_model import save_games_json + import json - save_games_json(str(games_file), sample_games) + with open(games_file, "w", encoding="utf-8") as f: + json.dump([g.to_dict() for g in sample_games], f, ensure_ascii=False, indent=4) return games_file diff --git a/tests/test_game_model.py b/tests/test_game_model.py index 7558648..190f52e 100644 --- a/tests/test_game_model.py +++ b/tests/test_game_model.py @@ -3,16 +3,19 @@ import json import os + +def _write_games_json(path, games): + """Write a list of Game objects to a JSON file (test helper).""" + with open(path, "w", encoding="utf-8") as f: + json.dump([g.to_dict() for g in games], f, ensure_ascii=False, indent=4) + from fungus.game_model import ( Game, GameLibrary, MigrationResult, - backup_games_file, load_games_json, migrate_to_per_game, needs_migration, - save_games_json, - update_game_field, ) @@ -120,115 +123,6 @@ class TestLoadGamesJson: assert games == [] -# ── save_games_json ───────────────────────────────────────────────────── - - -class TestSaveGamesJson: - def test_save_and_reload(self, games_file, sample_games): - save_games_json(str(games_file), sample_games, backup=False) - games = load_games_json(str(games_file)) - assert len(games) == 2 - assert games[0].gameid == "umu-1234" - assert games[1].favorite is False # should be preserved - - def test_save_with_deleted_gameid(self, games_file, sample_games): - save_games_json( - str(games_file), sample_games, deleted_gameid="umu-1234", backup=False - ) - games = load_games_json(str(games_file)) - assert len(games) == 1 - assert games[0].gameid == "umu-5678" - - def test_save_hidden_games_preserved(self, games_file, sample_games): - save_games_json(str(games_file), sample_games, backup=False) - games = load_games_json(str(games_file)) - hidden_games = [g for g in games if g.hidden] - assert len(hidden_games) == 1 - assert hidden_games[0].gameid == "umu-5678" - - def test_save_empty_list(self, games_file): - save_games_json(str(games_file), [], backup=False) - with open(games_file) as f: - data = json.load(f) - assert data == [] - - def test_backup_is_created(self, games_file, sample_games, tmp_path): - # Write initial file - save_games_json(str(games_file), sample_games, backup=False) - initial_mtime = os.path.getmtime(str(games_file)) - - # Now save again with backup - save_games_json(str(games_file), sample_games, backup=True) - assert os.path.getmtime(str(games_file)) > initial_mtime - - -# ── update_game_field ─────────────────────────────────────────────────── - - -class TestUpdateGameField: - def test_update_existing_game(self, populated_games_file): - update_game_field( - str(populated_games_file), "umu-1234", "favorite", True - ) - games = load_games_json(str(populated_games_file)) - game = next(g for g in games if g.gameid == "umu-1234") - assert game.favorite is True - - def test_update_nonexistent_gameid(self, populated_games_file): - # Should not raise; silently skips - update_game_field( - str(populated_games_file), "nonexistent", "title", "New Title" - ) - games = load_games_json(str(populated_games_file)) - assert all(g.title != "New Title" for g in games) - - def test_update_playtime(self, populated_games_file): - update_game_field( - str(populated_games_file), "umu-5678", "playtime", 9999 - ) - games = load_games_json(str(populated_games_file)) - game = next(g for g in games if g.gameid == "umu-5678") - assert game.playtime == 9999 - - -# ── backup_games_file ─────────────────────────────────────────────────── - - -class TestBackupGamesFile: - def test_creates_backup(self, games_file, tmp_config_dir, sample_games): - - # Write a games file - save_games_json(str(games_file), sample_games, backup=False) - - # Set backup dir - backup_dest = tmp_config_dir / "backups" - - backup_games_file(str(games_file), backup_dir=str(backup_dest)) - - backups = list(backup_dest.glob("games-data-*.json")) - assert len(backups) == 1 - - def test_prunes_old_backups(self, games_file, tmp_config_dir, sample_games): - backup_dest = tmp_config_dir / "backups" - backup_dest.mkdir(parents=True, exist_ok=True) - - # Create 22 fake old backups - for i in range(22): - ts = f"2025-01-01_00-00-{i:02d}" - (backup_dest / f"games-data-{ts}.json").write_text("[]") - - save_games_json(str(games_file), sample_games, backup=False) - backup_games_file(str(games_file), backup_dir=str(backup_dest)) - - backups = list(backup_dest.glob("games-data-*.json")) - assert len(backups) <= 20 - - def test_nonexistent_source_does_nothing(self, tmp_config_dir): - backup_dest = tmp_config_dir / "backups" - backup_games_file("/nonexistent/path.json", backup_dir=str(backup_dest)) - assert not backup_dest.exists() - - # ── GameLibrary ───────────────────────────────────────────────────────── @@ -405,7 +299,7 @@ class TestMigrateToPerGame: games_json = tmp_path / "games.json" games_dir = tmp_path / "games" # Write monolithic JSON - save_games_json(str(games_json), sample_games, backup=False) + _write_games_json(str(games_json), sample_games) assert games_json.exists() result = migrate_to_per_game(str(games_json), str(games_dir)) @@ -445,13 +339,12 @@ class TestMigrateToPerGame: games_json = tmp_path / "games.json" games_dir = tmp_path / "games" # Two games with same gameid - save_games_json( + _write_games_json( str(games_json), [ Game(gameid="dup", title="First", path="/first"), Game(gameid="dup", title="Second", path="/second"), ], - backup=False, ) result = migrate_to_per_game(str(games_json), str(games_dir)) @@ -465,7 +358,7 @@ class TestMigrateToPerGame: def test_migrate_dry_run(self, tmp_path, sample_games): games_json = tmp_path / "games.json" games_dir = tmp_path / "games" - save_games_json(str(games_json), sample_games, backup=False) + _write_games_json(str(games_json), sample_games) result = migrate_to_per_game( str(games_json), str(games_dir), dry_run=True |
