| 156 | |
| 157 | |
| 158 | def test_read_once(): |
| 159 | count = 0 |
| 160 | |
| 161 | # noinspection PyAbstractClass |
| 162 | class MyStorage(Storage): |
| 163 | def __init__(self): |
| 164 | self.memory = None |
| 165 | |
| 166 | def read(self): |
| 167 | nonlocal count |
| 168 | count += 1 |
| 169 | |
| 170 | return self.memory |
| 171 | |
| 172 | def write(self, data): |
| 173 | self.memory = data |
| 174 | |
| 175 | with TinyDB(storage=MyStorage) as db: |
| 176 | assert count == 0 |
| 177 | |
| 178 | db.table(db.default_table_name) |
| 179 | |
| 180 | assert count == 0 |
| 181 | |
| 182 | db.all() |
| 183 | |
| 184 | assert count == 1 |
| 185 | |
| 186 | db.insert({'foo': 'bar'}) |
| 187 | |
| 188 | assert count == 3 # One for getting the next ID, one for the insert |
| 189 | |
| 190 | db.all() |
| 191 | |
| 192 | assert count == 4 |
| 193 | |
| 194 | |
| 195 | def test_custom_with_exception(): |