class TestCache(unittest.TestCase): def setUp(self): self.cache = Cache() self.cache.clear() def testGetGame(self): game = self.cache.getGame({'id': '105600'}) self.assertIsNone(game) self.cache.putGame({'id': '105600', 'name': 'Terraria', 'features': ['Single-player', 'Multi-player', 'Co-op']}) game = self.cache.getGame('105600') self.assertIsNotNone(game) self.assertEqual(game['id'], '105600') self.assertEqual(game['name'], 'Terraria') self.assertItemsEqual(game['features'], ['Single-player', 'Multi-player', 'Co-op']) def testUpdateGame(self): game = self.cache.getGame({'id': '105600'}) self.assertIsNone(game) self.cache.putGame({'id': '105600', 'name': 'Terraria', 'features': ['Single-player', 'Multi-player', 'Co-op'], 'test': 'test'}) self.cache.putGame({'id': '105600', 'name': 'Terraria2', 'features': []}) game = self.cache.getGame('105600') self.assertIsNotNone(game) self.assertEqual(game['id'], '105600') self.assertEqual(game['name'], 'Terraria2') self.assertItemsEqual(game['features'], []) self.assertFalse('test' in game) self.assertFalse('_id' in game) self.assertEqual(self.cache.games.count(), 1)
def test_cache(monkeypatch): """Test cache load and clear method""" monkeypatch.setattr(Cache, 'download', lambda _: True) cache_test = yaml_cache.load_test_cache() cache_test.dump_shelve(TEST_DUMP_FILE) cache = Cache(TEST_DUMP_FILE) assert len(cache.arch2id) == 3 assert len(cache.arch_compat) == 2 assert len(cache.cve_detail) == 1 assert len(cache.dbchange) == 5 assert len(cache.errata_detail) == 5 assert len(cache.errataid2name) == 5 assert len(cache.errataid2repoids) == 3 assert len(cache.evr2id) == 7 assert len(cache.id2arch) == 3 assert len(cache.id2evr) == 7 assert len(cache.id2packagename) == 3 assert len(cache.modulename2id) == 2 assert len(cache.nevra2pkgid) == 5 assert len(cache.package_details) == 8 assert len(cache.packagename2id) == 3 assert len(cache.pkgerrata2module) == 5 assert len(cache.pkgid2errataids) == 7 assert len(cache.pkgid2repoids) == 6 assert len(cache.productid2repoids) == 1 assert len(cache.repo_detail) == 6 assert len(cache.repolabel2ids) == 2 assert len(cache.src_pkg_id2pkg_ids) == 2 assert len(cache.updates) == 2 assert len(cache.updates_index) == 2 assert len(cache.content_set_id2label) == 3 assert len(cache.label2content_set_id) == 3 assert len(cache.content_set_id2pkg_name_ids) == 3 cache.clear() variables = vars(cache) assert len(variables) == 33 for name, var in variables.items(): if name == "filename": assert var == TEST_DUMP_FILE else: assert var == {}
def test_clear_empties_cache(): c = Cache() c.set("foo", "bar") c.clear() assert c.get("foo") is None
class Merge(object): def get_file_name(self, s): """remove the first "file: " and blanl """ return s.partition("file:")[2].strip() def open_file(self, file_name): """open file """ try: fp_code = open(file_name) return fp_code except IOError: return self.perror(self.whole_line_num, \ self.current_name, "can't open code file: \""\ +file_name+"\"") def perror(self, line_num, name, msg): """print error """ sys.stderr.write("merge "+str(line_num)+": error: "\ +msg+"\n") sys.stderr.write("merge: skip the program: "+name[0]+"\n") return -2 def output(self, fp, s): fp.write(s) def move_in_comment(self, fp, in_comment): """start print comments """ if not in_comment: in_comment = True self.output(fp, "/*\n") return in_comment def move_out_comment(self, fp, in_comment): """stop print comments """ if in_comment: in_comment = False self.output(fp, "*/\n") return in_comment def output_code(self, fp, file_name, cache, print_flag = True): """open code file and print it to fp """ fp_code = self.open_file(file_name); if fp_code == -2: return -2 if not print_flag: return 0 code = fp_code.readlines() print_code = False fp.write(cache.get()) for line_code in code: if line_code.strip() == "/*codebook start*/": print_code = True elif line_code.strip() == "/*codebook end*/": print_code = False elif print_code: fp.write(line_code) fp_code.close() return 1 def run(self, list_file_name = "list", output_file_name \ = "codebook"): """main function """ fp_index = open(list_file_name) index = fp_index.readlines() fp_index.close() fp_code = 0 cnt = 0 part_line_num = -1 self.whole_line_num = 0 self.current_name = [] self.lib = {} in_comment = False fp_output = open(output_file_name , "w") self.cache = Cache() for line in index: self.whole_line_num += 1 line = line.strip() if part_line_num == -1 and line == "": #escape the blank continue elif part_line_num == -1: #start self.cache.clear() in_comment = self.move_in_comment(self.cache, \ in_comment) self.output(self.cache, line+"\n") part_line_num += 1 self.current_name = line, self.whole_line_num elif part_line_num == 0: #get the file name and open it if "file:" not in line: part_line_num = self.perror(\ self.whole_line_num, \ self.current_name, \ "doesn't find file name") continue file_name = self.get_file_name(line) #open file part_line_num += 1 elif part_line_num == 1 and line != "-": #print code program describe self.output(self.cache, line+"\n") elif part_line_num == 1: #print code and end in_comment = self.move_out_comment(self.cache, \ in_comment) self.output(self.cache, "\n") part_line_num = -1 #print code rt = 0 if not os.path.isfile(file_name): self.perror(self.whole_line_num, self.current_name, \ "can't open code file: \""+file_name+"\"") rt = -2 #TODO: using another way to check the file exist or not if rt >= 0: self.lib[self.current_name[0]] =\ file_name, copy.deepcopy(self.cache) elif part_line_num == -2: if line == "-": part_line_num = -1 in_comment = self.move_out_comment(\ self.cache, in_comment) if (part_line_num != -1): self.perror(self.whole_line_num, self.current_name, \ "missing a \"-\"") in_comment = self.move_out_comment(\ self.cache, in_comment) self.write(fp_output) fp_output.close() def write(self, fp): """will print as file "index" future """ for ea in self.lib.values(): self.output_code(fp, ea[0], ea[1] )