View Source Electric.Client.Mock (Electric Client v0.2.5)

Allows for mocking stream messages.

Usage

{:ok, client} = Electric.Client.Mock.new()

users = [
  %{id: 1, name: "User 1"},
  %{id: 2, name: "User 2"},
  %{id: 3, name: "User 3"}
]

ref = Electric.Client.Mock.async_response(client,
  status: 200,
  schema: %{id: %{type: "int8"}, name: %{type: "text"}},
  last_offset: Client.Offset.first(),
  shape_handle: "users-1",
  body: Electric.Client.Mock.transaction(users, operation: :insert)
)

messages = Electric.Client.stream(client, "users", live: false) |> Enum.into([])

request = Electric.Client.Mock.async_await(ref, timeout = 5_000)

Summary

Functions

Create a new mock client, linked to the parent process, self() by default.

Wrap the given values in Client.Messages.ChangeMessage structs at the given :lsn.

Types

@type change_opt() ::
  {:value, map()}
  | {:operation, :insert | :update | :delete}
  | {:offset, Electric.Client.Offset.t()}
@type change_opts() :: [change_opt()]
@type response_opt() ::
  {:status, pos_integer()}
  | {:headers, %{required(String.t()) => String.t() | [String.t(), ...]}}
  | {:body, [map()]}
  | {:schema, Electric.Client.schema()}
  | {:shape_handle, Electric.Client.shape_handle()}
  | {:last_offset, Electric.Client.Offset.t()}
@type response_opts() :: [response_opt()]
@type transaction_opt() :: {:lsn, non_neg_integer()} | {:up_to_date, boolean()}
@type transaction_opts() :: [transaction_opt() | change_opt()]

Functions

Link to this function

async_await(ref, timeout \\ 5000)

View Source
@spec async_await(reference(), pos_integer() | :infinity) ::
  Electric.Client.Fetch.Request.t()
Link to this function

async_response(client, response)

View Source
@spec change(change_opts()) :: map()
@spec headers(
  shape_handle: Electric.Client.shape_handle(),
  last_offset: Electric.Client.Offset.t(),
  schema: Electric.Client.schema()
) :: %{required(String.t()) => [String.t()]}
@spec new(pid()) :: {:ok, Electric.Client.t()}

Create a new mock client, linked to the parent process, self() by default.

Link to this function

response(client, response)

View Source
Link to this function

transaction(values, opts \\ [])

View Source
@spec transaction(values :: [map()], transaction_opts()) :: [map()]

Wrap the given values in Client.Messages.ChangeMessage structs at the given :lsn.

By default this will append an up-to-date control message to the end of the liist of changes. Pass up_to_date: false to disable this.