def test_list_files(): base_content = { 'val_int': 1, 'val_str': 'str', 'val_bytes': b'\x00', 'val_bool': True } base_dir = 'list_dir/' num_files = 100 all_contents = {} for x in range(num_files): base_content['val_int'] += 1 base_content['val_str'] += str(x) base_content['val_bytes'] += x.to_bytes(1, 'big') base_content['val_bool'] = not base_content['val_bool'] all_contents[f'file_{x}.yml'] = dict(base_content) for x in range(num_files): file_name = f'file_{x}.yml' file_helper.write(base_dir + file_name, all_contents[file_name]) filenames = file_helper.list_files(base_dir) for x in range(num_files): assert file_helper.read(base_dir + filenames[x]) == \ all_contents[filenames[x]]
def test_dir_exist(): empty_content = {} dir_path = 'test_dir/' assert file_helper.dir_exist(dir_path) is False file_helper.write(dir_path + 'empty_file.yml', empty_content) assert file_helper.dir_exist(dir_path) is True
def test_write_read(): content = { 'val_int': 95, 'val_str': 'october', 'val_bytes': b'\x24', 'val_bool': True } assert file_helper.file_exist('write_read_file.yml') is False file_helper.write('write_read_file.yml', content) assert file_helper.file_exist('write_read_file.yml') is True r_content = file_helper.read('write_read_file.yml') assert r_content['val_int'] == 95 assert r_content['val_str'] == 'october' assert r_content['val_bytes'] == b'\x24' assert r_content['val_bool'] is True
def update_sequence(self, template, field_name, custom_pattern=None): field_raw_value = template[field_name] if type(field_raw_value) != str: return None if re.findall(self._sequence_pattern, field_raw_value): try: if not custom_pattern: pattern = field_raw_value pattern = pattern.replace('\\s', '') pattern = pattern.replace('\\S', '') else: pattern = custom_pattern self._sequences[pattern] += 1 except KeyError: self._sequences[pattern] = 0 finally: file_helper.write(self._sequences_filename, self._sequences)
def save(self): file_helper.write(self.filename, asdict(self))