|
| 1 | +use eventstore::{ |
| 2 | + AppendToStreamOptions, Client, Credentials, EventData, ExpectedRevision, ReadStreamOptions, |
| 3 | + Single, StreamPosition, |
| 4 | +}; |
| 5 | +use futures::TryStreamExt; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use std::error::Error; |
| 8 | +use uuid::Uuid; |
| 9 | + |
| 10 | +#[derive(Serialize, Deserialize)] |
| 11 | +struct TestEvent { |
| 12 | + pub id: String, |
| 13 | + pub important_data: String, |
| 14 | +} |
| 15 | + |
| 16 | +type Result<A> = std::result::Result<A, Box<dyn Error>>; |
| 17 | + |
| 18 | +pub async fn append_to_stream(client: &Client) -> Result<()> { |
| 19 | + // region append-to-stream |
| 20 | + |
| 21 | + let data = TestEvent { |
| 22 | + id: "1".to_string(), |
| 23 | + important_data: "some value".to_string(), |
| 24 | + }; |
| 25 | + |
| 26 | + let event = EventData::json("some-event", &data)?.id(Uuid::new_v4()); |
| 27 | + let options = AppendToStreamOptions::default().expected_revision(ExpectedRevision::NoStream); |
| 28 | + |
| 29 | + let _ = client |
| 30 | + .append_to_stream("some-stream", &options, event) |
| 31 | + .await?; |
| 32 | + |
| 33 | + // endregion append-to-stream |
| 34 | + |
| 35 | + Ok(()) |
| 36 | +} |
| 37 | + |
| 38 | +pub async fn append_with_same_id(client: &Client) -> Result<()> { |
| 39 | + // region append-duplicate-event |
| 40 | + |
| 41 | + let data = TestEvent { |
| 42 | + id: "1".to_string(), |
| 43 | + important_data: "some value".to_string(), |
| 44 | + }; |
| 45 | + |
| 46 | + let event = EventData::json("some-event", &data)?.id(Uuid::new_v4()); |
| 47 | + let options = AppendToStreamOptions::default(); |
| 48 | + |
| 49 | + let _ = client |
| 50 | + .append_to_stream("same-event-stream", &options, event.clone()) |
| 51 | + .await?; |
| 52 | + |
| 53 | + let _ = client |
| 54 | + .append_to_stream("same-event-stream", &options, event) |
| 55 | + .await?; |
| 56 | + |
| 57 | + // endregion append-duplicate-event |
| 58 | + |
| 59 | + Ok(()) |
| 60 | +} |
| 61 | + |
| 62 | +pub async fn append_with_no_stream(client: &Client) -> Result<()> { |
| 63 | + // region append-with-no-stream |
| 64 | + |
| 65 | + let data = TestEvent { |
| 66 | + id: "1".to_string(), |
| 67 | + important_data: "some value".to_string(), |
| 68 | + }; |
| 69 | + |
| 70 | + let event = EventData::json("some-event", &data)?.id(Uuid::new_v4()); |
| 71 | + let options = AppendToStreamOptions::default().expected_revision(ExpectedRevision::NoStream); |
| 72 | + |
| 73 | + let _ = client |
| 74 | + .append_to_stream("same-event-stream", &options, event) |
| 75 | + .await?; |
| 76 | + |
| 77 | + let data = TestEvent { |
| 78 | + id: "2".to_string(), |
| 79 | + important_data: "some other value".to_string(), |
| 80 | + }; |
| 81 | + |
| 82 | + let event = EventData::json("some-event", &data)?.id(Uuid::new_v4()); |
| 83 | + |
| 84 | + let _ = client |
| 85 | + .append_to_stream("same-event-stream", &options, event) |
| 86 | + .await?; |
| 87 | + |
| 88 | + // endregion append-with-no-stream |
| 89 | + |
| 90 | + Ok(()) |
| 91 | +} |
| 92 | + |
| 93 | +pub async fn append_with_concurrency_check(client: Client) -> Result<()> { |
| 94 | + // region append-with-concurrency-check |
| 95 | + |
| 96 | + let options = ReadStreamOptions::default().position(StreamPosition::End); |
| 97 | + |
| 98 | + let last_event = client |
| 99 | + .read_stream("concurrency-stream", &options, Single) |
| 100 | + .await? |
| 101 | + .ok() |
| 102 | + .expect("we expect the stream to at least exist.") |
| 103 | + .expect("we expect the stream to have at least one event."); |
| 104 | + |
| 105 | + let data = TestEvent { |
| 106 | + id: "1".to_string(), |
| 107 | + important_data: "clientOne".to_string(), |
| 108 | + }; |
| 109 | + |
| 110 | + let event = EventData::json("some-event", data)?.id(Uuid::new_v4()); |
| 111 | + let options = AppendToStreamOptions::default().expected_revision(ExpectedRevision::Exact( |
| 112 | + last_event.get_original_event().revision, |
| 113 | + )); |
| 114 | + |
| 115 | + let _ = client |
| 116 | + .append_to_stream("concurrency-stream", &options, event) |
| 117 | + .await?; |
| 118 | + |
| 119 | + let data = TestEvent { |
| 120 | + id: "2".to_string(), |
| 121 | + important_data: "clientTwo".to_string(), |
| 122 | + }; |
| 123 | + |
| 124 | + let event = EventData::json("some-event", &data)?.id(Uuid::new_v4()); |
| 125 | + |
| 126 | + let _ = client |
| 127 | + .append_to_stream("concurrency-stream", &options, event) |
| 128 | + .await?; |
| 129 | + |
| 130 | + // endregion append-with-concurrency-check |
| 131 | + |
| 132 | + Ok(()) |
| 133 | +} |
| 134 | + |
| 135 | +pub async fn append_overriding_user_credentials(client: &Client) -> Result<()> { |
| 136 | + let data = TestEvent { |
| 137 | + id: "1".to_string(), |
| 138 | + important_data: "clientOne".to_string(), |
| 139 | + }; |
| 140 | + |
| 141 | + let event = EventData::json("some-event", data)?.id(Uuid::new_v4()); |
| 142 | + |
| 143 | + // region overriding-user-credentials |
| 144 | + let options = |
| 145 | + AppendToStreamOptions::default().authenticated(Credentials::new("admin", "changeit")); |
| 146 | + |
| 147 | + let _ = client |
| 148 | + .append_to_stream("some-stream", &options, event) |
| 149 | + .await?; |
| 150 | + |
| 151 | + // endregion overriding-user-credentials |
| 152 | + |
| 153 | + Ok(()) |
| 154 | +} |
0 commit comments