1
1
defmodule Kaffy.ResourceAdminTest do
2
2
use ExUnit.Case , async: true
3
3
alias Kaffy.ResourceAdmin
4
+ alias KaffyTest.Schemas . { Owner , Pet }
4
5
5
6
defmodule Cactus do
6
7
end
@@ -15,6 +16,28 @@ defmodule Kaffy.ResourceAdminTest do
15
16
defmodule PersonAdmin do
16
17
end
17
18
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
+
18
41
defmodule Nested.Node do
19
42
end
20
43
@@ -43,4 +66,32 @@ defmodule Kaffy.ResourceAdminTest do
43
66
assert ResourceAdmin . plural_name ( schema: Person , admin: PersonAdmin ) == "People"
44
67
end
45
68
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
46
97
end
0 commit comments