|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import firestore_patch |
| 16 | +import pathlib |
| 17 | +import shutil |
| 18 | +import tempfile |
| 19 | +import unittest |
| 20 | + |
| 21 | + |
| 22 | +class LoadLevelDbVersionTest(unittest.TestCase): |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + super().setUp() |
| 26 | + temp_dir = pathlib.Path(tempfile.mkdtemp()) |
| 27 | + self.addCleanup(shutil.rmtree, temp_dir) |
| 28 | + self.temp_file = temp_dir / "temp_file.txt" |
| 29 | + |
| 30 | + def test_loads_correct_version(self): |
| 31 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 32 | + print("blah1", file=f) |
| 33 | + print("set(version 1.23)", file=f) |
| 34 | + print("blah2", file=f) |
| 35 | + |
| 36 | + version = firestore_patch.load_leveldb_version(self.temp_file) |
| 37 | + |
| 38 | + self.assertEqual(version, "1.23") |
| 39 | + |
| 40 | + def test_ignores_whitespace(self): |
| 41 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 42 | + print(" set ( version 1.23 ) ", file=f) |
| 43 | + |
| 44 | + version = firestore_patch.load_leveldb_version(self.temp_file) |
| 45 | + |
| 46 | + self.assertEqual(version, "1.23") |
| 47 | + |
| 48 | + def test_case_insensitive(self): |
| 49 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 50 | + print("SeT(vErSiOn 1.23)", file=f) |
| 51 | + |
| 52 | + version = firestore_patch.load_leveldb_version(self.temp_file) |
| 53 | + |
| 54 | + self.assertEqual(version, "1.23") |
| 55 | + |
| 56 | + def test_version_not_found_raises_error(self): |
| 57 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 58 | + print("aaa", file=f) |
| 59 | + print("bbb", file=f) |
| 60 | + |
| 61 | + with self.assertRaises(firestore_patch.LevelDbVersionLineError) as cm: |
| 62 | + firestore_patch.load_leveldb_version(self.temp_file) |
| 63 | + |
| 64 | + self.assertIn("no line matching", str(cm.exception).lower()) |
| 65 | + self.assertIn(str(self.temp_file), str(cm.exception)) |
| 66 | + |
| 67 | + def test_multiple_version_lines_found_raises_error(self): |
| 68 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 69 | + for i in range(100): |
| 70 | + print(f"line {i+1}", file=f) |
| 71 | + print("set(version aaa)", file=f) |
| 72 | + print("set(version bbb)", file=f) |
| 73 | + |
| 74 | + with self.assertRaises(firestore_patch.LevelDbVersionLineError) as cm: |
| 75 | + firestore_patch.load_leveldb_version(self.temp_file) |
| 76 | + |
| 77 | + self.assertIn("multiple lines matching", str(cm.exception).lower()) |
| 78 | + self.assertIn(str(self.temp_file), str(cm.exception)) |
| 79 | + self.assertIn("line 101", str(cm.exception)) |
| 80 | + self.assertIn("line 102", str(cm.exception)) |
| 81 | + self.assertIn("aaa", str(cm.exception)) |
| 82 | + self.assertIn("bbb", str(cm.exception)) |
| 83 | + |
| 84 | + |
| 85 | +class SetLevelDbVersionTest(unittest.TestCase): |
| 86 | + |
| 87 | + def setUp(self): |
| 88 | + super().setUp() |
| 89 | + temp_dir = pathlib.Path(tempfile.mkdtemp()) |
| 90 | + self.addCleanup(shutil.rmtree, temp_dir) |
| 91 | + self.temp_file = temp_dir / "temp_file.txt" |
| 92 | + |
| 93 | + def test_saves_correct_version(self): |
| 94 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 95 | + print("set(version asdfasdf)", file=f) |
| 96 | + |
| 97 | + firestore_patch.set_leveldb_version(self.temp_file, "1.2.3") |
| 98 | + |
| 99 | + new_version = firestore_patch.load_leveldb_version(self.temp_file) |
| 100 | + self.assertEqual(new_version, "1.2.3") |
| 101 | + |
| 102 | + def test_case_insensitivity(self): |
| 103 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 104 | + print("sEt(vErSiOn asdfasdf)", file=f) |
| 105 | + |
| 106 | + firestore_patch.set_leveldb_version(self.temp_file, "1.2.3") |
| 107 | + |
| 108 | + new_version = firestore_patch.load_leveldb_version(self.temp_file) |
| 109 | + self.assertEqual(new_version, "1.2.3") |
| 110 | + |
| 111 | + def test_leaves_whitespace_alone(self): |
| 112 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 113 | + print(" set ( version 1.2.3.4 ) ", file=f) |
| 114 | + temp_file_contents = self.temp_file.read_text(encoding="utf8") |
| 115 | + |
| 116 | + firestore_patch.set_leveldb_version(self.temp_file, "a.b.c.d") |
| 117 | + |
| 118 | + temp_file_contents_after = self.temp_file.read_text(encoding="utf8") |
| 119 | + expected_temp_file_contents = temp_file_contents.replace("1.2.3.4", "a.b.c.d") |
| 120 | + self.assertEqual(temp_file_contents_after, expected_temp_file_contents) |
| 121 | + |
| 122 | + def test_does_not_touch_other_lines(self): |
| 123 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 124 | + print("blah1", file=f) |
| 125 | + print("set(version 1.2.3.4)", file=f) |
| 126 | + print("blah2", file=f) |
| 127 | + temp_file_contents = self.temp_file.read_text(encoding="utf8") |
| 128 | + |
| 129 | + firestore_patch.set_leveldb_version(self.temp_file, "a.b.c.d") |
| 130 | + |
| 131 | + temp_file_contents_after = self.temp_file.read_text(encoding="utf8") |
| 132 | + expected_temp_file_contents = temp_file_contents.replace("1.2.3.4", "a.b.c.d") |
| 133 | + self.assertEqual(temp_file_contents_after, expected_temp_file_contents) |
| 134 | + |
| 135 | + def test_version_not_found_raises_error(self): |
| 136 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 137 | + print("aaa", file=f) |
| 138 | + print("bbb", file=f) |
| 139 | + |
| 140 | + with self.assertRaises(firestore_patch.LevelDbVersionLineError) as cm: |
| 141 | + firestore_patch.set_leveldb_version(self.temp_file, "a.b.c") |
| 142 | + |
| 143 | + self.assertIn("no line matching", str(cm.exception).lower()) |
| 144 | + self.assertIn(str(self.temp_file), str(cm.exception)) |
| 145 | + |
| 146 | + def test_multiple_version_lines_found_raises_error(self): |
| 147 | + with self.temp_file.open("wt", encoding="utf8") as f: |
| 148 | + for i in range(100): |
| 149 | + print(f"line {i+1}", file=f) |
| 150 | + print("set(version aaa)", file=f) |
| 151 | + print("set(version bbb)", file=f) |
| 152 | + |
| 153 | + with self.assertRaises(firestore_patch.LevelDbVersionLineError) as cm: |
| 154 | + firestore_patch.set_leveldb_version(self.temp_file, "a.b.c") |
| 155 | + |
| 156 | + self.assertIn("multiple lines matching", str(cm.exception).lower()) |
| 157 | + self.assertIn(str(self.temp_file), str(cm.exception)) |
| 158 | + self.assertIn("101, 102", str(cm.exception)) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + unittest.main() |
0 commit comments