Skip to content

Commit d952949

Browse files
committed
add serialization tests
1 parent cdb607e commit d952949

File tree

5 files changed

+131
-4
lines changed

5 files changed

+131
-4
lines changed

lib/kaffy/resource_query.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ defmodule Kaffy.ResourceQuery do
156156
{query, limited_query}
157157
end
158158

159-
160159
defp build_list_query(_schema, [], _key_pairs) do
161160
{:error, "No private keys. List action not supported."}
162161
end

test/actions_test.exs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,35 @@ defmodule ActionsTest do
6767
end
6868
end
6969

70+
defmodule ActionsCompositeKeyAdmin do
71+
@string_action "test_action"
72+
@response %{product_id: 1, tag_id: 1}
73+
74+
def index(_), do: []
75+
76+
def resource_actions(_conn) do
77+
%{
78+
@string_action => %{
79+
name: "Test Action",
80+
action: fn _, _ ->
81+
{:ok, @response}
82+
end
83+
}
84+
}
85+
end
86+
87+
def list_actions(_conn) do
88+
%{
89+
@string_action => %{
90+
name: "Test Action",
91+
action: fn _, _ ->
92+
:ok
93+
end
94+
}
95+
}
96+
end
97+
end
98+
7099
defmodule FakeSchema do
71100
use Ecto.Schema
72101

@@ -99,6 +128,12 @@ defmodule ActionsTest do
99128
resources: [
100129
test: [schema: FakeSchema, admin: ActionsMapAdmin]
101130
]
131+
],
132+
composite: [
133+
name: "composite",
134+
resources: [
135+
test: [schema: KaffyTest.Schemas.Owner, admin: ActionsCompositeKeyAdmin]
136+
]
102137
]
103138
]
104139
end)
@@ -140,4 +175,32 @@ defmodule ActionsTest do
140175
assert %{"success" => _} = get_flash(result_conn)
141176
end
142177
end
178+
179+
test "single action handles composite primary keys", %{conn: conn} do
180+
with_mock Kaffy.ResourceQuery, fetch_resource: fn _, _, _ -> %{product_id: 1, tag_id: 1} end do
181+
result_conn =
182+
ResourceController.single_action(conn, %{
183+
"context" => "composite",
184+
"resource" => "test",
185+
"action_key" => "test_action",
186+
"id" => "1:1"
187+
})
188+
189+
assert %{"success" => _} = get_flash(result_conn)
190+
end
191+
end
192+
193+
test "list action handles composite primary keys", %{conn: conn} do
194+
with_mock Kaffy.ResourceQuery, fetch_list: fn _, _ -> [%{product_id: 1, tag_id: 1}, %{product_id: 1, tag_id: 2}] end do
195+
result_conn =
196+
ResourceController.list_action(conn, %{
197+
"context" => "composite",
198+
"resource" => "test",
199+
"action_key" => "test_action",
200+
"id" => "1:1,1:2"
201+
})
202+
203+
assert %{"success" => _} = get_flash(result_conn)
204+
end
205+
end
143206
end

test/kaffy_test.exs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule KaffyTest do
22
use ExUnit.Case
33
doctest Kaffy
4-
alias KaffyTest.Schemas.{Company, Person, Pet}
4+
alias KaffyTest.Schemas.{Company, Person, Pet, Owner}
55
# alias KaffyTest.Admin.PersonAdmin
66

77
test "greets the world" do
@@ -29,8 +29,12 @@ defmodule KaffyTest do
2929
end
3030

3131
test "primary_key/1 should return a primary key" do
32-
assert [:id] == ResourceSchema.primary_key(Person)
33-
assert [:id] == ResourceSchema.primary_key(Pet)
32+
assert [:id] == ResourceSchema.primary_keys(Person)
33+
assert [:id] == ResourceSchema.primary_keys(Pet)
34+
end
35+
36+
test "primary_key/1 should return a composite key" do
37+
assert [:person_id, :pet_id] == ResourceSchema.primary_keys(Owner)
3438
end
3539

3640
test "kaffy_field_name/2 should return the name of the field" do

test/resource_admin_test.exs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Kaffy.ResourceAdminTest do
22
use ExUnit.Case, async: true
33
alias Kaffy.ResourceAdmin
4+
alias KaffyTest.Schemas.{Owner, Pet}
45

56
defmodule Cactus do
67
end
@@ -15,6 +16,28 @@ defmodule Kaffy.ResourceAdminTest do
1516
defmodule PersonAdmin do
1617
end
1718

19+
defmodule PetAdmin do
20+
end
21+
22+
defmodule OwnerAdmin do
23+
end
24+
25+
defmodule OwnerETFAdmin do
26+
def serialize_id(_schema, owner) do
27+
{owner.person_id, owner.pet_id}
28+
|> :erlang.term_to_binary()
29+
|> Base.url_encode64(padding: false)
30+
end
31+
32+
def deserialize_id(_schema, serialized_id) do
33+
{person_id, pet_id} = serialized_id
34+
|> Base.url_decode64!(padding: false)
35+
|> :erlang.binary_to_term()
36+
37+
[person_id: person_id, pet_id: pet_id]
38+
end
39+
end
40+
1841
defmodule Nested.Node do
1942
end
2043

@@ -43,4 +66,32 @@ defmodule Kaffy.ResourceAdminTest do
4366
assert ResourceAdmin.plural_name(schema: Person, admin: PersonAdmin) == "People"
4467
end
4568
end
69+
70+
describe "serialize_id/2" do
71+
test "serialize standard id" do
72+
assert ResourceAdmin.serialize_id([schema: Pet, admin: PetAdmin], %{id: 1}) == "1"
73+
end
74+
75+
test "serialize composite id" do
76+
assert ResourceAdmin.serialize_id([schema: Owner, admin: OwnerAdmin], %{person_id: 1, pet_id: 2}) == "1:2"
77+
end
78+
79+
test "custom serialization of composite key" do
80+
assert ResourceAdmin.serialize_id([schema: Owner, admin: OwnerETFAdmin], %{person_id: 1, pet_id: 2}) == "g2gCYQFhAg"
81+
end
82+
end
83+
84+
describe "deserialize_id/2" do
85+
test "deserialize standard id" do
86+
assert ResourceAdmin.deserialize_id([schema: Pet, admin: PetAdmin], "1") == [id: "1"]
87+
end
88+
89+
test "deserialize composite id" do
90+
assert ResourceAdmin.deserialize_id([schema: Owner, admin: OwnerAdmin], "1:2") == [person_id: "1", pet_id: "2"]
91+
end
92+
93+
test "custom deserialization of composite key" do
94+
assert ResourceAdmin.deserialize_id([schema: Owner, admin: OwnerETFAdmin], "g2gCYQFhAg") == [person_id: "1", pet_id: "2"]
95+
end
96+
end
4697
end

test/test_helper.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ defmodule KaffyTest.Schemas.Company do
4343
has_many(:people, KaffyTest.Schemas.Person)
4444
end
4545
end
46+
47+
defmodule KaffyTest.Schemas.Owner do
48+
use Ecto.Schema
49+
50+
@primary_key false
51+
schema "owner" do
52+
field :person_id, :id, primary_key: true
53+
field :pet_id, :id, primary_key: true
54+
end
55+
end

0 commit comments

Comments
 (0)