<commit_before>from AlphaTwirl.EventReader import EventReaderBundle, EventLoop
import unittest

##____________________________________________________________________________||
class MockEventBuilder(object):
    def build(self, component):
        return component._events

##____________________________________________________________________________||
class MockReader(object):
    def __init__(self, name):
        self.name = name
        self._eventIds = [ ]

    def event(self, event):
        self._eventIds.append(event.id)

##____________________________________________________________________________||
class MockReaderPackage(object):
    def __init__(self):
        self.reader = None
        self.collected = False

    def make(self, name):
        self.reader = MockReader(name)
        return self.reader

    def collect(self):
        self.collected = True

##____________________________________________________________________________||
class MockComponent(object):
    def __init__(self):
        self._events = None

##____________________________________________________________________________||
class MockEventLoopRunner(object):
    def __init__(self):
        self.began = False
        self.ended = False
        self.eventLoop = None

    def begin(self):
        self.began = True

    def run(self, eventLoop):
        self.eventLoop = eventLoop

    def end(self):
        self.ended = True

##____________________________________________________________________________||
class TestEventReaderEventReaderBundle(unittest.TestCase):

    def setUp(self):
        self.eventBuilder = MockEventBuilder()
        self.eventLoopRunner = MockEventLoopRunner()
        self.bundle = EventReaderBundle(self.eventBuilder, self.eventLoopRunner)

    def test_begin_read_end(self):

        package1 = MockReaderPackage()
        self.bundle.addReaderPackage(package1)

        package2 = MockReaderPackage()
        self.bundle.addReaderPackage(package2)

        self.assertFalse(self.eventLoopRunner.began)
        self.bundle.begin()
        self.assertTrue(self.eventLoopRunner.began)

        component1 = MockComponent()
        component1.name = "compName1"

        self.assertIsNone(self.eventLoopRunner.eventLoop)
        self.bundle.read(component1)

        self.assertIsInstance(self.eventLoopRunner.eventLoop, EventLoop)
        self.assertIs(self.eventBuilder, self.eventLoopRunner.eventLoop.eventBuilder)
        self.assertIs(component1, self.eventLoopRunner.eventLoop.component)
        self.assertIs("compName1", self.eventLoopRunner.eventLoop.component.name)
        self.assertEqual([package1.reader, package2.reader], self.eventLoopRunner.eventLoop.readers)

        component2 = MockComponent()
        component2.name = "compName2"
        self.bundle.read(component2)

        self.assertIsInstance(self.eventLoopRunner.eventLoop, EventLoop)
        self.assertIs(self.eventBuilder, self.eventLoopRunner.eventLoop.eventBuilder)
        self.assertIs(component2, self.eventLoopRunner.eventLoop.component)
        self.assertIs("compName2", self.eventLoopRunner.eventLoop.component.name)
        self.assertEqual([package1.reader, package2.reader], self.eventLoopRunner.eventLoop.readers)

        self.assertFalse(self.eventLoopRunner.ended)
        self.assertFalse(package1.collected)
        self.assertFalse(package2.collected)
        self.bundle.end()
        self.assertTrue(self.eventLoopRunner.ended)
        self.assertTrue(package1.collected)
        self.assertTrue(package2.collected)

##____________________________________________________________________________||<commit_msg>split a test into four in TestEventReaderEventReaderBundle<commit_after>from AlphaTwirl.EventReader import EventReaderBundle, EventLoop
import unittest

##____________________________________________________________________________||
class MockEventBuilder(object):
    def build(self, component):
        return component._events

##____________________________________________________________________________||
class MockEventSelection(object): pass

##____________________________________________________________________________||
class MockReader(object):
    def __init__(self, name):
        self.name = name
        self._eventIds = [ ]

    def event(self, event):
        self._eventIds.append(event.id)

##____________________________________________________________________________||
class MockReaderPackage(object):
    def __init__(self):
        self.reader = None
        self.collected = False

    def make(self, name):
        self.reader = MockReader(name)
        return self.reader

    def collect(self):
        self.collected = True

##____________________________________________________________________________||
class MockComponent(object):
    def __init__(self):
        self._events = None

##____________________________________________________________________________||
class MockEventLoopRunner(object):
    def __init__(self):
        self.began = False
        self.ended = False
        self.eventLoop = None

    def begin(self):
        self.began = True

    def run(self, eventLoop):
        self.eventLoop = eventLoop

    def end(self):
        self.ended = True

##____________________________________________________________________________||
class TestEventReaderEventReaderBundle(unittest.TestCase):

    def test_eventBuilder_passed_to_EventLoop(self):
        eventBuilder = MockEventBuilder()
        eventLoopRunner = MockEventLoopRunner()
        bundle = EventReaderBundle(eventBuilder, eventLoopRunner)

        component1 = MockComponent()
        bundle.read(component1)
        self.assertIs(eventBuilder, eventLoopRunner.eventLoop.eventBuilder)

    def test_eventSelection_passed_to_EventLoop(self):
        eventBuilder = MockEventBuilder()
        eventLoopRunner = MockEventLoopRunner()
        eventSelection = MockEventSelection()
        bundle = EventReaderBundle(eventBuilder, eventLoopRunner, eventSelection)

        component1 = MockComponent()
        bundle.read(component1)
        self.assertIs(eventSelection, eventLoopRunner.eventLoop.eventSelection)

    def test_eventLoopRunner_called(self):
        eventBuilder = MockEventBuilder()
        eventLoopRunner = MockEventLoopRunner()
        bundle = EventReaderBundle(eventBuilder, eventLoopRunner)

        self.assertFalse(eventLoopRunner.began)
        bundle.begin()
        self.assertTrue(eventLoopRunner.began)

        self.assertIsNone(eventLoopRunner.eventLoop)
        component1 = MockComponent()
        bundle.read(component1)
        self.assertIs(component1, eventLoopRunner.eventLoop.component)
        self.assertIsInstance(eventLoopRunner.eventLoop, EventLoop)

        self.assertFalse(eventLoopRunner.ended)
        bundle.end()
        self.assertTrue(eventLoopRunner.ended)


    def test_packages_read_and_collected(self):
        eventBuilder = MockEventBuilder()
        eventLoopRunner = MockEventLoopRunner()
        bundle = EventReaderBundle(eventBuilder, eventLoopRunner)

        package1 = MockReaderPackage()
        bundle.addReaderPackage(package1)

        package2 = MockReaderPackage()
        bundle.addReaderPackage(package2)

        bundle.begin()

        component1 = MockComponent()
        component1.name = "compName1"
        bundle.read(component1)
        self.assertIs("compName1", eventLoopRunner.eventLoop.component.name)
        self.assertEqual([package1.reader, package2.reader],eventLoopRunner.eventLoop.readers)

        component2 = MockComponent()
        component2.name = "compName2"
        bundle.read(component2)
        self.assertIs("compName2", eventLoopRunner.eventLoop.component.name)
        self.assertEqual([package1.reader, package2.reader],eventLoopRunner.eventLoop.readers)

        self.assertFalse(package1.collected)
        self.assertFalse(package2.collected)
        bundle.end()
        self.assertTrue(package1.collected)
        self.assertTrue(package2.collected)


##____________________________________________________________________________||
{'fraction_non_alphanumeric': 0.08270583878533515, 'fraction_numerical': 0.006542402172571287, 'mean_word_length': 3.8456937799043063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3943938', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1930, 'n_words': 314}