|
| 1 | +defmodule GroupherServer.Test.Helper.Validator.Schema do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + use GroupherServerWeb.ConnCase, async: true |
| 5 | + |
| 6 | + alias Helper.Validator.Schema |
| 7 | + |
| 8 | + describe "[basic schema]" do |
| 9 | + test "string with options" do |
| 10 | + schema = %{"text" => [:string, required: false]} |
| 11 | + data = %{"no_exsit" => "text"} |
| 12 | + assert {:ok, _} = Schema.cast(schema, data) |
| 13 | + |
| 14 | + schema = %{"text" => [:string, required: true]} |
| 15 | + data = %{"no_exsit" => "text"} |
| 16 | + {:error, error} = Schema.cast(schema, data) |
| 17 | + assert error == [%{field: "text", message: "should be: string", value: nil}] |
| 18 | + |
| 19 | + schema = %{"text" => [:string, required: true]} |
| 20 | + data = %{"text" => "text"} |
| 21 | + assert {:ok, _} = Schema.cast(schema, data) |
| 22 | + |
| 23 | + schema = %{"text" => [:string, min: 5]} |
| 24 | + data = %{"text" => "text"} |
| 25 | + {:error, error} = Schema.cast(schema, data) |
| 26 | + assert error == [%{field: "text", message: "min size: 5", value: "text"}] |
| 27 | + |
| 28 | + schema = %{"text" => [:string, required: false, min: 5]} |
| 29 | + data = %{"text" => "text"} |
| 30 | + {:error, error} = Schema.cast(schema, data) |
| 31 | + assert error == [%{field: "text", message: "min size: 5", value: "text"}] |
| 32 | + |
| 33 | + schema = %{"text" => [:string, min: 5]} |
| 34 | + data = %{"no_exsit" => "text"} |
| 35 | + {:error, error} = Schema.cast(schema, data) |
| 36 | + assert error == [%{field: "text", message: "should be: string", value: nil}] |
| 37 | + |
| 38 | + schema = %{"text" => [:string, required: true, min: 5]} |
| 39 | + data = %{"no_exsit" => "text"} |
| 40 | + {:error, error} = Schema.cast(schema, data) |
| 41 | + assert error == [%{field: "text", message: "should be: string", value: nil}] |
| 42 | + |
| 43 | + schema = %{"text" => [:string, required: true, min: "5"]} |
| 44 | + data = %{"text" => "text"} |
| 45 | + {:error, error} = Schema.cast(schema, data) |
| 46 | + assert error == [%{field: "text", message: "unknow option: min: 5", value: "text"}] |
| 47 | + # IO.inspect(Schema.cast(schema, data), label: "schema result") |
| 48 | + end |
| 49 | + |
| 50 | + test "number with options" do |
| 51 | + schema = %{"text" => [:number, required: false]} |
| 52 | + data = %{"no_exsit" => 1} |
| 53 | + assert {:ok, _} = Schema.cast(schema, data) |
| 54 | + |
| 55 | + schema = %{"text" => [:number, required: true]} |
| 56 | + data = %{"no_exsit" => 1} |
| 57 | + {:error, error} = Schema.cast(schema, data) |
| 58 | + assert error == [%{field: "text", message: "should be: number", value: nil}] |
| 59 | + |
| 60 | + schema = %{"text" => [:number, required: true]} |
| 61 | + data = %{"text" => 1} |
| 62 | + assert {:ok, _} = Schema.cast(schema, data) |
| 63 | + |
| 64 | + schema = %{"text" => [:number, min: 5]} |
| 65 | + data = %{"text" => 4} |
| 66 | + {:error, error} = Schema.cast(schema, data) |
| 67 | + assert error == [%{field: "text", message: "min size: 5", value: 4}] |
| 68 | + |
| 69 | + schema = %{"text" => [:number, required: false, min: 5]} |
| 70 | + data = %{"text" => 4} |
| 71 | + {:error, error} = Schema.cast(schema, data) |
| 72 | + assert error == [%{field: "text", message: "min size: 5", value: 4}] |
| 73 | + |
| 74 | + schema = %{"text" => [:number, min: 5]} |
| 75 | + data = %{"no_exsit" => 4} |
| 76 | + {:error, error} = Schema.cast(schema, data) |
| 77 | + assert error == [%{field: "text", message: "should be: number", value: nil}] |
| 78 | + |
| 79 | + schema = %{"text" => [:number, required: true, min: 5]} |
| 80 | + data = %{"no_exsit" => 1} |
| 81 | + {:error, error} = Schema.cast(schema, data) |
| 82 | + assert error == [%{field: "text", message: "should be: number", value: nil}] |
| 83 | + |
| 84 | + # IO.inspect(Schema.cast(schema, data), label: "schema result") |
| 85 | + # hello world |
| 86 | + end |
| 87 | + |
| 88 | + test "number with wrong option" do |
| 89 | + schema = %{"text" => [:number, required: true, min: "5"]} |
| 90 | + data = %{"text" => 1} |
| 91 | + |
| 92 | + {:error, error} = Schema.cast(schema, data) |
| 93 | + assert error == [%{field: "text", message: "unknow option: min: 5", value: 1}] |
| 94 | + |
| 95 | + schema = %{"text" => [:number, required: true, no_exsit_option: "xxx"]} |
| 96 | + data = %{"text" => 1} |
| 97 | + |
| 98 | + {:error, error} = Schema.cast(schema, data) |
| 99 | + assert error == [%{field: "text", message: "unknow option: no_exsit_option: xxx", value: 1}] |
| 100 | + end |
| 101 | + |
| 102 | + test "number with options edage case" do |
| 103 | + schema = %{"text" => [:number, min: 2]} |
| 104 | + data = %{"text" => "aa"} |
| 105 | + |
| 106 | + {:error, error} = Schema.cast(schema, data) |
| 107 | + assert error == [%{field: "text", message: "should be: number", value: "aa"}] |
| 108 | + end |
| 109 | + |
| 110 | + test "list with options" do |
| 111 | + schema = %{"text" => [:list, required: false]} |
| 112 | + data = %{"no_exsit" => []} |
| 113 | + assert {:ok, _} = Schema.cast(schema, data) |
| 114 | + |
| 115 | + schema = %{"text" => [:list, required: true]} |
| 116 | + data = %{"no_exsit" => []} |
| 117 | + {:error, error} = Schema.cast(schema, data) |
| 118 | + assert error == [%{field: "text", message: "should be: list", value: nil}] |
| 119 | + |
| 120 | + schema = %{"text" => [:list, required: true]} |
| 121 | + data = %{"text" => []} |
| 122 | + assert {:ok, _} = Schema.cast(schema, data) |
| 123 | + end |
| 124 | + |
| 125 | + test "boolean with options" do |
| 126 | + schema = %{"text" => [:boolean, required: false]} |
| 127 | + data = %{"no_exsit" => false} |
| 128 | + assert {:ok, _} = Schema.cast(schema, data) |
| 129 | + |
| 130 | + schema = %{"text" => [:boolean, required: true]} |
| 131 | + data = %{"no_exsit" => false} |
| 132 | + {:error, error} = Schema.cast(schema, data) |
| 133 | + assert error == [%{field: "text", message: "should be: boolean", value: nil}] |
| 134 | + |
| 135 | + schema = %{"text" => [:boolean, required: true]} |
| 136 | + data = %{"text" => false} |
| 137 | + assert {:ok, _} = Schema.cast(schema, data) |
| 138 | + end |
| 139 | + |
| 140 | + test "enum with options" do |
| 141 | + schema = %{"text" => [enum: [1, 2, 3], required: false]} |
| 142 | + data = %{"no_exsit" => false} |
| 143 | + assert {:ok, _} = Schema.cast(schema, data) |
| 144 | + |
| 145 | + schema = %{"text" => [enum: [1, 2, 3], required: true]} |
| 146 | + data = %{"no_exsit" => false} |
| 147 | + {:error, error} = Schema.cast(schema, data) |
| 148 | + assert error == [%{field: "text", message: "should be: 1 | 2 | 3"}] |
| 149 | + |
| 150 | + schema = %{"text" => [enum: [1, 2, 3]]} |
| 151 | + data = %{"text" => 1} |
| 152 | + assert {:ok, _} = Schema.cast(schema, data) |
| 153 | + |
| 154 | + # IO.inspect(Schema.cast(schema, data), label: "schema result") |
| 155 | + # hello world |
| 156 | + end |
| 157 | + end |
| 158 | +end |
0 commit comments