def test_handles_multiple_sender_ids_as_separate_items(): report = WorstReport() results_read.send( sender=WithId('id one'), results=['a'], context={'context': 'one'}) results_read.send( sender=WithId('id two'), results=['z'], context={'context': 'two'}) assert len(report.data) == 2 assert ('a', {'context': 'one'}) == get_value_and_context(report, 'id one') assert ('z', {'context': 'two'}) == get_value_and_context(report, 'id two')
def test_writer_only_writes_when_end_is_called(tmpfilepath): writer = serializer.Writer(tmpfilepath) writer.start() results_collected.send(sender=WithId('after start'), results=[2], context={'after': 'start'}) reader = serializer.Reader(tmpfilepath) deserialized = reader.read_all() try: assert deserialized == [] finally: writer.end() deserialized = reader.read_all() assert deserialized == [(WithId('after start'), [2], {'after': 'start'})]
def test_has_worst_value_and_its_context(): report = WorstReport() results_read.send( sender=WithId('id'), results=[4], context={'first': 'context'}) assert len(report.data) == 1 assert (4, {'first': 'context'}) == get_value_and_context(report, 'id') results_read.send( sender=WithId('id'), results=[7], context={'2nd': 'context'}) assert len(report.data) == 1 assert (7, {'2nd': 'context'}) == get_value_and_context(report, 'id') results_read.send( sender=WithId('id'), results=[5], context={'3rd': 'context'}) assert len(report.data) == 1 assert (7, {'2nd': 'context'}) == get_value_and_context(report, 'id')
def test_report_can_deal_with_single_anonymous_result_not_with_more(): report = WorstReport() report.handle_results_collected( signal=None, sender=WithId('foo'), results=[9], context={}) assert list(report.data.keys()) == ['foo'] assert get_value_and_context(report, 'foo')[0] == 9 with pytest.raises(TypeError) as excinfo: report.handle_results_collected( signal=None, sender=WithId('foo'), results=[1, 2], context={}) assert 'Duplicate result name(s): \'\'' == str(excinfo.value) assert list(report.data.keys()) == ['foo'] assert get_value_and_context(report, 'foo')[0] == 9
def test_has_separate_context_for_each_channels_worst(): report = WorstReport() report.handle_results_collected( signal=None, sender=WithId('id'), results=[NameValueResult('one', 1), NameValueResult('two', 2)], context={'event': 'first'}) report.handle_results_collected( signal=None, sender=WithId('id'), results=[NameValueResult('one', 3), NameValueResult('two', 1)], context={'event': 'second'}) assert list(report.data.keys()) == ['id'] assert get_tp_names(report, 'id') == ['one', 'two'] assert (3, {'event': 'second'}) == \ get_value_and_context(report, 'id', tp='one') assert (2, {'event': 'first'}) == \ get_value_and_context(report, 'id', tp='two')
def test_there_is_one_channel_per_each_name_received(): report = WorstReport() report.handle_results_collected( signal=None, sender=WithId('id'), results=[ NameValueResult('one', 1), NameValueResult('two', 2)], context={}) assert list(report.data.keys()) == ['id'] assert get_tp_names(report, 'id') == ['one', 'two'] assert (1, {}) == get_value_and_context(report, 'id', tp='one') assert (2, {}) == get_value_and_context(report, 'id', tp='two')
def test_has_copy_of_the_context(): report = WorstReport() sent_context = {'sent': 'context'} results_read.send( sender=WithId('foo'), results=[4], context=sent_context) assert len(report.data) == 1 r_val, r_context = get_value_and_context(report, 'foo') assert r_context == {'sent': 'context'} assert id(r_context) != id(sent_context) sent_context['another'] = 'entry' assert r_context == {'sent': 'context'}
def test_writer_writes_collected_results_fired_between_statt_stop(tmpfilepath): writer = serializer.Writer(tmpfilepath) results_collected.send(sender=WithId('before start'), results=[1], context={'before': 'start'}) writer.start() results_collected.send(sender=WithId('after start'), results=[2], context={'after': 'start'}) writer.end() results_collected.send(sender=WithId('after end'), results=[3], context={'after': 'end'}) reader = serializer.Reader(tmpfilepath) deserialized = reader.read_all() assert deserialized == [(WithId('after start'), [2], {'after': 'start'})] writer.end() # dump data again reader = serializer.Reader(tmpfilepath) deserialized = reader.read_all() assert deserialized == \ [(WithId('after start'), [2], {'after': 'start'})], \ 'after first writer.end it should have disonnected the signal'
def test_read_all_fires_results_read_signals(tmpfilepath): writer = serializer.Writer(tmpfilepath) writer.start() results_collected.send(sender=WithId('after start'), results=[2], context={'after': 'start'}) writer.end() results_from_results_read_signal = [] def record_read_results(sender, results, context, **kwargs): results_from_results_read_signal.append((sender, results, context)) results_read.connect(record_read_results) reader = serializer.Reader(tmpfilepath) deserialized = reader.read_all() try: assert deserialized != [] assert results_from_results_read_signal != [] assert results_from_results_read_signal == deserialized finally: results_read.disconnect(record_read_results)
def test_whatever_two(self): results_collected.send(sender=WithId('whatever'), results=[2], context={'test': 'two'})
def test_whatever_one(self): results_collected.send(sender=WithId('whatever'), results=[1], context={'test': 'one'})