Best Python code snippet using hypothesis
test_txt_file_cache.py
Source:test_txt_file_cache.py
...27 cache = SingleTxtFileDataCache(url=self.cache_filepath)28 with self.assertRaises(ValueError):29 cache.initialize()30 def test_create_from_json_file_no_data(self):31 cache = self.create_cache("{}")32 self.assertEqual(len(cache.list_content()), 0)33 def test_contains_keys(self):34 cache = self.create_cache()35 self.assertIn("a", cache)36 self.assertIn("b", cache)37 self.assertNotIn("NON EXISTENT", cache)38 def test_get_value(self):39 cache = self.create_cache()40 self.assertEqual(cache.get_value("a"), 1)41 self.assertEqual(cache.get_value("b"), 2)42 with self.assertRaises(KeyError):43 cache.get_value("NON EXISTENT")44 def test_get_metadata(self):45 cache = self.create_cache()46 self.assertEqual(cache.get_metadata("a"), {})47 self.assertEqual(cache.get_metadata("b"), {"timestamp": 1234567})48 with self.assertRaises(KeyError):49 cache.get_metadata("NON EXISTENT")50 def test_get_specific_metadata(self):51 key = "c"52 cache = self.create_cache()53 metadata = {"attr1": "attr_val",54 "attr2": "attr_val2",55 "attr3": "attr_val3"}56 cache.set_value(key, 2, metadata=metadata)57 metadata = cache.get_metadata(key, metadata_list=["attr1", "attr2"])58 self.assertEqual(metadata, {"attr1": "attr_val", "attr2": "attr_val2"})59 def test_set_value(self):60 cache = self.create_cache("{}")61 self.assertNotIn("a", cache)62 cache.set_value("a", 1)63 self.assertEqual(cache.get_value("a"), 1)64 # Second entry65 self.assertNotIn("b", cache)66 cache.set_value("b", 2)67 self.assertEqual(cache.get_value("b"), 2)68 def test_set_value_collision(self):69 cache = self.create_cache("{}")70 self.assertNotIn("a", cache)71 cache.set_value("a", 1)72 with self.assertRaises(ValueError):73 cache.set_value("a", 1)74 # Unless you set the overwrite flag:75 cache.set_value("a", 2, overwrite=True)76 self.assertEqual(cache.get_value("a"), 2)77 def test_set_value_with_metadata(self):78 cache = self.create_cache("{}")79 cache.set_value("a", 1, {"blah": "foo"})80 self.assertEqual(cache.get_metadata("a"), {"blah": "foo"})81 def test_invalidate_cache(self):82 cache = self.create_cache()83 self.assertIn("a", cache)84 self.assertIn("b", cache)85 cache.invalidate(skip_confirm=True)86 self.assertNotIn("a", cache)87 self.assertNotIn("b", cache)88 def test_invalidate_cache_for_keys(self):89 cache = self.create_cache()90 self.assertIn("a", cache)91 self.assertIn("b", cache)92 cache.invalidate(keys=["a"], skip_confirm=True)93 self.assertNotIn("a", cache)94 self.assertIn("b", cache)95 def test_list_content(self):96 cache = self.create_cache()97 self.assertEqual(set(cache.list_content()), {"a", "b"})98 def test_list_content_key_filter(self):99 cache = self.create_cache()100 content = cache.list_content(key_filter=lambda x: "a" in x)101 self.assertEqual(set(content), {"a"})102 def test_list_content_metadata_filter(self):103 cache = self.create_cache()104 content = cache.list_content(105 metadata_filter=lambda x: "timestamp" in x)106 self.assertEqual(set(content), {"b"})107 def test_changed_flag(self):108 cache = self.create_cache('{}')109 self.assertFalse(cache.changed)110 cache.set_value("c", 42)111 self.assertTrue(cache.changed)112 def test_dont_write_index_on_close_if_unmodified(self):113 cache = self.create_cache('{}')114 last_modified = os.path.getmtime(self.cache_filepath)115 cache.close()116 self.assertEqual(last_modified, os.path.getmtime(self.cache_filepath))117 def test_write_index_on_close_if_modified(self):118 cache = self.create_cache('{}')119 cache.set_value("blah", 2)120 cache.close()121 # Make sure the new value is stored in the index:122 index = self.load_index_file()123 self.assertIn("blah", index)124 def test_context_manager_set_value(self):125 self.create_cache()126 with SingleTxtFileDataCache(url=self.cache_filepath) as cache:127 cache.set_value("blah", 2)128 # Make sure the new value is stored in the index:129 index = self.load_index_file()130 self.assertIn("blah", index)131 def test_context_manager_get_value(self):132 self.create_cache()133 with SingleTxtFileDataCache(url=self.cache_filepath) as cache:134 last_modified = os.path.getmtime(self.cache_filepath)135 value = cache.get_value("a")136 self.assertEqual(value, 1)137 # Index file not rewritten:138 self.assertEqual(last_modified, os.path.getmtime(self.cache_filepath))139 # Helper ------------------------------------------------------------------140 def create_cache(self, content=DEFAULT_TXT_CACHE_CONTENT):141 with open(self.cache_filepath, "w") as f:142 f.write(content)143 cache = SingleTxtFileDataCache(url=self.cache_filepath)144 cache.initialize()145 return cache146 def load_index_file(self):...
api.py
Source:api.py
1# -*- coding: utf-8 -*-2from hero.models import Card, HeroBubble, Hero, HeroEvolveCosts, HeroSkill, Warrior, WarriorLevel, HeroLevel, HeroStarUpgrade, HeroMaster, HeroAttribute,HeroDestiny, HeroStar, HeroTeam, HeroTeamLevel,HeroCombat3def update_hero_cache():4 Card.create_cache()5 Hero.create_cache()6 HeroEvolveCosts.create_cache()7 HeroSkill.create_cache()8 Warrior.create_cache()9 WarriorLevel.create_cache()10 HeroLevel.create_cache()11 HeroStarUpgrade.create_cache()12 HeroMaster.create_cache()13 HeroAttribute.create_cache()14 HeroDestiny.create_cache()15 HeroStar.create_cache()16 # HeroTrain.create_cache()17 HeroTeam.create_cache()18 HeroTeamLevel.create_cache()19 HeroBubble.create_cache()20 HeroCombat.create_cache()21def get_card(pk):22 return Card.get(int(pk))23def get_cards():24 return Card.get_all_list()25# def get_heroequipfateattr(pk):26# return HeroEquipFatesAttr.get(int(pk))27# def get_heroequipfateattrs():28# return HeroEquipFatesAttr.get_all_list()29def get_warrior_by_upgrade(warrior_id, upgrade):30 pk = warrior_id * 100 + upgrade31 return Warrior.get(int(pk))32def get_warrior(pk):33 return Warrior.get(int(pk))34def get_warriorlevel(warrior_id, level):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!