def test_build_lsoas_count_query():
    # Given
    lsoas = ['E00000001', 'E00000002']

    expected_count_query = "SELECT COUNT(*) " \
                           "FROM actionv2.cases " \
                           "WHERE action_plan_id = %s " \
                           "AND receipt_received = 'f' " \
                           "AND address_invalid = 'f' " \
                           "AND skeleton = 'f' " \
                           "AND refusal_received IS DISTINCT FROM 'EXTRAORDINARY_REFUSAL' " \
                           "AND case_type = 'HH' " \
                           "AND lsoa IN %s; "

    expected_count_values = (str(TEST_ACTION_PLAN_ID), ('E00000001',
                                                        'E00000002'))

    # When
    actual_count_query, actual_query_values = reminder_lsoa_count.build_lsoas_count_query(
        TEST_ACTION_PLAN_ID, lsoas)

    # Then
    unittest_helper.assertEqual(actual_count_query, expected_count_query,
                                'Generated count query should match expected')
    unittest_helper.assertEqual(actual_query_values, expected_count_values,
                                'Generated count values match expected')
def test_build_action_rule_classifiers(wave, print_batches,
                                       expected_classifiers):
    action_rule_classifiers = reminder_batch.build_action_rule_classifiers(
        wave, print_batches)

    unittest_helper.assertEqual(action_rule_classifiers, expected_classifiers,
                                'Generated classifiers should match expected')
def test_main_insert_rules(patch_input, patch_db_helper, starting_batch,
                           expected_number_of_batches, max_cases,
                           count_per_batch):
    # Given
    patch_db_helper.execute_parametrized_sql_query.return_value = ((
        count_per_batch, ), )
    patch_input.return_value = 'Y'
    expected_number_of_database_counts = get_expected_number_of_database_counts(
        expected_number_of_batches)

    # When
    reminder_batch.main(1,
                        starting_batch,
                        max_cases,
                        TEST_ACTION_PLAN_ID,
                        insert_rules=True,
                        trigger_date_time=TEST_DATE_TIME)

    # Then
    unittest_helper.assertEqual(
        expected_number_of_database_counts,
        patch_db_helper.execute_parametrized_sql_query.call_count)
    if expected_number_of_batches:
        patch_input.assert_called_once()
        unittest_helper.assertEqual(
            2, patch_db_helper.execute_sql_query_with_write.call_count)
def test_generate_action_rules():
    # Given
    action_plan_id = 'test_action_plan_id'
    action_type = 'DUMMY_TEST'
    action_rule_classifiers = {
        action_type: "treatment_code IN ('DUMMY1', 'DUMMY2')"
    }

    # When
    action_rules = reminder_batch.generate_action_rules(
        action_rule_classifiers, action_plan_id, TEST_DATE_TIME)

    action_rule_id = list(action_rules.values())[0][1][0]
    expected_action_rules = {
        action_type:
        ("INSERT INTO actionv2.action_rule "
         "(id, action_type, classifiers_clause, trigger_date_time, action_plan_id, has_triggered) "
         "VALUES (%s, %s, %s, %s, %s, %s);",
         (action_rule_id, action_type, action_rule_classifiers[action_type],
          TEST_DATE_TIME, action_plan_id, False))
    }

    # Then
    unittest_helper.assertEqual(
        expected_action_rules, action_rules,
        'The generated action rules should match our expectations')
def test_build_batch_count_query(batch, wave_classifiers, expected_query,
                                 expected_params):
    actual_query, actual_params = reminder_batch.build_batch_count_query(
        batch, wave_classifiers, TEST_ACTION_PLAN_ID)
    unittest_helper.assertEqual(
        expected_query, actual_query,
        'Generated batch query should match expectation')
    unittest_helper.assertEqual(
        expected_params, actual_params,
        'Generated batch query parameters should match expectation')
Example #6
0
def test_get_lsoas_list(_mock_csv_data):
    # Given
    mock_file = Path('lsoas.csv')
    expected_lsoas = ['E00000001', 'E00000002']

    # When
    actual_lsoas = get_lsoas_from_file(mock_file)

    # Then
    unittest_helper.assertEqual(actual_lsoas, expected_lsoas,
                                'Generated LSOAs should match expected')
def test_build_action_rule_classifiers():
    # Given
    lsoas = ['E00000001', 'E00000002']
    expected_classifiers = "case_type = 'HH' " \
                           "AND lsoa IN ('E00000001', 'E00000002')"

    # When
    action_rule_classifiers = reminder_lsoa.build_action_rule_classifiers(lsoas)

    # Then
    unittest_helper.assertEqual(action_rule_classifiers, expected_classifiers,
                                'Generated classifiers should match expected')
