Skip to content

Commit

Permalink
Testing: Add TestChecksumCalculation; rucio#7008
Browse files Browse the repository at this point in the history
  • Loading branch information
rdimaio committed Nov 21, 2024
1 parent fef27c9 commit fc037c0
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions tests/rucio/common/test_checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@

class TestChecksumUtils:
@pytest.mark.parametrize(
'checksum,valid',
[
(GLOBALLY_SUPPORTED_CHECKSUMS[0], True),
(Mock(), False)
]
'checksum,valid',
[
(GLOBALLY_SUPPORTED_CHECKSUMS[0], True),
(Mock(), False)
]
)
def test_is_checksum_valid(self, checksum, valid):
assert is_checksum_valid(checksum) == valid


def test_set_preferred_checksum(self):
mock_checksum = Mock()
GLOBALLY_SUPPORTED_CHECKSUMS.append(mock_checksum)
Expand All @@ -43,7 +42,6 @@ def test_set_preferred_checksum(self):
assert PREFERRED_CHECKSUM == mock_checksum
GLOBALLY_SUPPORTED_CHECKSUMS.pop()


def test_set_preferred_checksum_not_valid(self):
mock_checksum = Mock()
set_preferred_checksum(mock_checksum)
Expand All @@ -52,3 +50,35 @@ def test_set_preferred_checksum_not_valid(self):
from rucio.common.checksum import PREFERRED_CHECKSUM

assert PREFERRED_CHECKSUM != mock_checksum


class TestChecksumCalculation:
@pytest.fixture(scope="class")
def test_file_to_checksum(self, tmp_path_factory):
file = tmp_path_factory.mktemp('data') / 'file.txt'
file.write_text('hello test\n')
return file

def test_md5(self, test_file_to_checksum):
assert md5(test_file_to_checksum) == '31d50dd6285b9ff9f8611d0762265d04'

def test_md5_no_file(self):
with pytest.raises(ChecksumCalculationError) as e:
md5('no_file')
assert e.value.algorithm_name == 'md5'
assert e.value.filepath == 'no_file'

def test_adler32(self, test_file_to_checksum):
assert adler32(test_file_to_checksum) == '198d03ff'

def test_adler32_no_file(self):
with pytest.raises(ChecksumCalculationError) as e:
adler32('no_file')
assert e.value.algorithm_name == 'adler32'
assert e.value.filepath == 'no_file'

def test_sha256(self, test_file_to_checksum):
assert sha256(test_file_to_checksum) == 'd1b81a303d340fb689c6b6f4f474d9e04f314ed9ad8925686e4106452b53181b'

def test_crc32(self, test_file_to_checksum):
assert crc32(test_file_to_checksum) == 'C843500'

0 comments on commit fc037c0

Please sign in to comment.