zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

validate.test.exs (5956B)


      1 # Zenflows is designed to implement the Valueflows vocabulary,
      2 # written and maintained by srfsh <info@dyne.org>.
      3 # Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>.
      4 #
      5 # This program is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU Affero General Public License as published by
      7 # the Free Software Foundation, either version 3 of the License, or
      8 # (at your option) any later version.
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU Affero General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU Affero General Public License
     16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 
     18 defmodule ZenflowsTest.VF.Validate do
     19 use ExUnit.Case, async: true
     20 
     21 alias Ecto.Changeset
     22 alias Zenflows.VF.Validate
     23 
     24 @spec key_chgset(map()) :: Changeset.t()
     25 defp key_chgset(changes) do
     26 	Changeset.change({%{}, %{key: :string}}, changes)
     27 end
     28 
     29 @spec name_chgset(map()) :: Changeset.t()
     30 defp name_chgset(changes) do
     31 	Changeset.change({%{}, %{name: :string}}, changes)
     32 end
     33 
     34 @spec note_chgset(map()) :: Changeset.t()
     35 defp note_chgset(changes) do
     36 	Changeset.change({%{}, %{note: :string}}, changes)
     37 end
     38 
     39 @spec img_chgset(map()) :: Changeset.t()
     40 defp img_chgset(changes) do
     41 	Changeset.change({%{}, %{img: :string}}, changes)
     42 end
     43 
     44 @spec uri_chgset(map()) :: Changeset.t()
     45 defp uri_chgset(changes) do
     46 	Changeset.change({%{}, %{uri: :string}}, changes)
     47 end
     48 
     49 @spec class_chgset(map()) :: Changeset.t()
     50 defp class_chgset(changes) do
     51 	Changeset.change({%{}, %{list: {:array, :string}}}, changes)
     52 end
     53 
     54 describe "key/2" do
     55 	test "with too short param" do
     56 		assert %Changeset{errors: errs} =
     57 			%{key: String.duplicate("a", 15)}
     58 			|> key_chgset()
     59 			|> Validate.key(:key)
     60 
     61 		assert {:ok, _} = Keyword.fetch(errs, :key)
     62 	end
     63 
     64 	test "with too long param" do
     65 		assert %Changeset{errors: errs} =
     66 			%{key: String.duplicate("a", 2048 + 1)}
     67 			|> key_chgset()
     68 			|> Validate.key(:key)
     69 
     70 		assert {:ok, _} = Keyword.fetch(errs, :key)
     71 	end
     72 
     73 	test "with the right size param" do
     74 		assert %Changeset{errors: errs} =
     75 			%{key: String.duplicate("a", 16)}
     76 			|> key_chgset()
     77 			|> Validate.key(:key)
     78 
     79 		assert :error = Keyword.fetch(errs, :key)
     80 	end
     81 end
     82 
     83 describe "name/2" do
     84 	test "with too short param" do
     85 		assert %Changeset{errors: errs} =
     86 			%{name: ""}
     87 			|> name_chgset()
     88 			|> Validate.name(:name)
     89 
     90 		assert {:ok, _} = Keyword.fetch(errs, :name)
     91 	end
     92 
     93 	test "with too long param" do
     94 		assert %Changeset{errors: errs} =
     95 			%{name: String.duplicate("a", 256 + 1)}
     96 			|> name_chgset()
     97 			|> Validate.name(:name)
     98 
     99 		assert {:ok, _} = Keyword.fetch(errs, :name)
    100 	end
    101 
    102 	test "with the right size param" do
    103 		assert %Changeset{errors: errs} =
    104 			%{name: "aa"}
    105 			|> name_chgset()
    106 			|> Validate.name(:name)
    107 
    108 		assert :error = Keyword.fetch(errs, :name)
    109 	end
    110 end
    111 
    112 describe "note/2" do
    113 	test "with too short param" do
    114 		assert %Changeset{errors: errs} =
    115 			%{note: ""}
    116 			|> note_chgset()
    117 			|> Validate.note(:note)
    118 
    119 		assert {:ok, _} = Keyword.fetch(errs, :note)
    120 	end
    121 
    122 	test "with too long param" do
    123 		assert %Changeset{errors: errs} =
    124 			%{note: String.duplicate("a", 2048 + 1)}
    125 			|> note_chgset()
    126 			|> Validate.note(:note)
    127 
    128 		assert {:ok, _} = Keyword.fetch(errs, :note)
    129 	end
    130 
    131 	test "with the right size param" do
    132 		assert %Changeset{errors: errs} =
    133 			%{note: "aa"}
    134 			|> note_chgset()
    135 			|> Validate.note(:note)
    136 
    137 		assert :error = Keyword.fetch(errs, :note)
    138 	end
    139 end
    140 
    141 describe "img/2" do
    142 	test "with too short param" do
    143 		assert %Changeset{errors: errs} =
    144 			%{img: ""}
    145 			|> img_chgset()
    146 			|> Validate.img(:img)
    147 
    148 		assert {:ok, _} = Keyword.fetch(errs, :img)
    149 	end
    150 
    151 	test "with too long param" do
    152 		assert %Changeset{errors: errs} =
    153 			%{img: String.duplicate("a", 25 * 1024 * 1024 + 1)}
    154 			|> img_chgset()
    155 			|> Validate.img(:img)
    156 
    157 		assert {:ok, _} = Keyword.fetch(errs, :img)
    158 	end
    159 
    160 	test "with the right size param" do
    161 		assert %Changeset{errors: errs} =
    162 			%{img: String.duplicate("a", 1024)}
    163 			|> img_chgset()
    164 			|> Validate.img(:img)
    165 
    166 		assert :error = Keyword.fetch(errs, :img)
    167 	end
    168 end
    169 
    170 describe "uri/2" do
    171 	test "with too short param" do
    172 		assert %Changeset{errors: errs} =
    173 			%{uri: ""}
    174 			|> uri_chgset()
    175 			|> Validate.uri(:uri)
    176 
    177 		assert {:ok, _} = Keyword.fetch(errs, :uri)
    178 	end
    179 
    180 	test "with too long param" do
    181 		assert %Changeset{errors: errs} =
    182 			%{uri: String.duplicate("a", 512 + 1)}
    183 			|> uri_chgset()
    184 			|> Validate.uri(:uri)
    185 
    186 		assert {:ok, _} = Keyword.fetch(errs, :uri)
    187 	end
    188 
    189 	test "with the right size param" do
    190 		assert %Changeset{errors: errs} =
    191 			%{uri: "https://example.test/example.jpg"}
    192 			|> uri_chgset()
    193 			|> Validate.uri(:uri)
    194 
    195 		assert :error = Keyword.fetch(errs, :uri)
    196 	end
    197 end
    198 
    199 describe "class/2" do
    200 	test "with too few items" do
    201 		assert %Changeset{errors: errs} =
    202 			%{list: []}
    203 			|> class_chgset()
    204 			|> Validate.class(:list)
    205 
    206 		assert {:ok, _} = Keyword.fetch(errs, :list)
    207 	end
    208 
    209 	test "with too many items" do
    210 		assert %Changeset{errors: errs} =
    211 			%{list: Enum.map(0..127 + 1, &("uri #{&1}"))}
    212 			|> class_chgset()
    213 			|> Validate.class(:list)
    214 
    215 		assert {:ok, _} = Keyword.fetch(errs, :list)
    216 	end
    217 
    218 	test "with one of the items too short" do
    219 		assert %Changeset{errors: errs} =
    220 			%{list: Enum.map(0..64, &("uri #{&1}")) ++ [""]}
    221 			|> class_chgset()
    222 			|> Validate.class(:list)
    223 
    224 		assert {:ok, _} = Keyword.fetch(errs, :list)
    225 	end
    226 
    227 	test "with one of the items too long" do
    228 		assert %Changeset{errors: errs} =
    229 			%{list: Enum.map(0..64, &("uri #{&1}")) ++ [String.duplicate("a", 513)]}
    230 			|> class_chgset()
    231 			|> Validate.class(:list)
    232 
    233 		assert {:ok, _} = Keyword.fetch(errs, :list)
    234 	end
    235 
    236 	test "with everthing just right" do
    237 		assert %Changeset{errors: errs} =
    238 			%{list: ["aaa"]}
    239 			|> class_chgset()
    240 			|> Validate.class(:list)
    241 
    242 		assert :error = Keyword.fetch(errs, :list)
    243 	end
    244 end
    245 end