|
1 | 1 | defmodule AnomaSDK.Test.JSONRoundtripTest do |
2 | 2 | use ExUnit.Case |
3 | 3 |
|
4 | | - alias AnomaSDK.Arm.Action |
5 | | - alias AnomaSDK.Arm.AppData |
6 | | - alias AnomaSDK.Arm.ComplianceInstance |
7 | | - alias AnomaSDK.Arm.ComplianceUnit |
8 | | - alias AnomaSDK.Arm.ComplianceWitness |
9 | | - alias AnomaSDK.Arm.DeltaProof |
10 | | - alias AnomaSDK.Arm.DeltaWitness |
11 | | - alias AnomaSDK.Arm.ExpirableBlob |
12 | | - alias AnomaSDK.Arm.LogicVerifier |
13 | | - alias AnomaSDK.Arm.LogicVerifierInputs |
14 | | - alias AnomaSDK.Arm.MerkleTree |
15 | | - alias AnomaSDK.Arm.Resource |
16 | | - alias AnomaSDK.Arm.Test |
17 | | - alias AnomaSDK.Arm.Transaction |
| 4 | + alias AnomaSDK.Examples.JsonEncoding |
18 | 5 |
|
19 | 6 | describe "JSON roundtrip tests" do |
20 | | - test "Action - encode to JSON, decode back, and convert with from_map" do |
21 | | - original = Test.test_action() |
22 | | - |
23 | | - # Encode to JSON |
24 | | - json_string = Jason.encode!(original) |
25 | | - |
26 | | - # Decode from JSON to map and convert keys to atoms |
27 | | - decoded_map = |
28 | | - json_string |
29 | | - |> Jason.decode!() |
30 | | - |> AnomaSDK.Json.keys_to_atoms() |
31 | | - |
32 | | - # Convert map back to struct using from_map |
33 | | - recovered = Action.from_map(decoded_map) |
34 | | - |
35 | | - # Verify the decoded version matches the original |
36 | | - assert recovered == original |
37 | | - end |
38 | | - |
39 | | - test "AppData - encode to JSON, decode back, and convert with from_map" do |
40 | | - original = Test.test_app_data() |
41 | | - |
42 | | - # Encode to JSON |
43 | | - json_string = Jason.encode!(original) |
44 | | - |
45 | | - # Decode from JSON to map and convert keys to atoms |
46 | | - decoded_map = |
47 | | - json_string |
48 | | - |> Jason.decode!() |
49 | | - |> AnomaSDK.Json.keys_to_atoms() |
50 | | - |
51 | | - # Convert map back to struct using from_map |
52 | | - recovered = AppData.from_map(decoded_map) |
53 | | - |
54 | | - # Verify the decoded version matches the original |
55 | | - assert recovered == original |
56 | | - end |
57 | | - |
58 | | - test "ComplianceInstance - encode to JSON, decode back, and convert with from_map" do |
59 | | - original = Test.test_compliance_instance() |
60 | | - |
61 | | - # Encode to JSON |
62 | | - json_string = Jason.encode!(original) |
63 | | - |
64 | | - # Decode from JSON to map and convert keys to atoms |
65 | | - decoded_map = |
66 | | - json_string |
67 | | - |> Jason.decode!() |
68 | | - |> AnomaSDK.Json.keys_to_atoms() |
69 | | - |
70 | | - # Convert map back to struct using from_map |
71 | | - recovered = ComplianceInstance.from_map(decoded_map) |
72 | | - |
73 | | - # Verify the decoded version matches the original |
74 | | - assert recovered == original |
75 | | - end |
76 | | - |
77 | | - test "ComplianceUnit - encode to JSON, decode back, and convert with from_map" do |
78 | | - original = Test.test_compliance_unit() |
79 | | - |
80 | | - # Encode to JSON |
81 | | - json_string = Jason.encode!(original) |
82 | | - |
83 | | - # Decode from JSON to map and convert keys to atoms |
84 | | - decoded_map = |
85 | | - json_string |
86 | | - |> Jason.decode!() |
87 | | - |> AnomaSDK.Json.keys_to_atoms() |
88 | | - |
89 | | - # Convert map back to struct using from_map |
90 | | - recovered = ComplianceUnit.from_map(decoded_map) |
91 | | - |
92 | | - # Verify the decoded version matches the original |
93 | | - assert recovered == original |
94 | | - end |
95 | | - |
96 | | - test "ComplianceWitness - encode to JSON, decode back, and convert with from_map" do |
97 | | - original = Test.test_compliance_witness() |
98 | | - |
99 | | - # Encode to JSON |
100 | | - json_string = Jason.encode!(original) |
101 | | - |
102 | | - # Decode from JSON to map and convert keys to atoms |
103 | | - decoded_map = |
104 | | - json_string |
105 | | - |> Jason.decode!() |
106 | | - |> AnomaSDK.Json.keys_to_atoms() |
107 | | - |
108 | | - # Convert map back to struct using from_map |
109 | | - recovered = ComplianceWitness.from_map(decoded_map) |
110 | | - |
111 | | - # Verify the decoded version matches the original |
112 | | - assert recovered == original |
113 | | - end |
114 | | - |
115 | | - test "DeltaProof - encode to JSON, decode back, and convert with from_map" do |
116 | | - original = Test.test_delta_proof() |
117 | | - |
118 | | - # Encode to JSON |
119 | | - json_string = Jason.encode!(original) |
120 | | - |
121 | | - # Decode from JSON to map and convert keys to atoms |
122 | | - decoded_map = |
123 | | - json_string |
124 | | - |> Jason.decode!() |
125 | | - |> AnomaSDK.Json.keys_to_atoms() |
126 | | - |
127 | | - # Convert map back to struct using from_map |
128 | | - recovered = DeltaProof.from_map(decoded_map) |
129 | | - |
130 | | - # Verify the decoded version matches the original |
131 | | - assert recovered == original |
132 | | - end |
133 | | - |
134 | | - test "DeltaWitness - encode to JSON, decode back, and convert with from_map" do |
135 | | - original = Test.test_delta_witness() |
136 | | - |
137 | | - # Encode to JSON |
138 | | - json_string = Jason.encode!(original) |
139 | | - |
140 | | - # Decode from JSON to map and convert keys to atoms |
141 | | - decoded_map = |
142 | | - json_string |
143 | | - |> Jason.decode!() |
144 | | - |> AnomaSDK.Json.keys_to_atoms() |
145 | | - |
146 | | - # Convert map back to struct using from_map |
147 | | - recovered = DeltaWitness.from_map(decoded_map) |
148 | | - |
149 | | - # Verify the decoded version matches the original |
150 | | - assert recovered == original |
151 | | - end |
152 | | - |
153 | | - test "ExpirableBlob - encode to JSON, decode back, and convert with from_map" do |
154 | | - original = Test.test_expirable_blob() |
155 | | - |
156 | | - # Encode to JSON |
157 | | - json_string = Jason.encode!(original) |
158 | | - |
159 | | - # Decode from JSON to map and convert keys to atoms |
160 | | - decoded_map = |
161 | | - json_string |
162 | | - |> Jason.decode!() |
163 | | - |> AnomaSDK.Json.keys_to_atoms() |
164 | | - |
165 | | - # Convert map back to struct using from_map |
166 | | - recovered = ExpirableBlob.from_map(decoded_map) |
167 | | - |
168 | | - # Verify the decoded version matches the original |
169 | | - assert recovered == original |
170 | | - end |
171 | | - |
172 | | - test "LogicVerifier - encode to JSON, decode back, and convert with from_map" do |
173 | | - original = Test.test_logic_verifier() |
174 | | - |
175 | | - # Encode to JSON |
176 | | - json_string = Jason.encode!(original) |
177 | | - |
178 | | - # Decode from JSON to map and convert keys to atoms |
179 | | - decoded_map = |
180 | | - json_string |
181 | | - |> Jason.decode!() |
182 | | - |> AnomaSDK.Json.keys_to_atoms() |
183 | | - |
184 | | - # Convert map back to struct using from_map |
185 | | - recovered = LogicVerifier.from_map(decoded_map) |
186 | | - |
187 | | - # Verify the decoded version matches the original |
188 | | - assert recovered == original |
189 | | - end |
190 | | - |
191 | | - test "LogicVerifierInputs - encode to JSON, decode back, and convert with from_map" do |
192 | | - original = Test.test_logic_verifier_inputs() |
193 | | - |
194 | | - # Encode to JSON |
195 | | - json_string = Jason.encode!(original) |
196 | | - |
197 | | - # Decode from JSON to map and convert keys to atoms |
198 | | - decoded_map = |
199 | | - json_string |
200 | | - |> Jason.decode!() |
201 | | - |> AnomaSDK.Json.keys_to_atoms() |
202 | | - |
203 | | - # Convert map back to struct using from_map |
204 | | - recovered = LogicVerifierInputs.from_map(decoded_map) |
205 | | - |
206 | | - # Verify the decoded version matches the original |
207 | | - assert recovered == original |
208 | | - end |
209 | | - |
210 | | - test "MerkleTree - encode to JSON, decode back, and convert with from_map" do |
211 | | - original = Test.test_merkle_tree() |
212 | | - |
213 | | - # Encode to JSON |
214 | | - json_string = Jason.encode!(original) |
215 | | - |
216 | | - # Decode from JSON to map and convert keys to atoms |
217 | | - decoded_map = |
218 | | - json_string |
219 | | - |> Jason.decode!() |
220 | | - |> AnomaSDK.Json.keys_to_atoms() |
221 | | - |
222 | | - # Convert map back to struct using from_map |
223 | | - recovered = MerkleTree.from_map(decoded_map) |
224 | | - |
225 | | - # Verify the decoded version matches the original |
226 | | - assert recovered == original |
| 7 | + test "run examples turning data from maps to back" do |
| 8 | + JsonEncoding.basic_action() |
| 9 | + JsonEncoding.basic_app_data() |
| 10 | + JsonEncoding.basic_compliance_instance() |
| 11 | + JsonEncoding.basic_compliance_unit() |
| 12 | + JsonEncoding.basic_compliance_witness() |
| 13 | + JsonEncoding.basic_delta_proof() |
| 14 | + JsonEncoding.basic_delta_witness() |
| 15 | + JsonEncoding.basic_expirable_blob() |
| 16 | + JsonEncoding.basic_logic_verifier() |
| 17 | + JsonEncoding.basic_verifier_inputs() |
| 18 | + JsonEncoding.basic_merkle_tree() |
| 19 | + JsonEncoding.basic_resource() |
| 20 | + JsonEncoding.basic_transaction() |
227 | 21 | end |
228 | | - |
229 | | - test "Resource - encode to JSON, decode back, and convert with from_map" do |
230 | | - original = Test.test_resource() |
231 | | - |
232 | | - # Encode to JSON |
233 | | - json_string = Jason.encode!(original) |
234 | | - |
235 | | - # Decode from JSON to map and convert keys to atoms |
236 | | - decoded_map = |
237 | | - json_string |
238 | | - |> Jason.decode!() |
239 | | - |> AnomaSDK.Json.keys_to_atoms() |
240 | | - |
241 | | - # Convert map back to struct using from_map |
242 | | - recovered = Resource.from_map(decoded_map) |
243 | | - |
244 | | - # Verify the decoded version matches the original |
245 | | - assert recovered == original |
246 | | - end |
247 | | - end |
248 | | - |
249 | | - test "Transaction - encode to JSON, decode back, and convert with from_map" do |
250 | | - original = Test.test_transaction() |
251 | | - |
252 | | - # Encode to JSON |
253 | | - json_string = Jason.encode!(original) |
254 | | - |
255 | | - # Decode from JSON to map and convert keys to atoms |
256 | | - decoded_map = |
257 | | - json_string |
258 | | - |> Jason.decode!() |
259 | | - |> AnomaSDK.Json.keys_to_atoms() |
260 | | - |
261 | | - # Convert map back to struct using from_map |
262 | | - recovered = Transaction.from_map(decoded_map) |
263 | | - |
264 | | - # Verify the decoded version matches the original |
265 | | - assert recovered == original |
266 | 22 | end |
267 | 23 | end |
0 commit comments