Exemple #1
0
def createDonorIdentifier(donorRecord):
    try:
        date = datetime.datetime.strptime(donorRecord['dob'], '%d %b %Y')
        firstInitial = donorRecord['first_name'][0].upper()
        lastName = donorRecord['surname'].upper() if len(
            donorRecord['surname']
        ) >= LAST_NAME_BOUND else donorRecord['surname'].ljust(
            LAST_NAME_BOUND, SUBSTITUTION_CHARACTER).upper()
        lastSSNDigits = donorRecord['ssn'][SSN_LAST:].replace('-', '')
        donorIdentifier = f"{lastName[0:LAST_NAME_BOUND]}{firstInitial}{date.strftime('%Y%m%d')}{lastSSNDigits}"

        #return the check digit from the string of digits
        check_digit = mod_11_algorithm(donorIdentifier[-DIGITS:])
        #the check digit is one character by design
        return f'{donorIdentifier}{check_digit}'
    except Exception as ex:
        raise Exception(f'Exception occurred in createDonorIdentifier {ex}')
def test_mod11AlgorithEmptyValue():
    with pytest.raises(IndexError) as error_info:
        mod_11_algorithm('')
def test_mod11AlgorithNegativeValue():
    with pytest.raises(ValueError) as error_info:
        mod_11_algorithm('-199601210128')
def test_mod11AlgorithValueError():
    with pytest.raises(ValueError) as error_info:
        mod_11_algorithm('1996012A0128')
def test_mod11AlgorithReturnLength():
    assert len(
        mod_11_algorithm('199601210128')
    ) == EXPECTED_CHECK_DIGIT_LENGTH, f'Check Digits are Expected to be {EXPECTED_CHECK_DIGIT_LENGTH} Characters Long'
def test_mod11Algorithm():
    for index, expectedValue in enumerate(EXPECTED_VALUES):
        assert expectedValue[VALUE_WITH_CHECKSUM][
            -EXPECTED_CHECK_DIGIT_LENGTH:] == mod_11_algorithm(
                expectedValue[VALUE_INITIAL]
            ), f'Check Sum Doesn' 't Match Expected Value in Pair: {index}'