Esempio n. 1
0
    def test_module_receives_all_events(self):
        ai = Core()
        module = AllEventsModule()
        ai.add_module(module)

        ai.boot()

        ai.publish(EventTypeA(content="a"))
        ai.publish(EventTypeB(content="b"))

        self.assertEqual(['a', 'b'], module.received_events)
Esempio n. 2
0
    def test_handle_metadata(self):
        received_ids = []

        class MetadataModule(BaseModule):
            def boot(self):
                self.subscribe(self.handler)

            def handler(self, event: BaseEvent, metadata: Metadata):
                received_ids.append(metadata.id)

        ai = Core(metadata_provider=lambda: Metadata(id=test_uuid))
        ai.add_module(MetadataModule())
        ai.boot()

        ai.publish(TestEvent('hello'))

        self.assertEqual([test_uuid], received_ids)
Esempio n. 3
0
    def test_metadata_id_is_same_for_all_handlers(self):
        received_ids = []

        class MetadataModule(BaseModule):
            def boot(self):
                self.subscribe(
                    lambda metadata: received_ids.append(metadata.id))
                self.subscribe(
                    lambda metadata: received_ids.append(metadata.id))

        ai = Core()
        ai.add_module(MetadataModule())
        ai.boot()

        ai.publish(TestEvent('hello'))

        self.assertEqual(2, len(received_ids))
        self.assertEqual(received_ids[0], received_ids[1])
Esempio n. 4
0
    def test_module_receives_only_events_it_subscribed_to(self):
        class AEventsModule(BaseModule):
            received_events = []

            def boot(self):
                self.subscribe(self.handle_a, types=EventTypeA)

            def handle_a(self, event: EventTypeA):
                self.received_events.append(event.content)

        ai = Core()
        foobar = AEventsModule()
        ai.add_module(foobar)

        ai.boot()

        ai.publish(EventTypeA(content="a"))
        ai.publish(EventTypeB(content="b"))

        self.assertEqual(['a'], foobar.received_events)
Esempio n. 5
0
    def test_output_expression_result(self):
        class TestHelperBot(BaseModule):
            events: List[BaseEvent]

            def boot(self) -> None:
                self.events = []
                self.subscribe(lambda event: self.events.append(event))

        helper = TestHelperBot()
        bot = MathBot()

        ai = Core()
        ai.add_module(bot)
        ai.add_module(helper)
        ai.boot()
        ai.publish(TextInput('10+2'))

        self.assertIn(MathParsed(Addition(Constant(10), Constant(2))),
                      helper.events)
        self.assertIn(TextOutput('The result is: 12.0'), helper.events)
Esempio n. 6
0
    def test_module_should_not_subscribe_after_boot(self):
        class ModuleSubscribingAfterBoot(BaseModule):
            def boot(self) -> None:
                self.subscribe(self.handle, EventTypeA)

            def handle(self, event: EventTypeA) -> None:
                self.subscribe(lambda event: None, EventTypeB)

        ai = Core()
        ai.add_module(ModuleSubscribingAfterBoot())
        ai.boot()

        self.assertRaises(ModuleSubscribedAfterBoot,
                          lambda: ai.publish(EventTypeA('foobar')))
Esempio n. 7
0
 def test_ai_must_be_booted_before_publishing(self):
     ai = Core()
     self.assertRaises(CoreNotBootedError,
                       lambda: ai.publish(EventTypeA(content="a")))