Beispiel #1
0
def test_compare_checksums(dummy_file_path):
    """Test to compare two checksums"""
    # GIVEN a file

    # WHEN generating a checksum from that file
    res = get_checksum(dummy_file_path)
    res_2 = get_checksum(dummy_file_path)
    # THEN assert the checksums are correct
    assert res == res_2
Beispiel #2
0
def get_fastq_info(fastq: Path, tag: str, algorithm) -> dict:
    """Get the necessary information about a fastq file and return it in a dict"""
    fastq_info = {"file": tag}
    fastq_info["checksum"] = get_checksum(infile=fastq, algorithm=algorithm)
    fastq_info["path"] = str(fastq.absolute())
    fastq_info["algorithm"] = algorithm

    return fastq_info
Beispiel #3
0
def test_generate_checksum_read_1(first_read, checksum_first_read):
    """Test to generate checksum for a fastq file"""
    # GIVEN a fastq file and the corresponding sha256 checksum

    # WHEN generating a checksum for that fastq file
    res = get_checksum(first_read)

    # THEN assert that the checksums are the same
    assert res == checksum_first_read
Beispiel #4
0
def test_decompress_spring_with_fastq_failing_integrity_check(
    spring_tmp_file,
    first_tmp_path,
    second_tmp_path,
    base_context,
    first_read,
    second_read,
):
    """Test to run decompress spring command with a failing integrity check"""
    # GIVEN a cli runner
    runner = CliRunner()
    # GIVEN fasq paths that does not exist
    assert not (first_tmp_path.exists() or second_tmp_path.exists())
    # GIVEN a mock that produces output
    base_context["spring_api"]._create_output = True
    base_context["spring_api"]._fastq1 = first_read
    base_context["spring_api"]._fastq2 = second_read
    # GIVEN checksums for the original fastq files
    checksum1 = get_checksum(first_read)
    # GIVEN wrong checksum for second read
    checksum2 = get_checksum(first_read)
    # WHEN running the decompress command with fastq files
    result = runner.invoke(
        spring,
        [
            str(spring_tmp_file),
            "-f",
            str(first_tmp_path),
            "-s",
            str(second_tmp_path),
            "--first-checksum",
            checksum1,
            "--second-checksum",
            checksum2,
        ],
        obj=base_context,
    )
    # THEN assert the command was succesful
    assert result.exit_code == 1
    # THEN assert that the fastq files are deleted since check failed
    assert not (first_tmp_path.exists() or second_tmp_path.exists())
Beispiel #5
0
def test_generate_md5(dummy_file_path):
    """Test to generate a md5"""
    # GIVEN a file and a calculated md5
    with open(dummy_file_path, "rb") as infile:
        content = infile.read()
    md5 = hashlib.md5(content).hexdigest()

    # WHEN generating a md5 from that file
    res = get_checksum(dummy_file_path, "md5")

    # THEN assert a md5 was created and returned as a string
    assert isinstance(res, str)
    # THEN the md5 is correct
    assert res == md5
Beispiel #6
0
def test_generate_sha1(dummy_file_path):
    """Test to generate a sha1 checksum"""
    # GIVEN a file and a calculated sha1
    with open(dummy_file_path, "rb") as infile:
        content = infile.read()
    sha1 = hashlib.sha1(content).hexdigest()

    # WHEN generating a sha1 from that file
    res = get_checksum(dummy_file_path, "sha1")

    # THEN assert a sha1 was created and returned as a string
    assert isinstance(res, str)
    # THEN the sha1 is correct
    assert res == sha1
Beispiel #7
0
def compare(first, second, algorithm, checksum, dry_run):
    """Compare two files by generating checksums. Fails if two files differ.

    Either the checksum of two files can be compared. Files will be decompressed and checksums
    calculated. Or the checksum of a file can be compared to a checksum string given on the command
    line. Use --first and --checksum if a file should be compared directly to a checksum.
    """
    LOG.info("Running checksum")
    if second and checksum:
        LOG.error("Use --first only in combination with --checksum")
        raise click.Abort

    if dry_run:
        LOG.info("Dry Run!")

    checksums = []
    if checksum:
        checksums.append(checksum)

    for _infile in [first, second]:
        if not _infile:
            continue
        if dry_run:
            checksums.append("dummy_checksum")
            continue
        _infile = pathlib.Path(_infile)
        checksums.append(get_checksum(_infile, algorithm))

    if not dry_run:
        if not compare_elements(checksums):
            LOG.warning("Checksums for %s and %s are NOT the same", first,
                        second)
            raise click.Abort

    LOG.info("Checksum: %s", checksums[0])
    LOG.info("All checksums are the same")
Beispiel #8
0
def checksum(infile, algorithm):
    """Generate the checksum for a file
    """
    LOG.info("Running checksum")
    click.echo(get_checksum(pathlib.Path(infile), algorithm))
Beispiel #9
0
def fixture_checksum_second_read(second_read):
    """Return the checksum for second fastq read"""
    return get_checksum(second_read)
Beispiel #10
0
def fixture_checksum_first_read(first_read):
    """Return the checksum for first fastq read"""
    return get_checksum(first_read)