def test_main(patch_db_helper, starting_batch, expected_number_of_batches,
              max_cases, count_per_batch):
    # Given
    patch_db_helper.execute_parametrized_sql_query.return_value = ((
        count_per_batch, ), )
    expected_number_of_database_counts = get_expected_number_of_database_counts(
        expected_number_of_batches)

    # When
    reminder_batch.main(1, starting_batch, max_cases, TEST_ACTION_PLAN_ID)

    # Then
    unittest_helper.assertEqual(
        expected_number_of_database_counts,
        patch_db_helper.execute_parametrized_sql_query.call_count)
    patch_db_helper.execute_sql_query_with_write.assert_not_called()
def test_generate_action_rules():
    # Given
    action_plan_id = 'test_action_plan_id'
    action_type = 'DUMMY_TEST'
    action_rule_classifiers = "lsoa IN ('E00000001', 'E00000002')"

    # When
    action_rules = reminder_lsoa.generate_action_rule(action_type, action_rule_classifiers, action_plan_id,
                                                      TEST_DATE_TIME)

    action_rule_id = list(action_rules)[1][0]
    expected_action_rules = (
        "INSERT INTO actionv2.action_rule "
        "(id, action_type, classifiers_clause, trigger_date_time, action_plan_id, has_triggered) "
        "VALUES (%s, %s, %s, %s, %s, %s);",
        (action_rule_id, action_type, action_rule_classifiers, TEST_DATE_TIME, action_plan_id, False)
    )

    # Then
    unittest_helper.assertEqual(expected_action_rules, action_rules,
                                'The generated action rule should match expected')
def test_select_batches(patch_execute_sql, starting_batch,
                        expected_number_of_batches, max_cases,
                        count_per_batch):
    # Given
    # Mock the database to return a constant count
    patch_execute_sql.return_value = ((count_per_batch, ), )

    # We can use empty classifiers because the DB bit is mocked
    empty_classifiers = ""

    expected_batches = [
        str(batch) for batch in list(
            range(starting_batch, starting_batch + expected_number_of_batches))
    ]

    expected_number_of_database_counts = get_expected_number_of_database_counts(
        expected_number_of_batches)

    # When
    selected_batches = reminder_batch.select_batches(starting_batch,
                                                     empty_classifiers,
                                                     max_cases,
                                                     TEST_ACTION_PLAN_ID)

    # Then
    for count in selected_batches.values():
        unittest_helper.assertEqual(
            count_per_batch, count,
            'The correct batch count should be stored for each selected batch')

    unittest_helper.assertEqual(
        expected_batches, list(selected_batches.keys()),
        'The selected print batches should be every value between our expected first and last'
        ' (as strings)')

    unittest_helper.assertEqual(
        expected_number_of_database_counts, patch_execute_sql.call_count,
        'The number of database counts should match our expectation')
def test_build_event_messages_minimum_values(patched_uuid, patched_datetime):
    # Given
    datetime_value = 'some_date'
    patched_datetime.utcnow.return_value.isoformat.return_value = datetime_value

    transaction_id = 'test_id'
    patched_uuid.uuid4.return_value = transaction_id
    address_update_processor = AddressUpdateProcessor()

    row = {
        'CASE_ID': 'foo_case_id',
        'UPRN': '',
        'ESTAB_UPRN': '',
        'ESTAB_TYPE': '',
        'ABP_CODE': '',
        'ORGANISATION_NAME': '',
        'ADDRESS_LINE1': '',
        'ADDRESS_LINE2': '',
        'ADDRESS_LINE3': '',
        'TOWN_NAME': '',
        'POSTCODE': '',
        'LATITUDE': '0.0',
        'LONGITUDE': '127.0',
        'OA': 'foo_1',
        'LSOA': 'foo_2',
        'MSOA': 'foo_3',
        'LAD': 'foo_4',
        'HTC_WILLINGNESS': '',
        'HTC_DIGITAL': '',
        'TREATMENT_CODE': 'FOO_CODE',
        'FIELDCOORDINATOR_ID': 'ABC123',
        'FIELDOFFICER_ID': 'XYZ999',
        'CE_EXPECTED_CAPACITY': '',
        'CE_SECURE': '',
        'PRINT_BATCH': '',
    }

    event_message = address_update_processor.build_event_messages(row)

    unittest_helper.assertEqual([{
        "event": {
            "type": "RM_CASE_UPDATED",
            "source": "RM_BULK_ADDRESS_UPDATE_PROCESSOR",
            "channel": 'AR',
            "dateTime": datetime_value + 'Z',
            "transactionId": transaction_id
        },
        "payload": {
            "rmCaseUpdated": {
                'caseId': row['CASE_ID'],
                'treatmentCode': row['TREATMENT_CODE'],
                'estabType': row['ESTAB_TYPE'],
                'oa': row['OA'],
                'lsoa': row['LSOA'],
                'msoa': row['MSOA'],
                'lad': row['LAD'],
                'fieldCoordinatorId': row['FIELDCOORDINATOR_ID'],
                'fieldOfficerId': row['FIELDOFFICER_ID'],
                'latitude': row['LATITUDE'],
                'longitude': row['LONGITUDE'],
            }
        }
    }], event_message)
