Packages
commanded
0.18.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
alias Commanded.EventStore.SnapshotData
defmodule BankAccountOpened do
@derive Jason.Encoder
defstruct [:account_number, :initial_balance]
end
describe "record a snapshot" do
test "should record the snapshot" do
snapshot = build_snapshot_data(100)
assert :ok = EventStore.record_snapshot(snapshot)
end
end
describe "read a snapshot" do
test "should read the snapshot" do
snapshot1 = build_snapshot_data(100)
snapshot2 = build_snapshot_data(101)
snapshot3 = build_snapshot_data(102)
assert :ok == EventStore.record_snapshot(snapshot1)
assert :ok == EventStore.record_snapshot(snapshot2)
assert :ok == EventStore.record_snapshot(snapshot3)
{:ok, snapshot} = EventStore.read_snapshot(snapshot3.source_uuid)
assert snapshot_timestamps_within_delta?(snapshot, snapshot3, 60)
end
test "should error when snapshot does not exist" do
{:error, :snapshot_not_found} = EventStore.read_snapshot("doesnotexist")
end
end
describe "delete a snapshot" do
test "should delete the snapshot" do
snapshot1 = build_snapshot_data(100)
assert :ok == EventStore.record_snapshot(snapshot1)
{:ok, snapshot} = EventStore.read_snapshot(snapshot1.source_uuid)
assert snapshot_timestamps_within_delta?(snapshot, snapshot1, 60)
assert :ok == EventStore.delete_snapshot(snapshot1.source_uuid)
assert {:error, :snapshot_not_found} == EventStore.read_snapshot(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.to_naive(DateTime.utc_now())
}
end
defp snapshot_timestamps_within_delta?(snapshot, other_snapshot, delta_seconds) do
NaiveDateTime.diff(snapshot.created_at, other_snapshot.created_at, :second) < delta_seconds
end
end
end