File size: 1,403 Bytes
c9c49ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
defmodule Plausible.Stats.JSONSchema.UtilsTest do
  use ExUnit.Case, async: true

  alias Plausible.Stats.JSONSchema

  describe "traversing" do
    test "transform 'fn value -> value end' does not drop anything" do
      json = %{foo: %{bar: [0, ""], baz: nil, pax: %{}}}
      assert JSONSchema.Utils.traverse(json, fn value -> value end) == json
    end

    test "can remove specific items, keeping the original order of lists" do
      assert JSONSchema.Utils.traverse(
               %{
                 foo: [
                   "a",
                   %{type: "string", "$comment": "only :internal"},
                   %{type: "number"}
                 ]
               },
               fn
                 %{"$comment": "only :internal"} -> :remove
                 value -> value
               end
             ) == %{foo: ["a", %{type: "number"}]}
    end

    test "can transform specific items" do
      assert JSONSchema.Utils.traverse(
               %{
                 foo: [
                   %{type: "string", "$comment": "anything"},
                   %{type: "number"}
                 ]
               },
               fn
                 %{"$comment": "anything"} -> %{type: "number", "$comment": "transformed"}
                 value -> value
               end
             ) == %{foo: [%{type: "number", "$comment": "transformed"}, %{type: "number"}]}
    end
  end
end