zf

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

domain.test.exs (12318B)


      1 # Zenflows is designed to implement the Valueflows vocabulary,
      2 # written and maintained by srfsh <info@dyne.org>.
      3 # Copyright (C) 2021-2023 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.RecipeFlow.Domain do
     19 use ZenflowsTest.Help.EctoCase, async: true
     20 
     21 alias Ecto.Changeset
     22 alias Zenflows.VF.{
     23 	Action,
     24 	Measure,
     25 	RecipeExchange,
     26 	RecipeFlow,
     27 	RecipeFlow.Domain,
     28 	RecipeProcess,
     29 	RecipeResource,
     30 }
     31 
     32 setup do
     33 	%{
     34 		params: %{
     35 			action_id: Factory.build(:action_id),
     36 			recipe_input_of_id: Factory.insert!(:recipe_process).id,
     37 			recipe_output_of_id: Factory.insert!(:recipe_process).id,
     38 			recipe_flow_resource_id: Factory.insert!(:recipe_resource).id,
     39 			resource_quantity: %{
     40 				has_unit_id: Factory.insert!(:unit).id,
     41 				has_numerical_value: Factory.decimal(),
     42 			},
     43 			effort_quantity: %{
     44 				has_unit_id: Factory.insert!(:unit).id,
     45 				has_numerical_value: Factory.decimal(),
     46 			},
     47 			recipe_clause_of_id: Factory.insert!(:recipe_exchange).id,
     48 			note: Factory.str("some note"),
     49 	 	},
     50 	 	inserted: Factory.insert!(:recipe_flow),
     51 	 }
     52 end
     53 
     54 describe "one/1" do
     55 	test "with good id: finds the RecipeFlow", %{inserted: %{id: id}} do
     56 		assert {:ok, %RecipeFlow{}} = Domain.one(id)
     57 	end
     58 
     59 	test "with bad id: doesn't find the RecipeFlow" do
     60 		assert {:error, "not found"} = Domain.one(Factory.id())
     61 	end
     62 end
     63 
     64 describe "create/1" do
     65 	test "with good params (with :resource_quantity): creates a RecipeFlow", %{params: params} do
     66 		params = Map.delete(params, :effort_quantity)
     67 		assert {:ok, %RecipeFlow{} = new} = Domain.create(params)
     68 		assert new.note == params.note
     69 		assert new.action_id == params.action_id
     70 		assert new.recipe_input_of_id == params.recipe_input_of_id
     71 		assert new.recipe_output_of_id == params.recipe_output_of_id
     72 		assert new.recipe_flow_resource_id == params.recipe_flow_resource_id
     73 		assert new.recipe_clause_of_id == params.recipe_clause_of_id
     74 		assert new.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id
     75 		assert Decimal.eq?(new.resource_quantity_has_numerical_value, params.resource_quantity.has_numerical_value)
     76 		assert new.effort_quantity_has_unit_id == nil
     77 		assert new.effort_quantity_has_numerical_value == nil
     78 	end
     79 
     80 	test "with good params (with :effort_quantity): creates a RecipeFlow", %{params: params} do
     81 		params = Map.delete(params, :resource_quantity)
     82 		assert {:ok, %RecipeFlow{} = new} = Domain.create(params)
     83 		assert new.note == params.note
     84 		assert new.action_id == params.action_id
     85 		assert new.recipe_input_of_id == params.recipe_input_of_id
     86 		assert new.recipe_output_of_id == params.recipe_output_of_id
     87 		assert new.recipe_flow_resource_id == params.recipe_flow_resource_id
     88 		assert new.recipe_clause_of_id == params.recipe_clause_of_id
     89 		assert new.resource_quantity_has_unit_id == nil
     90 		assert new.resource_quantity_has_numerical_value == nil
     91 		assert new.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id
     92 		assert Decimal.eq?(new.effort_quantity_has_numerical_value, params.effort_quantity.has_numerical_value)
     93 	end
     94 
     95 	test "with good params (with :resource_quantity and :effort_quantity): creates a RecipeFlow", %{params: params} do
     96 		assert {:ok, %RecipeFlow{} = new} = Domain.create(params)
     97 		assert new.note == params.note
     98 		assert new.action_id == params.action_id
     99 		assert new.recipe_input_of_id == params.recipe_input_of_id
    100 		assert new.recipe_output_of_id == params.recipe_output_of_id
    101 		assert new.recipe_flow_resource_id == params.recipe_flow_resource_id
    102 		assert new.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id
    103 		assert Decimal.eq?(new.resource_quantity_has_numerical_value, params.resource_quantity.has_numerical_value)
    104 		assert new.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id
    105 		assert Decimal.eq?(new.effort_quantity_has_numerical_value, params.effort_quantity.has_numerical_value)
    106 	end
    107 
    108 	test "with bad params (without :resource_qunatity and :effort_quantity): doesn't create a RecipeFlow", %{params: params} do
    109 		params =
    110 			params
    111 			|> Map.delete(:resource_quantity)
    112 			|> Map.delete(:effort_quantity)
    113 
    114 		assert {:error, %Changeset{errors: errs}} = Domain.create(params)
    115 		assert Keyword.has_key?(errs, :resource_quantity_has_numerical_value)
    116 		assert Keyword.has_key?(errs, :resource_quantity_has_unit_id)
    117 		assert Keyword.has_key?(errs, :effort_quantity_has_numerical_value)
    118 		assert Keyword.has_key?(errs, :effort_quantity_has_unit_id)
    119 	end
    120 
    121 	test "with bad params: doesn't create a RecipeFlow" do
    122 		assert {:error, %Changeset{}} = Domain.create(%{})
    123 	end
    124 end
    125 
    126 describe "update/2" do
    127 	test "with good params (with :resource_quantity): updates the RecipeFlow", %{params: params, inserted: old} do
    128 		params = Map.delete(params, :effort_quantity)
    129 		assert {:ok, %RecipeFlow{} = new} = Domain.update(old.id, params)
    130 		assert new.note == params.note
    131 		assert new.action_id == params.action_id
    132 		assert new.recipe_input_of_id == params.recipe_input_of_id
    133 		assert new.recipe_output_of_id == params.recipe_output_of_id
    134 		assert new.recipe_flow_resource_id == params.recipe_flow_resource_id
    135 		assert new.recipe_clause_of_id == params.recipe_clause_of_id
    136 		assert new.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id
    137 		assert Decimal.eq?(new.resource_quantity_has_numerical_value, params.resource_quantity.has_numerical_value)
    138 		assert new.effort_quantity_has_unit_id == old.effort_quantity_has_unit_id
    139 		assert Decimal.eq?(new.effort_quantity_has_numerical_value, old.effort_quantity_has_numerical_value)
    140 	end
    141 
    142 	test "with good params (with :effort_quantity): updates the RecipeFlow", %{params: params, inserted: old} do
    143 		params = Map.delete(params, :resource_quantity)
    144 		assert {:ok, %RecipeFlow{} = new} = Domain.update(old.id, params)
    145 		assert new.note == params.note
    146 		assert new.action_id == params.action_id
    147 		assert new.recipe_input_of_id == params.recipe_input_of_id
    148 		assert new.recipe_output_of_id == params.recipe_output_of_id
    149 		assert new.recipe_flow_resource_id == params.recipe_flow_resource_id
    150 		assert new.recipe_clause_of_id == params.recipe_clause_of_id
    151 		assert new.resource_quantity_has_unit_id == old.resource_quantity_has_unit_id
    152 		assert Decimal.eq?(new.resource_quantity_has_numerical_value, old.resource_quantity_has_numerical_value)
    153 		assert new.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id
    154 		assert Decimal.eq?(new.effort_quantity_has_numerical_value, params.effort_quantity.has_numerical_value)
    155 	end
    156 
    157 	test "with good params (with :resource_quantity set to nil): updates the RecipeFlow", %{params: params, inserted: old} do
    158 		params =
    159 			params
    160 			|> Map.delete(:effort_quantity)
    161 			|> Map.put(:resource_quantity, nil)
    162 
    163 		assert {:ok, %RecipeFlow{} = new} = Domain.update(old.id, params)
    164 		assert new.note == params.note
    165 		assert new.action_id == params.action_id
    166 		assert new.recipe_input_of_id == params.recipe_input_of_id
    167 		assert new.recipe_output_of_id == params.recipe_output_of_id
    168 		assert new.recipe_flow_resource_id == params.recipe_flow_resource_id
    169 		assert new.recipe_clause_of_id == params.recipe_clause_of_id
    170 		assert new.resource_quantity_has_unit_id == nil
    171 		assert new.resource_quantity_has_numerical_value == nil
    172 		assert new.effort_quantity_has_unit_id == old.effort_quantity_has_unit_id
    173 		assert Decimal.eq?(new.effort_quantity_has_numerical_value, old.effort_quantity_has_numerical_value)
    174 	end
    175 
    176 	test "with good params (with :effort_quantity set to nil): updates the RecipeFlow", %{params: params, inserted: old} do
    177 		params =
    178 			params
    179 			|> Map.delete(:resource_quantity)
    180 			|> Map.put(:effort_quantity, nil)
    181 
    182 		assert {:ok, %RecipeFlow{} = new} = Domain.update(old.id, params)
    183 		assert new.note == params.note
    184 		assert new.action_id == params.action_id
    185 		assert new.recipe_input_of_id == params.recipe_input_of_id
    186 		assert new.recipe_output_of_id == params.recipe_output_of_id
    187 		assert new.recipe_flow_resource_id == params.recipe_flow_resource_id
    188 		assert new.recipe_clause_of_id == params.recipe_clause_of_id
    189 		assert new.resource_quantity_has_unit_id == old.resource_quantity_has_unit_id
    190 		assert Decimal.eq?(new.resource_quantity_has_numerical_value, old.resource_quantity_has_numerical_value)
    191 		assert new.effort_quantity_has_unit_id == nil
    192 		assert new.effort_quantity_has_numerical_value == nil
    193 	end
    194 
    195 	test "with bad params: doesn't update the RecipeFlow", %{inserted: old} do
    196 		assert {:ok, %RecipeFlow{} = new} = Domain.update(old.id, %{})
    197 		assert new.note == old.note
    198 		assert new.action_id == old.action_id
    199 		assert new.recipe_input_of_id == old.recipe_input_of_id
    200 		assert new.recipe_output_of_id == old.recipe_output_of_id
    201 		assert new.recipe_flow_resource_id == old.recipe_flow_resource_id
    202 		assert new.recipe_clause_of_id == old.recipe_clause_of_id
    203 		assert new.resource_quantity_has_unit_id == old.resource_quantity_has_unit_id
    204 		assert Decimal.eq?(new.resource_quantity_has_numerical_value, old.resource_quantity_has_numerical_value)
    205 		assert new.effort_quantity_has_unit_id == old.effort_quantity_has_unit_id
    206 		assert Decimal.eq?(new.effort_quantity_has_numerical_value, old.effort_quantity_has_numerical_value)
    207 	end
    208 
    209 	test "with bad params (with :resource_quantity and :effort_quantity set to nil): doesn't update the RecipeFlow", %{params: params, inserted: old} do
    210 		params =
    211 			params
    212 			|> Map.put(:resource_quantity, nil)
    213 			|> Map.put(:effort_quantity, nil)
    214 
    215 		assert {:error, %Changeset{}} = Domain.update(old.id, params)
    216 	end
    217 end
    218 
    219 describe "delete/1" do
    220 	test "with good id: deletes the RecipeFlow", %{inserted: %{id: id}} do
    221 		assert {:ok, %RecipeFlow{id: ^id}} = Domain.delete(id)
    222 		assert {:error, "not found"} = Domain.one(id)
    223 	end
    224 
    225 	test "with bad id: doesn't delete the RecipeFlow" do
    226 		assert {:error, "not found"} = Domain.delete(Factory.id())
    227 	end
    228 end
    229 
    230 describe "preload/2" do
    231 	test "preloads :resource_quantity", %{inserted: rec_flow} do
    232 		rec_flow = Domain.preload(rec_flow, :resource_quantity)
    233 		assert res_qty = %Measure{} = rec_flow.resource_quantity
    234 		assert res_qty.has_unit_id == rec_flow.resource_quantity_has_unit_id
    235 		assert res_qty.has_numerical_value == rec_flow.resource_quantity_has_numerical_value
    236 	end
    237 
    238 	test "preloads :effort_quantity", %{inserted: rec_flow} do
    239 		rec_flow = Domain.preload(rec_flow, :effort_quantity)
    240 		assert eff_qty = %Measure{} = rec_flow.effort_quantity
    241 		assert eff_qty.has_unit_id == rec_flow.effort_quantity_has_unit_id
    242 		assert eff_qty.has_numerical_value == rec_flow.effort_quantity_has_numerical_value
    243 	end
    244 
    245 	test "preloads :recipe_flow_resource", %{inserted: rec_flow} do
    246 		rec_flow = Domain.preload(rec_flow, :recipe_flow_resource)
    247 		assert rec_flow_res = %RecipeResource{} = rec_flow.recipe_flow_resource
    248 		assert rec_flow_res.id == rec_flow.recipe_flow_resource_id
    249 	end
    250 
    251 	test "preloads :action", %{inserted: rec_flow} do
    252 		rec_flow = Domain.preload(rec_flow, :action)
    253 		assert action = %Action{} = rec_flow.action
    254 		assert action.id == rec_flow.action_id
    255 	end
    256 
    257 	test "preloads :recipe_input_of", %{inserted: rec_flow} do
    258 		rec_flow = Domain.preload(rec_flow, :recipe_input_of)
    259 		assert rec_in_of = %RecipeProcess{} = rec_flow.recipe_input_of
    260 		assert rec_in_of.id == rec_flow.recipe_input_of_id
    261 	end
    262 
    263 	test "preloads :recipe_output_of", %{inserted: rec_flow} do
    264 		rec_flow = Domain.preload(rec_flow, :recipe_output_of)
    265 		assert rec_out_of = %RecipeProcess{} = rec_flow.recipe_output_of
    266 		assert rec_out_of.id == rec_flow.recipe_output_of_id
    267 	end
    268 
    269 	test "preloads :recipe_clause_of", %{inserted: rec_flow} do
    270 		rec_flow = Domain.preload(rec_flow, :recipe_clause_of)
    271 		assert rec_clause_of = %RecipeExchange{} = rec_flow.recipe_clause_of
    272 		assert rec_clause_of.id == rec_flow.recipe_clause_of_id
    273 	end
    274 end
    275 end