Skip to content

Commit

Permalink
Added tests to Document
Browse files Browse the repository at this point in the history
  • Loading branch information
GRoguelon committed Jun 13, 2024
1 parent e9824ba commit a958151
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/elasticsearch_ex/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ defmodule ElasticsearchEx.Document do
_shards: nil | %{required(atom() | binary()) => non_neg_integer()}
}

## Module attributes

@attributes @__struct__ |> Map.from_struct() |> Map.keys()

@attributes_as_map @attributes
|> Enum.flat_map(fn key -> [{key, key}, {Atom.to_string(key), key}] end)
|> Enum.into(%{})

## Public functions

def new(attrs \\ %{})
Expand All @@ -42,17 +50,14 @@ defmodule ElasticsearchEx.Document do

@spec new(map()) :: t()
def new(attrs) when is_map(attrs) do
case Enum.at(attrs, 0) do
{key, _} when is_atom(key) ->
struct!(__MODULE__, attrs)

{key, _} when is_binary(key) ->
attrs = Map.new(attrs, fn {key, value} -> {String.to_existing_atom(key), value} end)

struct!(__MODULE__, attrs)

nil ->
__MODULE__.__struct__()
end
Enum.reduce(attrs, %__MODULE__{}, fn {key, value}, acc ->
if casted_key = Map.get(@attributes_as_map, key) do
Map.put(acc, casted_key, value)
else
raise ArgumentError,
"unable to initialize the document with the key: `#{key}`, " <>
"valid keys are: #{Enum.map_join(@attributes, ", ", &to_string/1)}."
end
end)
end
end
85 changes: 85 additions & 0 deletions test/elasticsearch_ex/document_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
defmodule ElasticsearchEx.DocumentTest do
use ExUnit.Case, async: true

alias ElasticsearchEx.Document

describe "new/1 with a map" do
test "returns a struct with a map of atoms as key" do
attrs = %{
_id: "rvO6H4wBhbFpuyekVIiV",
_index: "residents-v20240223013056",
_primary_term: 1,
_score: 1.0,
_seq_no: 26776,
_source: %{"hello" => "world"},
_version: 1
}

assert %Document{
_id: "rvO6H4wBhbFpuyekVIiV",
_index: "residents-v20240223013056",
_primary_term: 1,
_score: 1.0,
_seq_no: 26776,
_source: %{"hello" => "world"},
_version: 1
} = Document.new(attrs)
end

test "returns a struct with a map of atoms as binary" do
attrs = %{
"_id" => "rvO6H4wBhbFpuyekVIiV",
"_index" => "residents-v20240223013056",
"_primary_term" => 1,
"_score" => 1.0,
"_seq_no" => 26776,
"_source" => %{"hello" => "world"},
"_version" => 1
}

assert %Document{
_id: "rvO6H4wBhbFpuyekVIiV",
_index: "residents-v20240223013056",
_primary_term: 1,
_score: 1.0,
_seq_no: 26776,
_source: %{"hello" => "world"},
_version: 1
} = Document.new(attrs)
end

test "returns a struct with a map of mixed key" do
attrs = %{
"_id" => "rvO6H4wBhbFpuyekVIiV",
"_index" => "residents-v20240223013056",
"_primary_term" => 1,
"_score" => 1.0,
_seq_no: 26776,
_source: %{"hello" => "world"},
_version: 1
}

assert %Document{
_id: "rvO6H4wBhbFpuyekVIiV",
_index: "residents-v20240223013056",
_primary_term: 1,
_score: 1.0,
_seq_no: 26776,
_source: %{"hello" => "world"},
_version: 1
} = Document.new(attrs)
end

test "returns a struct with _source as map of atoms as key" do
attrs = %{_source: %{hello: "world"}}

assert %Document{_source: %{hello: "world"}} = Document.new(attrs)
end

test "returns a struct with _source as map of atoms as binary" do
attrs = %{_source: %{"hello" => "world"}}

assert %Document{_source: %{"hello" => "world"}} = Document.new(attrs)
end
end
end

0 comments on commit a958151

Please sign in to comment.