def test_update_codes_by_key(mock_database, case_data: CaseDataGetter):
    test_data, key, code, status, expected_result = case_data.get()

    before_test_data = insert_test_data(test_data=test_data)

    # Set some expectations
    should_get_updated = [r["code"] for r in before_test_data if r["active"] != status]
    status_should_be = status
    last_updated_date_should_be = date_formatter(datetime.datetime.now())

    # Run test function
    db = boto3.resource("dynamodb")
    test_result = update_codes(database=db, key=key, code=code, status=status)

    # Test db contents after function

    after_test_data = get_codes(database=db, key=key, code=code)

    for row in after_test_data:
        assert isinstance(row["active"], bool)
        assert row["active"] == status_should_be
        if row["code"] in should_get_updated:
            assert row["last_updated_date"] == last_updated_date_should_be

    assert test_result == expected_result

    remove_test_data(test_data)
def test_get_codes(mock_database, caplog, case_data: CaseDataGetter):
    (
        test_data,
        code,
        key,
        expected_result,
        expected_result_count,
        expected_logger_message,
    ) = case_data.get()
    # Set up test data
    insert_test_data(test_data=test_data)

    # Run test function
    db = boto3.resource("dynamodb")
    result = code_generator.get_codes(database=db, code=code, key=key)

    assert len(result) == expected_result_count

    if expected_result_count == 0:
        with caplog.at_level(logging.INFO):
            assert expected_logger_message in caplog.text

    for row in result:

        assert isinstance(row["active"], bool)
        if code:
            assert code == row["code"]
        if key:
            assert key["lpa"] == row["lpa"]
            assert key["actor"] == row["actor"]

    remove_test_data(test_data)
def test_post(mock_database, mock_generate_code, case_data: CaseDataGetter):
    test_data, data, expected_result, expected_status_code = case_data.get()

    result, status_code = handle_create(data=data)

    assert result == expected_result
    assert status_code == expected_status_code

    if expected_result["codes"] is not None:
        remove_test_data(expected_result["codes"])
def test_post(mock_database, case_data: CaseDataGetter):
    test_data, data, expected_result, expected_status_code = case_data.get()
    # Set up test data
    insert_test_data(test_data=test_data)

    result, status_code = handle_validate(data=data)

    assert result == expected_result
    assert status_code == expected_status_code

    remove_test_data(test_data)
def test_check_code_unique(mock_database, caplog, case_data: CaseDataGetter):
    test_data, code, logger_message, expected_result = case_data.get()

    insert_test_data(test_data=test_data)
    db = boto3.resource("dynamodb")
    result = check_code_unique(database=db, code=code)

    assert result == expected_result
    with caplog.at_level(logging.INFO):
        assert logger_message in caplog.text

    remove_test_data(test_data)
def test_data(mock_database, mock_generate_code, case_data: CaseDataGetter):
    test_data, data, expected_result, expected_status_code = case_data.get()

    insert_test_data(test_data=test_data)

    codes_created, status_code = handle_create(data=data)

    for i, item in enumerate(data):

        try:
            lpa = data["lpas"][i]["lpa"]

            actor = data["lpas"][i]["actor"]

            dob = data["lpas"][i]["dob"]
        except KeyError:
            assert codes_created == expected_result
            assert status_code == expected_status_code
            break

        if "" not in [lpa, actor, dob]:

            db = boto3.resource("dynamodb")
            table = db.Table(lpa_codes_table())

            query_result = table.query(
                IndexName="key_index",
                KeyConditionExpression=Key("lpa").eq(lpa) & Key("actor").eq(actor),
            )

            print(f"query_result: {query_result}")

            for item in query_result["Items"]:

                if item["code"] == test_constants["DEFAULT_CODE"]:
                    assert item["dob"] == dob
                    assert item["active"] is True
                    assert item["status_details"] == "Generated"
                    assert item["last_updated_date"] == test_constants["TODAY_ISO"]
                    assert item["generated_date"] == test_constants["TODAY_ISO"]
                    assert item["expiry_date"] == test_constants["EXPIRY_DATE"]
                else:
                    assert item["active"] is False
                    assert item["status_details"] == "Superseded"
        else:
            assert codes_created == expected_result
            break

    if expected_result["codes"] is not None:
        remove_test_data(expected_result["codes"] + test_data)
    else:
        remove_test_data(test_data)
def test_post(mock_database, case_data: CaseDataGetter):
    (
        test_data,
        data,
        expected_result,
        expected_last_updated_date,
        expected_status_code,
    ) = case_data.get()
    # Set up test data
    insert_test_data(test_data=test_data)

    # Perform revoke & check return
    result, status_code = handle_revoke(data=data)
    assert result == expected_result
    assert status_code == expected_status_code

    # Check the data after revoke has been performed
    db = boto3.resource("dynamodb")
    after_revoke = code_generator.get_codes(database=db, code=data["code"])

    if after_revoke:
        lpa = after_revoke[0]["lpa"]
        actor = after_revoke[0]["actor"]

        db = boto3.resource("dynamodb")
        table = db.Table(lpa_codes_table())

        query_result = table.query(
            IndexName="key_index",
            KeyConditionExpression=Key("lpa").eq(lpa) & Key("actor").eq(actor),
        )

        row_data = query_result["Items"][0]

        if data["code"] == row_data["code"]:
            assert row_data["last_updated_date"] == expected_last_updated_date
            assert row_data["status_details"] in [
                "Revoked",
                "Superseded",
            ]
            assert row_data["active"] is False

    remove_test_data(test_data)