|
| 1 | +"""Tests for json_dump_if_changed functionality.""" |
| 2 | + |
| 3 | +import codecs |
| 4 | +import json |
| 5 | +import os |
| 6 | +import tempfile |
| 7 | +from unittest.mock import Mock, patch |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from github_backup import github_backup |
| 12 | + |
| 13 | + |
| 14 | +class TestJsonDumpIfChanged: |
| 15 | + """Test suite for json_dump_if_changed function.""" |
| 16 | + |
| 17 | + def test_writes_new_file(self): |
| 18 | + """Should write file when it doesn't exist.""" |
| 19 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 20 | + output_file = os.path.join(tmpdir, "test.json") |
| 21 | + test_data = {"key": "value", "number": 42} |
| 22 | + |
| 23 | + result = github_backup.json_dump_if_changed(test_data, output_file) |
| 24 | + |
| 25 | + assert result is True |
| 26 | + assert os.path.exists(output_file) |
| 27 | + |
| 28 | + # Verify content matches expected format |
| 29 | + with codecs.open(output_file, "r", encoding="utf-8") as f: |
| 30 | + content = f.read() |
| 31 | + loaded = json.loads(content) |
| 32 | + assert loaded == test_data |
| 33 | + |
| 34 | + def test_skips_unchanged_file(self): |
| 35 | + """Should skip write when content is identical.""" |
| 36 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 37 | + output_file = os.path.join(tmpdir, "test.json") |
| 38 | + test_data = {"key": "value", "number": 42} |
| 39 | + |
| 40 | + # First write |
| 41 | + result1 = github_backup.json_dump_if_changed(test_data, output_file) |
| 42 | + assert result1 is True |
| 43 | + |
| 44 | + # Get the initial mtime |
| 45 | + mtime1 = os.path.getmtime(output_file) |
| 46 | + |
| 47 | + # Second write with same data |
| 48 | + result2 = github_backup.json_dump_if_changed(test_data, output_file) |
| 49 | + assert result2 is False |
| 50 | + |
| 51 | + # File should not have been modified |
| 52 | + mtime2 = os.path.getmtime(output_file) |
| 53 | + assert mtime1 == mtime2 |
| 54 | + |
| 55 | + def test_writes_when_content_changed(self): |
| 56 | + """Should write file when content has changed.""" |
| 57 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 58 | + output_file = os.path.join(tmpdir, "test.json") |
| 59 | + test_data1 = {"key": "value1"} |
| 60 | + test_data2 = {"key": "value2"} |
| 61 | + |
| 62 | + # First write |
| 63 | + result1 = github_backup.json_dump_if_changed(test_data1, output_file) |
| 64 | + assert result1 is True |
| 65 | + |
| 66 | + # Second write with different data |
| 67 | + result2 = github_backup.json_dump_if_changed(test_data2, output_file) |
| 68 | + assert result2 is True |
| 69 | + |
| 70 | + # Verify new content |
| 71 | + with codecs.open(output_file, "r", encoding="utf-8") as f: |
| 72 | + loaded = json.load(f) |
| 73 | + assert loaded == test_data2 |
| 74 | + |
| 75 | + def test_uses_consistent_formatting(self): |
| 76 | + """Should use same JSON formatting as json_dump.""" |
| 77 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 78 | + output_file = os.path.join(tmpdir, "test.json") |
| 79 | + test_data = {"z": "last", "a": "first", "m": "middle"} |
| 80 | + |
| 81 | + github_backup.json_dump_if_changed(test_data, output_file) |
| 82 | + |
| 83 | + with codecs.open(output_file, "r", encoding="utf-8") as f: |
| 84 | + content = f.read() |
| 85 | + |
| 86 | + # Check for consistent formatting: |
| 87 | + # - sorted keys |
| 88 | + # - 4-space indent |
| 89 | + # - comma-colon-space separator |
| 90 | + expected = json.dumps( |
| 91 | + test_data, |
| 92 | + ensure_ascii=False, |
| 93 | + sort_keys=True, |
| 94 | + indent=4, |
| 95 | + separators=(",", ": "), |
| 96 | + ) |
| 97 | + assert content == expected |
| 98 | + |
| 99 | + def test_atomic_write_always_used(self): |
| 100 | + """Should always use temp file and rename for atomic writes.""" |
| 101 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 102 | + output_file = os.path.join(tmpdir, "test.json") |
| 103 | + test_data = {"key": "value"} |
| 104 | + |
| 105 | + result = github_backup.json_dump_if_changed(test_data, output_file) |
| 106 | + |
| 107 | + assert result is True |
| 108 | + assert os.path.exists(output_file) |
| 109 | + |
| 110 | + # Temp file should not exist after atomic write |
| 111 | + temp_file = output_file + ".temp" |
| 112 | + assert not os.path.exists(temp_file) |
| 113 | + |
| 114 | + # Verify content |
| 115 | + with codecs.open(output_file, "r", encoding="utf-8") as f: |
| 116 | + loaded = json.load(f) |
| 117 | + assert loaded == test_data |
| 118 | + |
| 119 | + def test_handles_unicode_content(self): |
| 120 | + """Should correctly handle Unicode content.""" |
| 121 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 122 | + output_file = os.path.join(tmpdir, "test.json") |
| 123 | + test_data = { |
| 124 | + "emoji": "🚀", |
| 125 | + "chinese": "你好", |
| 126 | + "arabic": "مرحبا", |
| 127 | + "cyrillic": "Привет" |
| 128 | + } |
| 129 | + |
| 130 | + result = github_backup.json_dump_if_changed(test_data, output_file) |
| 131 | + assert result is True |
| 132 | + |
| 133 | + # Verify Unicode is preserved |
| 134 | + with codecs.open(output_file, "r", encoding="utf-8") as f: |
| 135 | + loaded = json.load(f) |
| 136 | + assert loaded == test_data |
| 137 | + |
| 138 | + # Second write should skip |
| 139 | + result2 = github_backup.json_dump_if_changed(test_data, output_file) |
| 140 | + assert result2 is False |
| 141 | + |
| 142 | + def test_handles_complex_nested_data(self): |
| 143 | + """Should handle complex nested data structures.""" |
| 144 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 145 | + output_file = os.path.join(tmpdir, "test.json") |
| 146 | + test_data = { |
| 147 | + "users": [ |
| 148 | + {"id": 1, "name": "Alice", "tags": ["admin", "user"]}, |
| 149 | + {"id": 2, "name": "Bob", "tags": ["user"]} |
| 150 | + ], |
| 151 | + "metadata": { |
| 152 | + "version": "1.0", |
| 153 | + "nested": {"deep": {"value": 42}} |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + result = github_backup.json_dump_if_changed(test_data, output_file) |
| 158 | + assert result is True |
| 159 | + |
| 160 | + # Verify structure is preserved |
| 161 | + with codecs.open(output_file, "r", encoding="utf-8") as f: |
| 162 | + loaded = json.load(f) |
| 163 | + assert loaded == test_data |
| 164 | + |
| 165 | + def test_overwrites_on_unicode_decode_error(self): |
| 166 | + """Should overwrite if existing file has invalid UTF-8.""" |
| 167 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 168 | + output_file = os.path.join(tmpdir, "test.json") |
| 169 | + test_data = {"key": "value"} |
| 170 | + |
| 171 | + # Write invalid UTF-8 bytes |
| 172 | + with open(output_file, "wb") as f: |
| 173 | + f.write(b'\xff\xfe invalid utf-8') |
| 174 | + |
| 175 | + # Should catch UnicodeDecodeError and overwrite |
| 176 | + result = github_backup.json_dump_if_changed(test_data, output_file) |
| 177 | + assert result is True |
| 178 | + |
| 179 | + # Verify new content was written |
| 180 | + with codecs.open(output_file, "r", encoding="utf-8") as f: |
| 181 | + loaded = json.load(f) |
| 182 | + assert loaded == test_data |
| 183 | + |
| 184 | + def test_key_order_independence(self): |
| 185 | + """Should treat differently-ordered dicts as same if keys/values match.""" |
| 186 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 187 | + output_file = os.path.join(tmpdir, "test.json") |
| 188 | + |
| 189 | + # Write first dict |
| 190 | + data1 = {"z": 1, "a": 2, "m": 3} |
| 191 | + github_backup.json_dump_if_changed(data1, output_file) |
| 192 | + |
| 193 | + # Try to write same data but different order |
| 194 | + data2 = {"a": 2, "m": 3, "z": 1} |
| 195 | + result = github_backup.json_dump_if_changed(data2, output_file) |
| 196 | + |
| 197 | + # Should skip because content is the same (keys are sorted) |
| 198 | + assert result is False |
| 199 | + |
| 200 | + |
| 201 | +if __name__ == "__main__": |
| 202 | + pytest.main([__file__, "-v"]) |
0 commit comments