def test_readonly_preexist_get( self, name: str, mapping: CacheDictMapping, extra: Extra, ): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) if extra.preexisting: preexist = extra.preexisting else: preexist = {} missing_value = "MISSING_VALUE_MARKER" for (key, expected) in preexist.items(): with self.subTest(key=key, expected=expected): if expected is not NOT_PRESENT: actual_value = c.get(key, missing_value) self.assertIsNot(actual_value, missing_value) self.assertEqual(actual_value, expected) else: actual_value = c.get(key, missing_value) self.assertIs(actual_value, missing_value)
def test_readonly_preexist_complete( self, name: str, mapping: CacheDictMapping, extra: Extra, ): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) if extra.preexisting: preexist = extra.preexisting else: preexist = {} item_count = 0 for (actual_key, actual_value) in c.items(): with self.subTest(actual_key=actual_key, actual_value=actual_value): in_preexist = (actual_key, actual_value) in preexist.items() self.assertTrue(in_preexist, "Missing key/value in preexisting items") item_count += 1 preexist_present_count = sum( 1 for x in preexist.values() if x is not NOT_PRESENT ) self.assertEqual(item_count, preexist_present_count) self.assertEqual(len(c), preexist_present_count)
def test_readonly_iter(self, name: str, mapping: CacheDictMapping, extra: Extra): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) key_count = 0 for _ in iter(c): key_count += 1 self.assertEqual(key_count, len(c))
def test_readonly_update_none( self, name: str, mapping: CacheDictMapping, extra: Extra, ): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) c.update()
def test_readonly_pop( self, name: str, mapping: CacheDictMapping, extra: Extra, ): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) if extra.preexisting: preexist = extra.preexisting else: preexist = {} for (key, expected) in preexist.items(): with self.subTest(key=key, expected=expected): if expected is not NOT_PRESENT: actual_value = c.get(key) self.assertEqual(actual_value, expected) with self.assertRaises(SqliteCachingException) as raised_context: _ = c.pop(key) actual: typing.Any = raised_context.exception self.assertEqual( actual.category.id, CacheDictReadOnlyException.category_id, actual.msg, ) self.assertEqual( actual.cause.id, CacheDictReadOnlyException.id, actual.msg, ) else: actual_value = c.get(key) self.assertIsNone(actual_value) with self.assertRaises(KeyError) as raised_context_keyerror: _ = c.pop(key) actual_ex: typing.Any = raised_context_keyerror.exception self.assertIsInstance(actual_ex, SqliteCachingException) self.assertEqual( actual_ex.category.id, CacheDictNoSuchKeyException.category_id, actual_ex.msg, ) self.assertEqual( actual_ex.cause.id, CacheDictNoSuchKeyException.id, actual_ex.msg, )
def test_readonly_preexist( self, name: str, mapping: CacheDictMapping, extra: Extra, ): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) if extra.preexisting: preexist = extra.preexisting else: preexist = {} for (key, expected) in preexist.items(): with self.subTest(key=key, expected=expected): if expected is not NOT_PRESENT: actual_value = c[key] self.assertEqual(actual_value, expected) actual_present = key in c self.assertTrue(actual_present) actual_missing = key not in c self.assertFalse(actual_missing) else: actual_present = key in c self.assertFalse(actual_present) actual_missing = key not in c self.assertTrue(actual_missing) with self.assertRaises(KeyError) as raised_context: _ = c[key] actual: typing.Any = raised_context.exception self.assertIsInstance(actual, SqliteCachingException) self.assertEqual( actual.category.id, CacheDictNoSuchKeyException.category_id, actual.msg, ) self.assertEqual( actual.cause.id, CacheDictNoSuchKeyException.id, actual.msg, )
def test_readonly_preexist_bool( self, name: str, mapping: CacheDictMapping, extra: Extra, ): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) actual = bool(c) if extra.preexisting: self.assertTrue(actual) else: self.assertFalse(actual)
def test_readonly_setdefault( self, name: str, mapping: CacheDictMapping, extra: Extra, ): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) if extra.preexisting: preexist = extra.preexisting else: preexist = {} missing_value = self._create_missing_value(mapping) for (key, expected) in preexist.items(): with self.subTest(key=key, expected=expected): if expected is not NOT_PRESENT: actual_value = c.get(key) self.assertIsNot(actual_value, missing_value) self.assertEqual(actual_value, expected) actual_value = c.setdefault(key, missing_value) self.assertIsNot(actual_value, missing_value) self.assertEqual(actual_value, expected) else: actual_value = c.get(key) self.assertIsNone(actual_value) with self.assertRaises(SqliteCachingException) as raised_context: _ = c.setdefault(key, missing_value) actual: typing.Any = raised_context.exception self.assertEqual( actual.category.id, CacheDictReadOnlyException.category_id, actual.msg, ) self.assertEqual( actual.cause.id, CacheDictReadOnlyException.id, actual.msg, )
def test_readonly_update_kwargs( self, name: str, mapping: CacheDictMapping, extra: Extra, ): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) with self.assertRaises(SqliteCachingException) as raised_context: c.update(x="a") actual = raised_context.exception self.assertEqual( actual.category.id, CacheDictUpdateKwargsException.category_id, actual.msg, ) self.assertEqual( actual.cause.id, CacheDictUpdateKwargsException.id, actual.msg, )
def test_readonly_update_mapping( self, name: str, mapping: CacheDictMapping, extra: Extra, ): c = CacheDict.open_readonly( path=f"{self.tmp_dir}/{name}.readonly.sqlite", mapping=mapping, sqlite_params=extra.sqlite_params, ) with self.assertRaises(SqliteCachingException) as raised_context: c.update({k: v for k, v in extra.updates}) actual: typing.Any = raised_context.exception self.assertEqual( actual.category.id, CacheDictReadOnlyException.category_id, actual.msg, ) self.assertEqual( actual.cause.id, CacheDictReadOnlyException.id, actual.msg, )