def main():
    record = Record()
    ben = record.create_person()
    record.deal_with_life(ben, 'school')
    record.deal_with_life(ben, 'friend')
    assert record.view_life(ben) == ['school', 'friend']

    alex = record.create_person()
    assert alex != ben
    record.deal_with_life(alex, 'girl')
    assert record.view_life(alex) == ['girl']

    # Create event notification
    reader = NotificationLogReader(record.log)
    notifications = list(reader.read(start=1))
    assert len(notifications) == 5
    print(notifications)
    for notification in notifications:
        print(notification_to_table(notification))
    def test_select(self):
        recorder = SQLiteProcessRecorder(SQLiteDatastore(":memory:"))
        recorder.create_table()

        # Construct notification log.
        notification_log = LocalNotificationLog(recorder, section_size=5)
        reader = NotificationLogReader(notification_log, section_size=5)
        notifications = list(chain(*reader.select(start=1)))
        self.assertEqual(len(notifications), 0)

        # Write 5 events.
        originator_id = uuid4()
        for i in range(5):
            stored_event = StoredEvent(
                originator_id=originator_id,
                originator_version=i,
                topic="topic",
                state=b"state",
            )
            recorder.insert_events([stored_event], )

        notifications = list(chain(*reader.select(start=1)))
        self.assertEqual(len(notifications), 5)

        # Write 4 events.
        originator_id = uuid4()
        for i in range(4):
            stored_event = StoredEvent(
                originator_id=originator_id,
                originator_version=i,
                topic="topic",
                state=b"state",
            )
            recorder.insert_events([stored_event])

        notifications = list(chain(*reader.select(start=1)))
        self.assertEqual(len(notifications), 9)

        notifications = list(chain(*reader.select(start=2)))
        self.assertEqual(len(notifications), 8)

        notifications = list(chain(*reader.select(start=3)))
        self.assertEqual(len(notifications), 7)

        notifications = list(chain(*reader.select(start=4)))
        self.assertEqual(len(notifications), 6)

        notifications = list(chain(*reader.select(start=8)))
        self.assertEqual(len(notifications), 2)

        notifications = list(chain(*reader.select(start=9)))
        self.assertEqual(len(notifications), 1)

        notifications = list(chain(*reader.select(start=10)))
        self.assertEqual(len(notifications), 0)
Пример #3
0
    def test_parking_lot(self) -> None:
        # Construct the application object to use an SQLite database.
        app = ParkingLot(
            env={
                "INFRASTRUCTURE_FACTORY": "eventsourcing.sqlite:Factory",
                "SQLITE_DBNAME": ":memory:",
            }
        )

        # Create a valid licence plate.
        licence_plate = LicencePlate("123-123")

        # Book unregistered vehicle.
        app.book(licence_plate, EndOfDay)

        # Check vehicle state.
        vehicle = app.get_vehicle(licence_plate)
        self.assertEqual(vehicle.licence_plate, licence_plate)
        self.assertEqual(len(vehicle.bookings), 1)
        self.assertEqual(len(vehicle.inspection_failures), 0)
        booking1 = vehicle.bookings[-1]

        # Book registered vehicle.
        app.book(licence_plate, EndOfWeek)

        # Check vehicle state.
        vehicle = app.get_vehicle(licence_plate)
        self.assertEqual(len(vehicle.bookings), 2)
        self.assertEqual(len(vehicle.inspection_failures), 0)
        booking2 = vehicle.bookings[-1]

        # Inspect whilst has booking.
        app.inspect(licence_plate, datetime.now())

        # Check vehicle state.
        vehicle = app.get_vehicle(licence_plate)
        self.assertEqual(len(vehicle.bookings), 2)
        self.assertEqual(len(vehicle.inspection_failures), 0)

        # Inspect after bookings expired.
        inspected_on = datetime.now() + timedelta(days=10)
        app.inspect(licence_plate, inspected_on)

        # Check vehicle state.
        vehicle = app.get_vehicle(licence_plate)
        self.assertEqual(len(vehicle.bookings), 2)
        self.assertEqual(len(vehicle.inspection_failures), 1)

        # Check all domain events in bounded context.
        notifications = NotificationLogReader(app.log).read(start=1)
        domain_events = [app.mapper.to_domain_event(n) for n in notifications]
        self.assertEqual(len(domain_events), 4)

        self.assertIsInstance(domain_events[0], Vehicle.Registered)
        vehicle1_id = Vehicle.create_id("123-123")
        self.assertEqual(domain_events[0].originator_id, vehicle1_id)
        self.assertEqual(domain_events[0].originator_version, 1)
        self.assertEqual(domain_events[0].licence_plate_number, "123-123")

        self.assertIsInstance(domain_events[1], Vehicle.Booked)
        self.assertEqual(domain_events[1].originator_id, vehicle1_id)
        self.assertEqual(domain_events[1].originator_version, 2)
        self.assertEqual(domain_events[1].start, booking1.start)
        self.assertEqual(domain_events[1].finish, booking1.finish)

        self.assertIsInstance(domain_events[2], Vehicle.Booked)
        self.assertEqual(domain_events[2].originator_id, vehicle1_id)
        self.assertEqual(domain_events[2].originator_version, 3)
        self.assertEqual(domain_events[2].start, booking2.start)
        self.assertEqual(domain_events[2].finish, booking2.finish)

        self.assertIsInstance(domain_events[3], Vehicle.Unbooked)
        self.assertEqual(domain_events[3].originator_id, vehicle1_id)
        self.assertEqual(domain_events[3].originator_version, 4)
        self.assertEqual(domain_events[3].when, inspected_on)
