commitment.test.exs (12680B)
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.Commitment do 19 use ZenflowsTest.Help.EctoCase, async: true 20 21 alias Ecto.Changeset 22 alias Zenflows.VF.Commitment 23 24 setup do 25 %{params: %{ 26 action_id: Factory.build(:action_id), 27 provider_id: Factory.insert!(:agent).id, 28 receiver_id: Factory.insert!(:agent).id, 29 input_of_id: Factory.insert!(:process).id, 30 output_of_id: Factory.insert!(:process).id, 31 resource_classified_as: Factory.str_list("uri"), 32 resource_quantity: %{ 33 has_unit_id: Factory.insert!(:unit).id, 34 has_numerical_value: Factory.decimal(), 35 }, 36 effort_quantity: %{ 37 has_unit_id: Factory.insert!(:unit).id, 38 has_numerical_value: Factory.decimal(), 39 }, 40 resource_inventoried_as_id: Factory.insert!(:economic_resource).id, 41 resource_conforms_to_id: Factory.insert!(:resource_specification).id, 42 has_point_in_time: Factory.now(), 43 has_beginning: Factory.now(), 44 has_end: Factory.now(), 45 due: Factory.now(), 46 finished: Factory.bool(), 47 note: Factory.str("note"), 48 # in_scope_of_id: 49 agreed_in: Factory.str("uri"), 50 independent_demand_of_id: Factory.insert!(:plan).id, 51 at_location_id: Factory.insert!(:spatial_thing).id, 52 clause_of_id: Factory.insert!(:agreement).id, 53 }} 54 end 55 56 describe "create Commitment" do 57 test "with both :has_point_in_time and :has_beginning", %{params: params} do 58 params = params 59 |> Map.delete(:resource_inventoried_as_id) 60 |> Map.delete(:has_end) 61 62 assert {:error, %Changeset{errors: errs}} = 63 params 64 |> Commitment.changeset() 65 |> Repo.insert() 66 67 assert {:ok, _} = Keyword.fetch(errs, :has_point_in_time) 68 assert {:ok, _} = Keyword.fetch(errs, :has_beginning) 69 end 70 71 test "with both :has_point_in_time and :has_end", %{params: params} do 72 params = params 73 |> Map.delete(:resource_inventoried_as_id) 74 |> Map.delete(:has_beginning) 75 76 assert {:error, %Changeset{errors: errs}} = 77 params 78 |> Commitment.changeset() 79 |> Repo.insert() 80 81 assert {:ok, _} = Keyword.fetch(errs, :has_point_in_time) 82 assert {:ok, _} = Keyword.fetch(errs, :has_end) 83 end 84 85 test "with only :has_point_in_time", %{params: params} do 86 params = params 87 |> Map.delete(:resource_inventoried_as_id) 88 |> Map.delete(:has_beginning) 89 |> Map.delete(:has_end) 90 91 assert {:ok, %Commitment{} = comm} = 92 params 93 |> Commitment.changeset() 94 |> Repo.insert() 95 96 assert comm.action_id == params.action_id 97 assert comm.provider_id == params.provider_id 98 assert comm.receiver_id == params.receiver_id 99 assert comm.input_of_id == params.input_of_id 100 assert comm.output_of_id == params.output_of_id 101 assert comm.resource_classified_as == params.resource_classified_as 102 assert comm.resource_conforms_to_id == params.resource_conforms_to_id 103 assert comm.resource_inventoried_as_id == nil 104 assert comm.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id 105 assert Decimal.eq?(comm.resource_quantity_has_numerical_value, params.resource_quantity.has_numerical_value) 106 assert comm.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id 107 assert Decimal.eq?(comm.effort_quantity_has_numerical_value, params.effort_quantity.has_numerical_value) 108 assert comm.has_beginning == nil 109 assert comm.has_end == nil 110 assert comm.has_point_in_time == params.has_point_in_time 111 assert comm.due == params.due 112 assert comm.finished == params.finished 113 assert comm.note == params.note 114 # assert in_scope_of_id 115 assert comm.agreed_in == params.agreed_in 116 assert comm.independent_demand_of_id == params.independent_demand_of_id 117 assert comm.at_location_id == params.at_location_id 118 assert comm.clause_of_id == params.clause_of_id 119 end 120 121 test "with only :has_beginning", %{params: params} do 122 params = params 123 |> Map.delete(:resource_inventoried_as_id) 124 |> Map.delete(:has_point_in_time) 125 |> Map.delete(:has_end) 126 127 assert {:ok, %Commitment{} = comm} = 128 params 129 |> Commitment.changeset() 130 |> Repo.insert() 131 132 assert comm.action_id == params.action_id 133 assert comm.provider_id == params.provider_id 134 assert comm.receiver_id == params.receiver_id 135 assert comm.input_of_id == params.input_of_id 136 assert comm.output_of_id == params.output_of_id 137 assert comm.resource_classified_as == params.resource_classified_as 138 assert comm.resource_conforms_to_id == params.resource_conforms_to_id 139 assert comm.resource_inventoried_as_id == nil 140 assert comm.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id 141 assert Decimal.eq?(comm.resource_quantity_has_numerical_value, params.resource_quantity.has_numerical_value) 142 assert comm.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id 143 assert Decimal.eq?(comm.effort_quantity_has_numerical_value, params.effort_quantity.has_numerical_value) 144 assert comm.has_beginning == params.has_beginning 145 assert comm.has_end == nil 146 assert comm.has_point_in_time == nil 147 assert comm.due == params.due 148 assert comm.finished == params.finished 149 assert comm.note == params.note 150 # assert in_scope_of_id 151 assert comm.agreed_in == params.agreed_in 152 assert comm.independent_demand_of_id == params.independent_demand_of_id 153 assert comm.at_location_id == params.at_location_id 154 assert comm.clause_of_id == params.clause_of_id 155 end 156 157 test "with only :has_end", %{params: params} do 158 params = params 159 |> Map.delete(:resource_inventoried_as_id) 160 |> Map.delete(:has_point_in_time) 161 |> Map.delete(:has_beginning) 162 163 assert {:ok, %Commitment{} = comm} = 164 params 165 |> Commitment.changeset() 166 |> Repo.insert() 167 168 assert comm.action_id == params.action_id 169 assert comm.provider_id == params.provider_id 170 assert comm.receiver_id == params.receiver_id 171 assert comm.input_of_id == params.input_of_id 172 assert comm.output_of_id == params.output_of_id 173 assert comm.resource_classified_as == params.resource_classified_as 174 assert comm.resource_conforms_to_id == params.resource_conforms_to_id 175 assert comm.resource_inventoried_as_id == nil 176 assert comm.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id 177 assert Decimal.eq?(comm.resource_quantity_has_numerical_value, params.resource_quantity.has_numerical_value) 178 assert comm.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id 179 assert Decimal.eq?(comm.effort_quantity_has_numerical_value, params.effort_quantity.has_numerical_value) 180 assert comm.has_beginning == nil 181 assert comm.has_end == params.has_end 182 assert comm.has_point_in_time == nil 183 assert comm.due == params.due 184 assert comm.finished == params.finished 185 assert comm.note == params.note 186 # assert in_scope_of_id 187 assert comm.agreed_in == params.agreed_in 188 assert comm.independent_demand_of_id == params.independent_demand_of_id 189 assert comm.at_location_id == params.at_location_id 190 assert comm.clause_of_id == params.clause_of_id 191 end 192 193 test "with both :has_beginning and :has_end", %{params: params} do 194 params = params 195 |> Map.delete(:resource_inventoried_as_id) 196 |> Map.delete(:has_point_in_time) 197 198 assert {:ok, %Commitment{} = comm} = 199 params 200 |> Commitment.changeset() 201 |> Repo.insert() 202 203 assert comm.action_id == params.action_id 204 assert comm.provider_id == params.provider_id 205 assert comm.receiver_id == params.receiver_id 206 assert comm.input_of_id == params.input_of_id 207 assert comm.output_of_id == params.output_of_id 208 assert comm.resource_classified_as == params.resource_classified_as 209 assert comm.resource_conforms_to_id == params.resource_conforms_to_id 210 assert comm.resource_inventoried_as_id == nil 211 assert comm.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id 212 assert Decimal.eq?(comm.resource_quantity_has_numerical_value, params.resource_quantity.has_numerical_value) 213 assert comm.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id 214 assert Decimal.eq?(comm.effort_quantity_has_numerical_value, params.effort_quantity.has_numerical_value) 215 assert comm.has_beginning == params.has_beginning 216 assert comm.has_end == params.has_end 217 assert comm.has_point_in_time == nil 218 assert comm.due == params.due 219 assert comm.finished == params.finished 220 assert comm.note == params.note 221 # assert in_scope_of_id 222 assert comm.agreed_in == params.agreed_in 223 assert comm.independent_demand_of_id == params.independent_demand_of_id 224 assert comm.at_location_id == params.at_location_id 225 assert comm.clause_of_id == params.clause_of_id 226 end 227 228 test "with both :resource_conforms_to and :resource_inventoried_as", %{params: params} do 229 params = Map.delete(params, :has_point_in_time) 230 231 assert {:error, %Changeset{errors: errs}} = 232 params 233 |> Commitment.changeset() 234 |> Repo.insert() 235 236 assert {:ok, _} = Keyword.fetch(errs, :resource_conforms_to_id) 237 assert {:ok, _} = Keyword.fetch(errs, :resource_inventoried_as_id) 238 end 239 240 test "with only :resource_conforms_to", %{params: params} do 241 params = params 242 |> Map.delete(:has_point_in_time) 243 |> Map.delete(:resource_inventoried_as_id) 244 245 assert {:ok, %Commitment{} = comm} = 246 params 247 |> Commitment.changeset() 248 |> Repo.insert() 249 250 assert comm.action_id == params.action_id 251 assert comm.provider_id == params.provider_id 252 assert comm.receiver_id == params.receiver_id 253 assert comm.input_of_id == params.input_of_id 254 assert comm.output_of_id == params.output_of_id 255 assert comm.resource_classified_as == params.resource_classified_as 256 assert comm.resource_conforms_to_id == params.resource_conforms_to_id 257 assert comm.resource_inventoried_as_id == nil 258 assert comm.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id 259 assert Decimal.eq?(comm.resource_quantity_has_numerical_value, params.resource_quantity.has_numerical_value) 260 assert comm.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id 261 assert Decimal.eq?(comm.effort_quantity_has_numerical_value, params.effort_quantity.has_numerical_value) 262 assert comm.has_beginning == params.has_beginning 263 assert comm.has_end == params.has_end 264 assert comm.has_point_in_time == nil 265 assert comm.due == params.due 266 assert comm.finished == params.finished 267 assert comm.note == params.note 268 # assert in_scope_of_id 269 assert comm.agreed_in == params.agreed_in 270 assert comm.independent_demand_of_id == params.independent_demand_of_id 271 assert comm.at_location_id == params.at_location_id 272 assert comm.clause_of_id == params.clause_of_id 273 end 274 275 test "with only :resource_inventoried_as", %{params: params} do 276 params = params 277 |> Map.delete(:has_point_in_time) 278 |> Map.delete(:resource_conforms_to_id) 279 280 assert {:ok, %Commitment{} = comm} = 281 params 282 |> Commitment.changeset() 283 |> Repo.insert() 284 285 assert comm.action_id == params.action_id 286 assert comm.provider_id == params.provider_id 287 assert comm.receiver_id == params.receiver_id 288 assert comm.input_of_id == params.input_of_id 289 assert comm.output_of_id == params.output_of_id 290 assert comm.resource_classified_as == params.resource_classified_as 291 assert comm.resource_conforms_to_id == nil 292 assert comm.resource_inventoried_as_id == params.resource_inventoried_as_id 293 assert comm.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id 294 assert Decimal.eq?(comm.resource_quantity_has_numerical_value, params.resource_quantity.has_numerical_value) 295 assert comm.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id 296 assert Decimal.eq?(comm.effort_quantity_has_numerical_value, params.effort_quantity.has_numerical_value) 297 assert comm.has_beginning == params.has_beginning 298 assert comm.has_end == params.has_end 299 assert comm.has_point_in_time == nil 300 assert comm.due == params.due 301 assert comm.finished == params.finished 302 assert comm.note == params.note 303 # assert in_scope_of_id 304 assert comm.agreed_in == params.agreed_in 305 assert comm.independent_demand_of_id == params.independent_demand_of_id 306 assert comm.at_location_id == params.at_location_id 307 assert comm.clause_of_id == params.clause_of_id 308 end 309 end 310 end