|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from typing import Iterable |
| 15 | + |
| 16 | +import pytest |
| 17 | +from apache_beam.options.value_provider import StaticValueProvider |
| 18 | + |
| 19 | +from error.error_handling import ErrorHandler, Error, GmailNotifier, ErrorNotifier |
| 20 | +from models.execution import DestinationType, Execution, AccountConfig, Source, SourceType, Destination |
| 21 | +from models.oauth_credentials import OAuthCredentials |
| 22 | + |
| 23 | + |
| 24 | +class MockErrorNotifier(ErrorNotifier): |
| 25 | + def __init__(self): |
| 26 | + self.were_errors_sent = False |
| 27 | + self.errors_sent = {} |
| 28 | + self.destination_type = None |
| 29 | + |
| 30 | + def notify(self, destination_type: DestinationType, errors: Iterable[Error]): |
| 31 | + self.were_errors_sent = True |
| 32 | + self.errors_sent = {error.execution: error.error_message for error in errors} |
| 33 | + self.destination_type = destination_type |
| 34 | + |
| 35 | + |
| 36 | +# ErrorHandler tests |
| 37 | + |
| 38 | +def create_execution(source_name, destination_name): |
| 39 | + account_config = AccountConfig('', False, '', '', '') |
| 40 | + source = Source(source_name, SourceType.BIG_QUERY, ['', '']) |
| 41 | + destination = Destination(destination_name, DestinationType.ADS_OFFLINE_CONVERSION, ['']) |
| 42 | + return Execution(account_config, source, destination) |
| 43 | + |
| 44 | + |
| 45 | +def test_single_error_per_execution(): |
| 46 | + error_handler = ErrorHandler(DestinationType.ADS_OFFLINE_CONVERSION, MockErrorNotifier()) |
| 47 | + |
| 48 | + first_execution = create_execution('source1', 'destination1') |
| 49 | + second_execution = create_execution('source1', 'destination2') |
| 50 | + |
| 51 | + error_handler.add_error(first_execution, 'Error for first execution, fist input') |
| 52 | + error_handler.add_error(first_execution, 'Error for first execution, second input') |
| 53 | + error_handler.add_error(second_execution, 'Error for second execution, fist input') |
| 54 | + |
| 55 | + returned_errors = error_handler.errors |
| 56 | + assert len(returned_errors) == 2 |
| 57 | + assert returned_errors.keys() == {first_execution, second_execution} |
| 58 | + |
| 59 | + |
| 60 | +def test_destination_type_consistency(): |
| 61 | + error_handler = ErrorHandler(DestinationType.CM_OFFLINE_CONVERSION, MockErrorNotifier()) |
| 62 | + wrong_destination_type_execution = create_execution('source', 'destination') |
| 63 | + |
| 64 | + with pytest.raises(ValueError): |
| 65 | + error_handler.add_error(wrong_destination_type_execution, 'error message') |
| 66 | + |
| 67 | + |
| 68 | +def test_errors_sent_to_error_notifier(): |
| 69 | + mock_notifier = MockErrorNotifier() |
| 70 | + error_handler = ErrorHandler(DestinationType.ADS_OFFLINE_CONVERSION, mock_notifier) |
| 71 | + |
| 72 | + first_execution = create_execution('source1', 'destination1') |
| 73 | + second_execution = create_execution('source1', 'destination2') |
| 74 | + |
| 75 | + error_handler.add_error(first_execution, 'Error for first execution, fist input') |
| 76 | + error_handler.add_error(second_execution, 'Error for second execution, fist input') |
| 77 | + |
| 78 | + error_handler.notify_errors() |
| 79 | + |
| 80 | + assert mock_notifier.were_errors_sent is True |
| 81 | + assert mock_notifier.errors_sent == {first_execution: 'Error for first execution, fist input', |
| 82 | + second_execution: 'Error for second execution, fist input'} |
| 83 | + assert mock_notifier.destination_type == DestinationType.ADS_OFFLINE_CONVERSION |
| 84 | + |
| 85 | + |
| 86 | +def test_should_not_notify_when_empty_errors(): |
| 87 | + mock_notifier = MockErrorNotifier() |
| 88 | + error_handler = ErrorHandler(DestinationType.ADS_OFFLINE_CONVERSION, mock_notifier) |
| 89 | + |
| 90 | + error_handler.notify_errors() |
| 91 | + |
| 92 | + assert mock_notifier.were_errors_sent is False |
| 93 | + |
| 94 | + |
| 95 | +# GmailNotifier tests |
| 96 | + |
| 97 | +def test_multiple_destinations_separated_by_comma(): |
| 98 | + |
| 99 | + second_email = '[email protected]' |
| 100 | + |
| 101 | + |
| 102 | + credentials = OAuthCredentials('', '', '', '') |
| 103 | + gmail_notifier = GmailNotifier(StaticValueProvider(str, 'true'), credentials, |
| 104 | + StaticValueProvider(str, f'{first_email}, {second_email} ,{third_email}')) |
| 105 | + |
| 106 | + emails = set(gmail_notifier.email_destinations) |
| 107 | + assert len(emails) == 3 |
| 108 | + assert set(emails) == {first_email, third_email, second_email} |
| 109 | + |
| 110 | + |
| 111 | +def test_should_not_notify_when_param_is_false(): |
| 112 | + |
| 113 | + second_email = '[email protected]' |
| 114 | + |
| 115 | + |
| 116 | + credentials = OAuthCredentials('', '', '', '') |
| 117 | + gmail_notifier = GmailNotifier(StaticValueProvider(str, 'false'), credentials, |
| 118 | + StaticValueProvider(str, f'{first_email}, {second_email} ,{third_email}')) |
| 119 | + |
| 120 | + gmail_notifier.notify(DestinationType.ADS_OFFLINE_CONVERSION, [Error(create_execution('s', 'd'), 'error message')]) |
| 121 | + |
| 122 | + |
| 123 | +def test_should_not_notify_when_param_is_empty(): |
| 124 | + |
| 125 | + second_email = '[email protected]' |
| 126 | + |
| 127 | + |
| 128 | + credentials = OAuthCredentials('', '', '', '') |
| 129 | + gmail_notifier = GmailNotifier(StaticValueProvider(str, None), credentials, |
| 130 | + StaticValueProvider(str, f'{first_email}, {second_email} ,{third_email}')) |
| 131 | + |
| 132 | + gmail_notifier.notify(DestinationType.ADS_OFFLINE_CONVERSION, [Error(create_execution('s', 'd'), 'error message')]) |
0 commit comments