Packages
commanded
1.4.0
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
test/event_store/support/snapshot_test_case.ex
defmodule Commanded.EventStore.SnapshotTestCase do
import Commanded.SharedTestCase
define_tests do
alias Commanded.EventStore.SnapshotData
alias Commanded.UUID
defmodule BankAccountOpened do
@derive Jason.Encoder
defstruct [:account_number, :initial_balance]
end
describe "record a snapshot" do
test "should record the snapshot", %{
event_store: event_store,
event_store_meta: event_store_meta
} do
snapshot = build_snapshot_data(100)
assert :ok = event_store.record_snapshot(event_store_meta, snapshot)
end
end
describe "read a snapshot" do
test "should read the snapshot", %{
event_store: event_store,
event_store_meta: event_store_meta
} do
snapshot1 = build_snapshot_data(100)
snapshot2 = build_snapshot_data(101)
snapshot3 = build_snapshot_data(102)
assert :ok == event_store.record_snapshot(event_store_meta, snapshot1)
assert :ok == event_store.record_snapshot(event_store_meta, snapshot2)
assert :ok == event_store.record_snapshot(event_store_meta, snapshot3)
{:ok, snapshot} = event_store.read_snapshot(event_store_meta, snapshot3.source_uuid)
assert snapshot_timestamps_within_delta?(snapshot, snapshot3, 60)
end
test "should error when snapshot does not exist", %{
event_store: event_store,
event_store_meta: event_store_meta
} do
{:error, :snapshot_not_found} =
event_store.read_snapshot(event_store_meta, "doesnotexist")
end
end
describe "delete a snapshot" do
test "should delete the snapshot", %{
event_store: event_store,
event_store_meta: event_store_meta
} do
snapshot1 = build_snapshot_data(100)
assert :ok == event_store.record_snapshot(event_store_meta, snapshot1)
{:ok, snapshot} = event_store.read_snapshot(event_store_meta, snapshot1.source_uuid)
assert snapshot_timestamps_within_delta?(snapshot, snapshot1, 60)
assert :ok == event_store.delete_snapshot(event_store_meta, snapshot1.source_uuid)
assert {:error, :snapshot_not_found} ==
event_store.read_snapshot(event_store_meta, snapshot1.source_uuid)
end
end
defp build_snapshot_data(account_number) do
%SnapshotData{
source_uuid: UUID.uuid4(),
source_version: account_number,
source_type: "#{__MODULE__}.BankAccountOpened",
data: %BankAccountOpened{account_number: account_number, initial_balance: 1_000},
metadata: nil,
created_at: DateTime.utc_now()
}
end
defp snapshot_timestamps_within_delta?(snapshot, other_snapshot, delta_seconds) do
DateTime.diff(snapshot.created_at, other_snapshot.created_at, :second) < delta_seconds
end
end
end