def create_any() -> ComparativeBenchmark: subject_benchmark = PageBenchmark('some url', 1) competitors_benchmarks = [PageBenchmark('another url', 1)] datetime_str = str(datetime.now()) return ComparativeBenchmark('1', datetime_str, subject_benchmark, competitors_benchmarks)
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 create_comparative_benchmark_with_fixed_load_time( benchmark_id: str, subject_url: str, competitors_urls: List[str], load_time) -> ComparativeBenchmark: subject_benchmark = PageBenchmark(subject_url, load_time) competitors_benchmarks = [ PageBenchmark(u, load_time) for u in competitors_urls ] datetime_str = str(datetime.now()) return ComparativeBenchmark(benchmark_id, datetime_str, subject_benchmark, competitors_benchmarks)
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 create_any_with_competitor( competitor_benchmark: PageBenchmark) -> ComparativeBenchmark: subject_benchmark = PageBenchmark('some url', 1) competitors_benchmarks = [competitor_benchmark] datetime_str = str(datetime.now()) return ComparativeBenchmark('1', datetime_str, subject_benchmark, competitors_benchmarks)
def measure_page_load(self, url: str) -> PageBenchmark: uri = self.normalize_url(url) opened_url = urllib.request.urlopen(uri) start_time = time.time() opened_url.read() end_time = time.time() opened_url.close() load_time: float = end_time - start_time return PageBenchmark(uri, load_time)
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
def measure_page_load(self, url: str) -> PageBenchmark: return PageBenchmark(url, self.fixed_load_time)