# Path: django_performance_testing/serializer.py
# DEFAULT_DJPT_DATAFILE_PATH = 'djpt.results_collected'
# def get_datafile_path():
#     def __init__(self, fpath):
#     def read_all(self):
#     def __init__(self, fpath):
#     def start(self):
#     def end(self):
#     def handle_results_collected(self, sender, results, context, **kwargs):
#     def handle_result(self, sender, results, context):
# class Reader:
# class Writer:
# 
# Path: django_performance_testing/signals.py



import pytest
from django_performance_testing import serializer
from django_performance_testing.signals import results_collected, results_read
from testapp.test_helpers import FakeSender, WithId
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_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
        ))


# Path: django_performance_testing/serializer.py
# DEFAULT_DJPT_DATAFILE_PATH = 'djpt.results_collected'
# def get_datafile_path():
#     def __init__(self, fpath):
#     def read_all(self):
#     def __init__(self, fpath):
#     def start(self):
#     def end(self):
#     def handle_results_collected(self, sender, results, context, **kwargs):
#     def handle_result(self, sender, results, context):
# class Reader:
# class Writer:
# 
# Path: django_performance_testing/signals.py



# Path: tests/testapp/tests/test_serialization.py
import pytest
from django_performance_testing import serializer
from django_performance_testing.signals import results_collected, results_read
from testapp.test_helpers import FakeSender, WithId

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_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)