def test(self):
        with InvoicingApplication.mixin(PopoApplication)() as app:
            assert isinstance(app, InvoicingApplication)

            # Create an invoice.
            app.create_invoice(number="0001", amount=Decimal("10.00"))

            # Get invoice from repository.
            invoice = app.get_invoice("0001")
            self.assertIsInstance(invoice, Invoice)

            # Create an invoice.
            app.create_invoice(number="0002", amount=Decimal("10.00"))

            # Create an invoice.
            app.create_invoice(number="0003", amount=Decimal("10.00"))

            # Create an invoice.
            app.create_invoice(number="0004", amount=Decimal("10.00"))

            reader = NotificationLogReader(app.notification_log)
            event_notifications = reader.read_list()

            events = [
                app.event_store.event_mapper.event_from_topic_and_state(
                    e["topic"], e["state"]) for e in event_notifications
            ]

            total_amount_invoiced = sum([e.amount for e in events])
            self.assertEqual(total_amount_invoiced, Decimal("40.00"))
Esempio n. 2
0
    def test(self):
        with self.get_application() as app:

            # Start with a new table.
            app.drop_table()
            app.drop_table()
            app.setup_table()
            app.setup_table()

            # Check the application's persistence policy,
            # repository, and event store, are working.
            aggregate = ExampleAggregateRoot.__create__()
            aggregate.__save__()
            self.assertTrue(aggregate.id in app.repository)

            # Check the notifications.
            reader = NotificationLogReader(app.notification_log)
            notifications = reader.read_list()
            self.assertEqual(1, len(notifications))
            topic = 'eventsourcing.tests.core_tests.test_aggregate_root#ExampleAggregateRoot.Created'
            self.assertEqual(topic, notifications[0]['topic'])

            app.drop_table()
    def test(self):
        # Construct wiki application.
        with FederatedWikiApplication.mixin(SQLAlchemyApplication)() as app:

            # Just for the IDE.
            assert isinstance(app, FederatedWikiApplication)

            # Add all the detected transmissions to the wiki.
            for transmission in transmitted_data.transmissions:

                # Decide the wiki page slug.
                from_operator = transmission["from_operator"]

                # Get or create the wiki page.
                try:
                    app.get_page(slug=from_operator)
                except PageNotFound:
                    app.create_page(
                        title="Operator: %s" % from_operator, slug=from_operator
                    )

                # Append a paragraph to operator's page for each transmission.
                app.append_paragraph(
                    slug=from_operator, paragraph=json.dumps(transmission)
                )

            # Check operator 'KI5DYI' has 15 paragraphs on their page.
            page = app.get_page(slug="KI5DYI")
            self.assertEqual(len(page["paragraphs"]), 15)

            # Check their first transmission looks ok.
            self.assertEqual(json.loads(page["paragraphs"][0])["tx_frequency"], 361)
            self.assertEqual(json.loads(page["paragraphs"][0])["to_operator"], "SV1AIQ")
            self.assertEqual(json.loads(page["paragraphs"][0])["message"], "RR73")

            # Check their last transmission looks ok.
            self.assertEqual(json.loads(page["paragraphs"][-1])["tx_frequency"], 1688)
            self.assertEqual(json.loads(page["paragraphs"][-1])["to_operator"], "PA5RH")
            self.assertEqual(json.loads(page["paragraphs"][-1])["message"], "-07")

            # Process the transmission events into a set of frequencies and a set of operators.
            frequencies = set()
            operators = set()
            timestamps = []
            log_reader = NotificationLogReader(notification_log=app.notification_log)
            for notification in log_reader.read_list():
                if notification["topic"].endswith("ParagraphAppended"):
                    domain_event_state = json.loads(notification["state"])
                    paragraph = domain_event_state["paragraph"]
                    transmission = json.loads(paragraph)
                    tx_frequency = transmission["tx_frequency"]
                    frequencies.add(int(tx_frequency))
                    operators.add(transmission["from_operator"])
                    operators.add(transmission["to_operator"])
                    timestamps.append(transmission['timestamp'])

            # Check the maximum and minimum frequencies.
            self.assertEqual(max(frequencies), 4245)
            self.assertEqual(min(frequencies), 342)

            # Check the number of operators.
            self.assertEqual(len(operators), 258)

            # Check the first and last timestamps (order is preserved).
            self.assertEqual(timestamps[0], 143330)
            self.assertEqual(timestamps[-1], 153145)