iT邦幫忙

2023 iThome 鐵人賽

DAY 11
0
Software Development

當rust 遇上 cqrs & es系列 第 11

D11 service testing

  • 分享至 

  • xImage
  •  

今天測一下 影響2個aggreagte,會影響2個aggregate。

借書完成後

  • 書籍庫存減少
  • 讀者手上的書增加

各自更新比較沒有問題,但要增加讀取的ViewModel 或 讀取的DataModel好像有點難,不只是單純的寫入,不確定是不是DDD年會中Teddy講的寫入會需要讀,實作上有點卡住。

先測一下2個Store:

準備2個store

let reader_store = mem_store::MemStore::<Reader>::default();
let book_store = mem_store::MemStore::<Book>::default();
let reader_service = ReaderService {};
let book_service = BookService {};

let reader_cqrs = CqrsFramework::new(
    reader_store.clone(),
    vec![],
    reader_service.clone());
let book_cqrs = CqrsFramework::new(
    book_store.clone(),
    vec![],
    book_service.clone());

新增1本書1個讀者,但書籍尚未入庫

reader_cqrs.execute("test-aggregate-reader-1", ReaderCommand::CreateReader {
    name: "John Smith".to_string(),
}).await.unwrap();
book_cqrs.execute("test-aggregate-book-1", BookCommand::CreateBook {
    id: "test-aggregate-book-1".to_string(),
    title: "test book 1".to_string(),
    isbn10: "1234567890".to_string(),
    description: "test book 1 description".to_string(),
}).await.unwrap();

執行借書指令:

let err = handle_reader_book_command(
    &reader_store, &book_store, &reader_service, &book_service,
    ReaderBookCommand::BorrowBook {
        book_id: "test-aggregate-book-1".to_string(),
        reader_id: "test-aggregate-reader-1".to_string(),
        borrow_date: Utc::now(),
        due_date: Utc::now().add(chrono::Duration::days(7)),
    }, ).await.unwrap_err();
println!("=== err ===\n{:#?}", err);
println!("=== reader_store ===\n{:#?}", reader_store);
println!("=== book_store ===\n{:#?}", book_store);

指令失敗正確回報訊息,且不影響EventStore

=== err ===
ReaderBookError(
    "書籍已無庫存",
)
=== reader_store ===
MemStore {
    events: RwLock {
        data: {
            "test-aggregate-reader-1": [
                EventEnvelope {
                    aggregate_id: "test-aggregate-reader-1",
                    sequence: 1,
                    payload: ReaderCreated {
                        name: "John Smith",
                        id: "793e08f9-163b-4937-9dce-c9c938521de6",
                    },
                    metadata: {},
                },
            ],
        },
        poisoned: false,
        ..
    },
}
=== book_store ===
MemStore {
    events: RwLock {
        data: {
            "test-aggregate-book-1": [
                EventEnvelope {
                    aggregate_id: "test-aggregate-book-1",
                    sequence: 1,
                    payload: BookCreated {
                        id: "test-aggregate-book-1",
                        title: "test book 1",
                        isbn10: "1234567890",
                        description: "test book 1 description",
                    },
                    metadata: {},
                },
            ],
        },
        poisoned: false,
        ..
    },
}

書籍入庫

book_cqrs.execute("test-aggregate-book-1", BookCommand::IngestBook {
    id: "test-book-1".to_string(),
    copies: 1,
}).await.unwrap();

執行借閱操作。

handle_reader_book_command(
    &reader_store, &book_store, &reader_service, &book_service,
    ReaderBookCommand::BorrowBook {
        book_id: "test-aggregate-book-1".to_string(),
        reader_id: "test-aggregate-reader-1".to_string(),
        borrow_date: Utc::now(),
        due_date: Utc::now().add(chrono::Duration::days(7)),
    }, ).await.unwrap();

println!("=== reader_store ===\n{:#?}", reader_store);
println!("=== book_store ===\n{:#?}", book_store);

2個store正確新增事件

=== reader_store ===
MemStore {
    events: RwLock {
        data: {
            "test-aggregate-reader-1": [
                EventEnvelope {
                    aggregate_id: "test-aggregate-reader-1",
                    sequence: 1,
                    payload: ReaderCreated {
                        name: "John Smith",
                        id: "793e08f9-163b-4937-9dce-c9c938521de6",
                    },
                    metadata: {},
                },
                EventEnvelope {
                    aggregate_id: "test-aggregate-reader-1",
                    sequence: 2,
                    payload: BookBorrowed(
                        BorrowedBook {
                            book_id: "test-aggregate-book-1",
                            borrowed_date: 2023-09-26T12:36:49.819406940Z,
                            due_date: 2023-10-03T12:36:49.819407735Z,
                            return_date: None,
                        },
                    ),
                    metadata: {},
                },
            ],
        },
        poisoned: false,
        ..
    },
}
=== book_store ===
MemStore {
    events: RwLock {
        data: {
            "test-aggregate-book-1": [
                EventEnvelope {
                    aggregate_id: "test-aggregate-book-1",
                    sequence: 1,
                    payload: BookCreated {
                        id: "test-aggregate-book-1",
                        title: "test book 1",
                        isbn10: "1234567890",
                        description: "test book 1 description",
                    },
                    metadata: {},
                },
                EventEnvelope {
                    aggregate_id: "test-aggregate-book-1",
                    sequence: 2,
                    payload: BookIngested {
                        id: "test-book-1",
                        copies: 1,
                    },
                    metadata: {},
                },
                EventEnvelope {
                    aggregate_id: "test-aggregate-book-1",
                    sequence: 3,
                    payload: BookLent(
                        LendingRecord {
                            reader_id: "test-aggregate-reader-1",
                            lent_date: 2023-09-26T12:36:49.819406940Z,
                            due_date: 2023-10-03T12:36:49.819407735Z,
                        },
                    ),
                    metadata: {},
                },
            ],
        },
        poisoned: false,
        ..
    },
}

aggregate 狀態:

Reader {
    id: "719a9e13-4071-479d-9c64-18b67c3c1542",
    name: "John Smith",
    borrowed_books: [
        BorrowedBook {
            book_id: "test-aggregate-book-1",
            borrowed_date: 2023-09-26T12:42:02.542640056Z,
            due_date: 2023-10-03T12:42:02.542640631Z,
            return_date: None,
        },
    ],
    borrowed_history: [],
}
Book {
    id: "test-aggregate-book-1",
    title: "test book 1",
    isbn10: "1234567890",
    description: "test book 1 description",
    copies: 1,
    lending_records: [
        LendingRecord {
            reader_id: "test-aggregate-reader-1",
            lent_date: 2023-09-26T12:42:02.542640056Z,
            due_date: 2023-10-03T12:42:02.542640631Z,
        },
    ],
    lent_history: [],
}

上一篇
D10 處理2個aggregate
下一篇
D12 Query of Reader
系列文
當rust 遇上 cqrs & es30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言