Пример #4
0
    def test(self) -> None:

        # Set user_id context variable.
        user_id = uuid4()
        user_id_cvar.set(user_id)

        # Construct application.
        app = WikiApplication()

        # Check the page doesn't exist.
        with self.assertRaises(PageNotFound):
            app.get_page_details(slug="welcome")

        # Check the list of pages is empty.
        pages = list(app.get_pages())
        self.assertEqual(len(pages), 0)

        # Create a page.
        app.create_page(title="Welcome", slug="welcome")

        # Present page identified by the given slug.
        page = app.get_page_details(slug="welcome")

        # Check we got a dict that has the given title and slug.
        self.assertEqual(page["title"], "Welcome")
        self.assertEqual(page["slug"], "welcome")
        self.assertEqual(page["body"], "")
        self.assertEqual(page["modified_by"], user_id)

        # Update the title.
        app.update_title(slug="welcome", title="Welcome Visitors")

        # Check the title was updated.
        page = app.get_page_details(slug="welcome")
        self.assertEqual(page["title"], "Welcome Visitors")
        self.assertEqual(page["modified_by"], user_id)

        # Update the slug.
        app.update_slug(old_slug="welcome", new_slug="welcome-visitors")

        # Check the index was updated.
        with self.assertRaises(PageNotFound):
            app.get_page_details(slug="welcome")

        # Check we can get the page by the new slug.
        page = app.get_page_details(slug="welcome-visitors")
        self.assertEqual(page["title"], "Welcome Visitors")
        self.assertEqual(page["slug"], "welcome-visitors")

        # Update the body.
        app.update_body(slug="welcome-visitors", body="Welcome to my wiki")

        # Check the body was updated.
        page = app.get_page_details(slug="welcome-visitors")
        self.assertEqual(page["body"], "Welcome to my wiki")

        # Update the body.
        app.update_body(slug="welcome-visitors", body="Welcome to this wiki")

        # Check the body was updated.
        page = app.get_page_details(slug="welcome-visitors")
        self.assertEqual(page["body"], "Welcome to this wiki")

        # Update the body.
        app.update_body(
            slug="welcome-visitors",
            body="""
Welcome to this wiki!

This is a wiki about...
""",
        )

        # Check the body was updated.
        page = app.get_page_details(slug="welcome-visitors")
        self.assertEqual(
            page["body"],
            """
Welcome to this wiki!

This is a wiki about...
""",
        )

        # Check all the Page events have the user_id.
        for notification in NotificationLogReader(
                app.notification_log).read(start=1):
            domain_event = app.mapper.to_domain_event(notification)
            if isinstance(domain_event, Page.Event):
                self.assertEqual(domain_event.user_id, user_id)

        # Change user_id context variable.
        user_id = uuid4()
        user_id_cvar.set(user_id)

        # Update the body.
        app.update_body(
            slug="welcome-visitors",
            body="""
Welcome to this wiki!

This is a wiki about us!
""",
        )

        # Check 'modified_by' changed.
        page = app.get_page_details(slug="welcome-visitors")
        self.assertEqual(page["title"], "Welcome Visitors")
        self.assertEqual(page["modified_by"], user_id)

        # Check a snapshot was created by now.
        assert app.snapshots
        index = cast(Index,
                     app.repository.get(Index.create_id("welcome-visitors")))
        assert index.ref
        self.assertTrue(len(list(app.snapshots.get(index.ref))))

        # Create some more pages and list all the pages.
        app.create_page("Page 2", "page-2")
        app.create_page("Page 3", "page-3")
        app.create_page("Page 4", "page-4")
        app.create_page("Page 5", "page-5")

        pages = list(app.get_pages(desc=True))
        self.assertEqual(pages[0]["title"], "Page 5")
        self.assertEqual(pages[0]["slug"], "page-5")
        self.assertEqual(pages[1]["title"], "Page 4")
        self.assertEqual(pages[1]["slug"], "page-4")
        self.assertEqual(pages[2]["title"], "Page 3")
        self.assertEqual(pages[2]["slug"], "page-3")
        self.assertEqual(pages[3]["title"], "Page 2")
        self.assertEqual(pages[3]["slug"], "page-2")
        self.assertEqual(pages[4]["title"], "Welcome Visitors")
        self.assertEqual(pages[4]["slug"], "welcome-visitors")

        pages = list(app.get_pages(desc=True, limit=3))
        self.assertEqual(len(pages), 3)
        self.assertEqual(pages[0]["slug"], "page-5")
        self.assertEqual(pages[1]["slug"], "page-4")
        self.assertEqual(pages[2]["slug"], "page-3")

        pages = list(app.get_pages(desc=True, limit=3, lte=2))
        self.assertEqual(len(pages), 2)
        self.assertEqual(pages[0]["slug"], "page-2")
        self.assertEqual(pages[1]["slug"], "welcome-visitors")

        pages = list(app.get_pages(desc=True, lte=2))
        self.assertEqual(len(pages), 2)
        self.assertEqual(pages[0]["slug"], "page-2")
        self.assertEqual(pages[1]["slug"], "welcome-visitors")

        # Check we can't change the slug of a page to one
        # that is being used by another page.
        with self.assertRaises(SlugConflictError):
            app.update_slug("page-2", "page-3")

        # Check we can change the slug of a page to one
        # that was previously being used.
        app.update_slug("welcome-visitors", "welcome")

        page = app.get_page_details(slug="welcome")
        self.assertEqual(page["title"], "Welcome Visitors")
        self.assertEqual(page["modified_by"], user_id)