def test_build_event_messages_maximum_values(patched_uuid, patched_datetime):
    # Given
    datetime_value = 'some_date'
    patched_datetime.utcnow.return_value.isoformat.return_value = datetime_value

    transaction_id = 'test_id'
    patched_uuid.uuid4.return_value = transaction_id
    address_update_processor = AddressUpdateProcessor()

    row = {
        'CASE_ID': 'foo_case_id',
        'UPRN': 'foo_uprn',
        'ESTAB_UPRN': 'foo_estab_uprn',
        'ESTAB_TYPE': 'foo_ESTAB_TYPE',
        'ABP_CODE': 'foo_abp',
        'ORGANISATION_NAME': 'foo_incorporated',
        'ADDRESS_LINE1': 'foo flat1',
        'ADDRESS_LINE2': 'foo some road',
        'ADDRESS_LINE3': 'foo somewhere',
        'TOWN_NAME': 'foo some town',
        'POSTCODE': 'F00 BAR',
        'LATITUDE': '0.0',
        'LONGITUDE': '127.0',
        'OA': 'foo_1',
        'LSOA': 'foo_2',
        'MSOA': 'foo_3',
        'LAD': 'foo_4',
        'HTC_WILLINGNESS': '5',
        'HTC_DIGITAL': '3',
        'TREATMENT_CODE': 'FOO_CODE',
        'FIELDCOORDINATOR_ID': 'ABC123',
        'FIELDOFFICER_ID': 'XYZ999',
        'CE_EXPECTED_CAPACITY': '10',
        'CE_SECURE': '1',
        'PRINT_BATCH': '99',
    }

    event_message = address_update_processor.build_event_messages(row)

    unittest_helper.assertEqual([{
        "event": {
            "type": "RM_CASE_UPDATED",
            "source": "RM_BULK_ADDRESS_UPDATE_PROCESSOR",
            "channel": 'AR',
            "dateTime": datetime_value + 'Z',
            "transactionId": transaction_id
        },
        "payload": {
            "rmCaseUpdated": {
                'caseId': row['CASE_ID'],
                'treatmentCode': row['TREATMENT_CODE'],
                'estabType': row['ESTAB_TYPE'],
                'oa': row['OA'],
                'lsoa': row['LSOA'],
                'msoa': row['MSOA'],
                'lad': row['LAD'],
                'fieldCoordinatorId': row['FIELDCOORDINATOR_ID'],
                'fieldOfficerId': row['FIELDOFFICER_ID'],
                'latitude': row['LATITUDE'],
                'longitude': row['LONGITUDE'],
                'secureEstablishment': True,
                'printBatch': row['PRINT_BATCH'],
                'ceExpectedCapacity': row['CE_EXPECTED_CAPACITY'],
                'htcWillingness': row['HTC_WILLINGNESS'],
                'htcDigital': row['HTC_DIGITAL'],
                'uprn': row['UPRN'],
                'estabUprn': row['ESTAB_UPRN'],
                'addressLine1': row['ADDRESS_LINE1'],
                'addressLine2': row['ADDRESS_LINE2'],
                'addressLine3': row['ADDRESS_LINE3'],
                'abpCode': row['ABP_CODE'],
                'organisationName': row['ORGANISATION_NAME'],
                'postcode': row['POSTCODE'],
                'townName': row['TOWN_NAME']
            }
        }
    }], event_message)
Example #13
0
def test_check_lsoa_invalid_format_and_length():
    expected_error = [
        'Row: 1, LSOA "\'E000000001\'" is not alphanumeric',
        'Row: 1, LSOA "\'E000000001\'" is too long'
    ]
    unittest_helper.assertEqual(expected_error, check_lsoa(1, "'E000000001'"))
Example #14
0
def test_check_lsoa_invalid_length():
    expected_error = ["Row: 1, LSOA 'E000000001' is too long"]
    unittest_helper.assertEqual(expected_error, check_lsoa(1, "E000000001"))
Example #15
0
def test_check_lsoa_invalid_format():
    expected_error = ['Row: 1, LSOA "E1000000\'" is not alphanumeric']
    unittest_helper.assertEqual(expected_error, check_lsoa(1, "E1000000'"))
Example #16
0
def test_check_lsoa_is_valid():
    unittest_helper.assertEqual([], check_lsoa(1, "E00000001"))