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)
def test_it_should_return_true_if_subject_url_loaded_twice_as_slow_as_at_least_one_of_competitors(): # given subject_benchmark = PageBenchmark('some url', 4) competitor_benchmark = PageBenchmark('another url', 2) benchmark = ComparativeBenchmarkMother.create_any_with(subject_benchmark, competitor_benchmark) # when result = specification.is_satisfied_by(benchmark) # then assert result is True
def test_it_should_return_false_if_subject_url_loaded_in_same_time_than_competitors(): # given subject_benchmark = PageBenchmark('some url', 3) competitor_benchmark = PageBenchmark('another url', 3) benchmark = ComparativeBenchmarkMother.create_any_with(subject_benchmark, competitor_benchmark) # when result = specification.is_satisfied_by(benchmark) # then assert result is False
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
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
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