Giza.QueryAdapter.Sandbox (giza_sphinxsearch v2.0.0)

Copy Markdown

Test adapter that records every executed query and returns configurable responses. No running Sphinx/Manticore instance is required.

Setup

Start the sandbox in test/test_helper.exs:

Giza.QueryAdapter.Sandbox.start_link()
ExUnit.start()

And configure it in config/test.exs:

config :giza_sphinxsearch, :query_adapter, Giza.QueryAdapter.Sandbox

Usage in tests

setup do
  Giza.QueryAdapter.Sandbox.reset()
  :ok
end

test "my query" do
  # default response is {:ok, %{columns: [], rows: [], num_rows: 0}}
  {:ok, _} = SearchTables.create_table("t", "title text")

  assert Sandbox.last_query() == "CREATE TABLE t (title text)"
  assert length(Sandbox.queries()) == 1
end

Custom responses

# Static response for all queries
Sandbox.set_response({:ok, %{columns: ["id"], rows: [[1]], num_rows: 1}})

# Function response — inspect the SQL to decide what to return
Sandbox.set_response({:fn, fn
  "DESCRIBE" <> _ -> {:ok, %{columns: ["Field", "Type"], rows: [["title", "text"]], num_rows: 1}}
  _ -> {:ok, %{columns: [], rows: [], num_rows: 0}}
end})

# Simulate an error
Sandbox.set_response({:error, %{mariadb: %{message: "table not found"}}})

Summary

Functions

Returns a specification to start this module under a supervisor.

Return the most recently executed query string, or nil.

Return the list of query strings executed so far, in chronological order.

Return how many queries have been executed since the last reset.

Clear recorded queries and reset the response to the default.

Set the response returned by execute/1.

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

last_query()

Return the most recently executed query string, or nil.

queries()

Return the list of query strings executed so far, in chronological order.

query_count()

Return how many queries have been executed since the last reset.

reset()

Clear recorded queries and reset the response to the default.

set_response(response)

Set the response returned by execute/1.

Accepts a static term (returned as-is) or {:fn, fun} where fun receives the query string and returns the response.

start_link(opts \\ [])