def test_init_GenomeGroup_from_recursive_folder(tmp_path): """Ensure recursive glob search works for finding py files. This tests Genome as well.""" f = tmp_path / "folder" f.mkdir() test_files = [ tmp_path / "first.py", tmp_path / "second.py", tmp_path / "test_first.py", tmp_path / "test_second.py", tmp_path / "third_test.py", f / "third.py", f / "test_third.py", ] expected = ["first.py", "second.py", "third.py"] for tf in test_files: with open(tf, "w") as temp_py: temp_py.write("import this") ggrp = GenomeGroup(tmp_path) assert sorted([g.name for g in ggrp.keys()]) == sorted(expected) for k, v in ggrp.items(): assert v.source_file.name in expected
def test_GenomeGroup_add_folder_with_exclusions(tmp_path): """Ensure excluded files are not used in the GenomeGroup add folder method.""" f = tmp_path / "folder" f.mkdir() test_files = [ tmp_path / "first.py", tmp_path / "second.py", tmp_path / "test_first.py", tmp_path / "test_second.py", tmp_path / "third_test.py", f / "third.py", f / "test_third.py", ] exclude = [(tmp_path / "second.py").resolve(), (f / "third.py").resolve()] expected = "first.py" # need at least on valid location operation to return a value for trees/targets for tf in test_files: with open(tf, "w") as temp_py: temp_py.write("x: int = 1 + 2") ggrp = GenomeGroup() ggrp.add_folder(tmp_path, exclude_files=exclude) assert len(ggrp) == 1 assert list(ggrp.keys())[0].name == expected
def test_init_GenomeGroup_from_flat_folder(tmp_path): """Test the only .py files are grabbed with GenomeGroup default initialization. This tests Genome as well. """ test_files = [ "first.py", "second.py", "third.py", "test_first.py", "test_second.py", "third_test.py", "fourth_test.py", "first.pyc", "first.pyo", "first.pyi", ] expected = ["first.py", "second.py", "third.py"] for tf in test_files: with open(tmp_path / tf, "w") as temp_py: temp_py.write("import this") ggrp = GenomeGroup(tmp_path) assert sorted([g.name for g in ggrp.keys()]) == sorted(expected) for k, v in ggrp.items(): assert v.source_file.name in expected
def test_generate_sample_FileNotFoundError(binop_file, sorted_binop_expected_locs): """If coverage file is not found, return the targets without coverage.""" ggrp = GenomeGroup(binop_file) ggrp.set_coverage(coverage_file="somethingbad") sample = run.get_sample(ggrp, ignore_coverage=False) assert list(gt.loc_idx for gt in sample) == sorted_binop_expected_locs
def test_generate_sample(binop_file, sorted_binop_expected_locs): """Sample generation from targets results in a sorted list.""" ggrp = GenomeGroup(binop_file) sample = run.get_sample(ggrp, ignore_coverage=True) for gt in sample: assert gt.source_path == binop_file assert list(gt.loc_idx for gt in sample) == sorted_binop_expected_locs
def test_GenomeGroup_basic_properties(binop_file, boolop_file, compare_file): """Basic class property tests and dictionary manipulation.""" ggrp = GenomeGroup(binop_file) ggrp.add_file(boolop_file) ggrp.add_file(compare_file) # test ValuesView is iterable view. for v in ggrp.values(): assert isinstance(v, Genome) # test basic __iter__ property keys = [k for k in ggrp] assert len(keys) == 3 # __repr__ is a string representation of the store assert isinstance(ggrp.__repr__(), str) # test basic .items() method, uses .pop() to activate __del__ key_values = [(k, v) for k, v in ggrp.items()] for k, v in key_values: v2 = ggrp.pop(k) assert v2 == v
def get_genome_group(src_loc: Path, config: Config) -> GenomeGroup: """Get the ``GenomeGroup`` based on ``src_loc`` and ``config``. ``Config`` is used to set global filter codes and exclude files on group creation. Args: src_loc: Path, can be directory or file config: the running config object Returns: ``GenomeGroup`` based on ``src_loc`` and config. """ ggrp = GenomeGroup() # check if src_loc is a single file, otherwise assume it's a directory if src_loc.is_file(): ggrp.add_file(source_file=src_loc) else: ggrp.add_folder(source_folder=src_loc, exclude_files=config.exclude_files, ignore_test_files=True) if config.filter_codes: LOGGER.info("Category restriction, chosen categories: %s", sorted(config.filter_codes)) ggrp.set_filter(filter_codes=config.filter_codes) for k, genome in ggrp.items(): LOGGER.info( "%s", colorize_output( f"{len(genome.targets)} mutation targets found in {genome.source_file} AST.", "green" if len(genome.targets) > 0 else "yellow", ), ) for e in config.exclude_files: LOGGER.info("%s", colorize_output(f"{e.resolve()} excluded.", "yellow")) return ggrp
def test_GenomeGroup_covered_targets(filter_codes, binop_file, mock_binop_coverage_file): """Mock coverage file sets lines 6 and 10 (not 15) to be covered.""" ggrp = GenomeGroup(binop_file) ggrp.set_coverage(mock_binop_coverage_file) ggrp.set_filter(filter_codes) assert len(ggrp.targets) == 4 assert len(ggrp.covered_targets) == 3 for ct in ggrp.covered_targets: assert ct.source_path == binop_file assert ct.loc_idx.lineno in [6, 10] diff = list(ggrp.targets - ggrp.covered_targets) assert diff[0].loc_idx.lineno == 15
def test_GenomeGroup_TypeError_source_file(): """GenomeGroup raises a TypeError adding a Genome without a set source_file.""" ggrp = GenomeGroup() with pytest.raises(TypeError): ggrp.add_genome(Genome())
def test_GenomeGroup_value_TypeError(value, binop_file): """Non-Genome values raise a type error.""" with pytest.raises(TypeError): ggrp = GenomeGroup() ggrp[binop_file] = value
def test_GenomeGroup_key_TypeError(key, binop_file): """Values that are not Path type keys raise a type error.""" with pytest.raises(TypeError): ggrp = GenomeGroup() ggrp[key] = Genome(binop_file)
def test_GenomeGroup_folder_exception(): """Invalid folders raise a type error.""" with pytest.raises(TypeError): ggrp = GenomeGroup() ggrp.add_folder("somethingrandom")
def test_init_GenomeGroup_raise_TypeError(): """Initialization with an non-file non-dir raises a TypeError.""" with pytest.raises(TypeError): _ = GenomeGroup("somethingrandom")
def test_init_GenomeGroup_from_single_file(binop_file): """Initialize the GenomgGroup from a single file. This tests Genome as well.""" ggrp = GenomeGroup(binop_file) assert len(ggrp.keys()) == 1 assert list(ggrp.keys())[0].resolve() == binop_file.resolve()