Пример #1
0
def test_it_should_log_benchmark(logger_mock):
    # given
    competitor_benchmark = PageBenchmark('another url', 1)
    benchmark = ComparativeBenchmarkMother.create_any_with_competitor(
        competitor_benchmark)

    # and
    repository = ComparativeBenchmarkInMemoryRepository()
    repository.add(benchmark)

    # when
    listener = ComparativeBenchmarkFinishedLoggerListener(
        repository, logger_mock)
    listener.execute(ComparativeBenchmarkFinished(benchmark.benchmark_id))

    # then
    expected_log = "\n".join([
        "id: %s date: %s" % (benchmark.benchmark_id, benchmark.date),
        "subject: %s load time: %s" % (benchmark.subject_benchmark.url,
                                       benchmark.subject_benchmark.load_time),
        "competitor: %s load time: %s difference: %s" %
        (competitor_benchmark.url, competitor_benchmark.load_time,
         competitor_benchmark.load_time -
         benchmark.subject_benchmark.load_time),
    ])

    logger_mock.info.assert_called_once_with(expected_log)
Пример #2
0
def test_it_should_inform_when_benchmark_not_found():
    # given
    benchmark_id = '1'
    benchmark_repository = ComparativeBenchmarkInMemoryRepository()

    try:
        # when
        benchmark_repository.get_by_id(benchmark_id)
    except ComparativeBenchmarkNotFound as error:
        # then
        assert error.benchmark_id == benchmark_id
Пример #3
0
def test_it_should_find_benchmark_by_id():
    # given
    benchmark_id = '1'
    benchmark = ComparativeBenchmarkMother.create_any_with_id(benchmark_id)

    # when
    benchmark_repository = ComparativeBenchmarkInMemoryRepository()
    benchmark_repository.add(benchmark)

    # then
    found_benchmark = benchmark_repository.get_by_id(benchmark_id)
    assert found_benchmark == benchmark
Пример #4
0
def test_it_should_not_send_email_if_subject_url_loaded_faster_than_competitors(specification_mock):
    # given
    specification_mock.is_satisfied_by.return_value = False
    benchmark = ComparativeBenchmarkMother.create_any()

    # and
    repository = ComparativeBenchmarkInMemoryRepository()
    repository.add(benchmark)

    # when
    listener = ComparativeBenchmarkFinishedEmailAlertListener(notifications_config, specification_mock, repository)
    result = listener.execute(ComparativeBenchmarkFinished(benchmark.benchmark_id))

    # then
    assert result is None
Пример #5
0
def test_it_should_send_email_if_subject_url_loaded_slower_than_at_least_one_of_competitors(specification_mock):
    # given
    specification_mock.is_satisfied_by.return_value = True
    benchmark = ComparativeBenchmarkMother.create_any()

    # and
    repository = ComparativeBenchmarkInMemoryRepository()
    repository.add(benchmark)

    # when
    listener = ComparativeBenchmarkFinishedEmailAlertListener(notifications_config, specification_mock, repository)
    result = listener.execute(ComparativeBenchmarkFinished(benchmark.benchmark_id))

    # then
    expected_command = SendEmailCommand('Benchmark alert', [notifications_config.notification_email], 'Your site is slow')
    assert expected_command == result
def test_it_should_send_sms_if_subject_url_loaded_twice_as_slow_as_at_least_one_of_competitors(
        specification_mock):
    # given
    specification_mock.is_satisfied_by.return_value = True
    benchmark = ComparativeBenchmarkMother.create_any()

    # and
    repository = ComparativeBenchmarkInMemoryRepository()
    repository.add(benchmark)

    # when
    listener = ComparativeBenchmarkFinishedSmsAlertListener(
        notifications_config, specification_mock, repository)
    result = listener.execute(
        ComparativeBenchmarkFinished(benchmark.benchmark_id))

    # then
    expected_command = SendSmsCommand(
        notifications_config.notification_sms_phone_number,
        'Your site is very slow')
    assert expected_command == result
Пример #7
0
def test_it_should_create_benchmarks_for_given_urls():
    # given
    command = CreateComparativeWebPagesBenchmarkCommandMother.create_any()

    # and
    benchmark_repository = ComparativeBenchmarkInMemoryRepository()
    page_benchmarker_stub = PageBenchmarkerStub()

    # when
    comparative_benchmark_handler = CreateComparativeWebPagesBenchmarkHandler(
        benchmark_repository, page_benchmarker_stub)
    comparative_benchmark_handler.handle(command)

    # then
    stored_benchmark = benchmark_repository.get_by_id(command.benchmark_id)

    assert stored_benchmark.benchmark_id == command.benchmark_id
    assert stored_benchmark.subject_benchmark == PageBenchmark(
        command.subject_url, 2)

    expected_event = ComparativeBenchmarkFinished(command.benchmark_id)
    assert comparative_benchmark_handler.release_events()[0] == expected_event
Пример #8
0
from bench.app.notifications.use_cases.commands import SendSmsCommand, SendEmailCommand
from bench.app.notifications.use_cases.handlers import SendSmsHandler, SendEmailHandler
from bench.configuration import Configuration

command_mapping = CommandMapping()

logger = FlaskFileLogger()

smsapi_client = SmsApiPlClient(access_token=Configuration.SMSAPI_ACCESS_TOKEN)

sms_sender = SmsapiSmsSender(smsapi_client)
email_sender = FlaskEmailSender()

page_benchmarker = UrllibPageBenchmarker()
benchmark_repository = ComparativeBenchmarkInMemoryRepository()
benchmark_handler = CreateComparativeWebPagesBenchmarkHandler(
    benchmark_repository, page_benchmarker)

command_mapping.register_command(CreateComparativeWebPagesBenchmarkCommand,
                                 benchmark_handler)
command_mapping.register_command(SendSmsCommand, SendSmsHandler(sms_sender))
command_mapping.register_command(SendEmailCommand,
                                 SendEmailHandler(email_sender))

benchmark_twice_slower_specification = SubjectLoadedTwiceAsSlowThanAtLeastOfCompetitorsSpecification(
)
benchmark_slow_load_specification = SubjectLoadedSlowerThanAtLeastOneOfCompetitorsSpecification(
)

notifications_config = NotificationsConfig(