domain.test.exs (8376B)
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.EconomicResource.Domain do 19 use ZenflowsTest.Help.EctoCase, async: true 20 21 alias Zenflows.VF.{ 22 Action, 23 Agent, 24 EconomicResource, 25 EconomicResource.Domain, 26 Measure, 27 ProcessSpecification, 28 ProductBatch, 29 ResourceSpecification, 30 SpatialThing, 31 Unit, 32 } 33 34 setup ctx do 35 %{id: unit_id} = Factory.insert!(:unit) 36 num_val = Factory.decimal() 37 params = %{ 38 name: Factory.str("name"), 39 note: Factory.str("note"), 40 #image: Factory.img(), 41 tracking_identifier: Factory.str("tracking identifier"), 42 classified_as: Factory.str_list("uri"), 43 conforms_to_id: Factory.insert!(:resource_specification).id, 44 accounting_quantity: %{ 45 has_unit_id: unit_id, 46 has_numerical_value: num_val, 47 }, 48 onhand_quantity: %{ 49 has_unit_id: unit_id, 50 has_numerical_value: num_val, 51 }, 52 primary_accountable_id: Factory.insert!(:agent).id, 53 custodian_id: Factory.insert!(:agent).id, 54 stage_id: Factory.insert!(:process_specification).id, 55 state_id: Factory.build(:action_id), 56 current_location_id: Factory.insert!(:spatial_thing).id, 57 lot_id: Factory.insert!(:product_batch).id, 58 contained_in_id: Factory.insert!(:economic_resource).id, 59 unit_of_effort_id: Factory.insert!(:unit).id, 60 okhv: Factory.str("okhv"), 61 repo: Factory.uri(), 62 version: Factory.str("version"), 63 licensor: Factory.str("licensor"), 64 license: Factory.str("license"), 65 metadata: %{"something" => "something"}, 66 } 67 68 if ctx[:no_insert] do 69 %{params: params} 70 else 71 %{params: params, inserted: Factory.insert!(:economic_resource)} 72 end 73 end 74 75 test "by_id/1 returns a EconomicResource", %{inserted: eco_res} do 76 assert {:ok, %EconomicResource{}} = Domain.one(eco_res.id) 77 end 78 79 test "classifications/0 returns list of unique `classified_as` values" do 80 Enum.each(1..10, fn _ -> Factory.insert!(:economic_resource) end) 81 left = Enum.flat_map(Domain.all!(), & &1.classified_as) 82 right = Domain.classifications() 83 assert [] = left -- right 84 end 85 86 describe "update/2" do 87 @tag skip: "TODO: fix economic resource factory" 88 test "doesn't update a EconomicResource", %{inserted: old} do 89 assert {:ok, %EconomicResource{} = new} = Domain.update(old.id, %{}) 90 assert new.name == old.name 91 assert new.primary_accountable_id == old.primary_accountable_id 92 assert new.custodian_id == old.custodian_id 93 assert new.classified_as == old.classified_as 94 assert new.conforms_to_id == old.conforms_to_id 95 assert new.tracking_identifier == old.tracking_identifier 96 assert new.lot_id == old.lot_id 97 assert new.accounting_quantity_has_unit_id == old.accounting_quantity_has_unit_id 98 assert new.accounting_quantity_has_numerical_value == old.accounting_quantity_has_numerical_value 99 assert new.onhand_quantity_has_unit_id == old.onhand_quantity_has_unit_id 100 assert new.onhand_quantity_has_numerical_value == old.onhand_quantity_has_numerical_value 101 assert new.current_location_id == old.current_location_id 102 assert new.note == old.note 103 #assert new.image == old.image 104 assert new.unit_of_effort_id == old.unit_of_effort_id 105 assert new.stage_id == old.stage_id 106 assert new.state_id == old.state_id 107 assert new.contained_in_id == old.contained_in_id 108 end 109 110 @tag skip: "TODO: fix economic resource factory" 111 test "updates a EconomicResource with valid params", %{params: params, inserted: old} do 112 assert {:ok, %EconomicResource{} = new} = Domain.update(old.id, params) 113 assert new.name == params.name 114 assert new.primary_accountable_id == params.primary_accountable_id 115 assert new.custodian_id == params.custodian_id 116 assert new.classified_as == params.classified_as 117 assert new.conforms_to_id == params.conforms_to_id 118 assert new.tracking_identifier == params.tracking_identifier 119 assert new.lot_id == params.lot_id 120 assert new.accounting_quantity_has_unit_id == params.accounting_quantity.has_unit_id 121 assert new.accounting_quantity_has_numerical_value == params.accounting_quantity.has_numerical_value 122 assert new.onhand_quantity_has_unit_id == params.onhand_quantity.has_unit_id 123 assert new.onhand_quantity_has_numerical_value == params.onhand_quantity.has_numerical_value 124 assert new.current_location_id == params.current_location_id 125 assert new.note == params.note 126 #assert new.image == params.image 127 assert new.unit_of_effort_id == params.unit_of_effort_id 128 assert new.stage_id == params.stage_id 129 assert new.state_id == params.state_id 130 assert new.contained_in_id == params.contained_in_id 131 end 132 end 133 134 @tag skip: "TODO: needs to deal with previous_event_id" 135 test "delete/1 deletes a EconomicResource", %{inserted: %{id: id}} do 136 assert {:ok, %EconomicResource{id: ^id}} = Domain.delete(id) 137 assert {:error, "not found"} = Domain.one(id) 138 end 139 140 describe "preload/2" do 141 test "preloads :conforms_to", %{inserted: eco_res} do 142 eco_res = Domain.preload(eco_res, :conforms_to) 143 assert confo_to = %ResourceSpecification{} = eco_res.conforms_to 144 assert confo_to.id == eco_res.conforms_to_id 145 end 146 147 test "preloads :accounting_quantity", %{inserted: eco_res} do 148 eco_res = Domain.preload(eco_res, :accounting_quantity) 149 assert accnt_qty = %Measure{} = eco_res.accounting_quantity 150 assert accnt_qty.has_unit_id == eco_res.accounting_quantity_has_unit_id 151 assert accnt_qty.has_numerical_value == eco_res.accounting_quantity_has_numerical_value 152 end 153 154 test "preloads :onhand_quantity", %{inserted: eco_res} do 155 eco_res = Domain.preload(eco_res, :onhand_quantity) 156 assert onhnd_qty = %Measure{} = eco_res.onhand_quantity 157 assert onhnd_qty.has_unit_id == eco_res.onhand_quantity_has_unit_id 158 assert onhnd_qty.has_numerical_value == eco_res.onhand_quantity_has_numerical_value 159 end 160 161 test "preloads :primary_accountable", %{inserted: eco_res} do 162 eco_res = Domain.preload(eco_res, :primary_accountable) 163 assert pri_accnt = %Agent{} = eco_res.primary_accountable 164 assert pri_accnt.id == eco_res.primary_accountable_id 165 end 166 167 test "preloads :custodian", %{inserted: eco_res} do 168 eco_res = Domain.preload(eco_res, :custodian) 169 assert custo = %Agent{} = eco_res.custodian 170 assert custo.id == eco_res.custodian_id 171 end 172 173 @tag skip: "TODO: fix EconomicResource factory" 174 test "preloads :stage", %{inserted: eco_res} do 175 eco_res = Domain.preload(eco_res, :stage) 176 assert stage = %ProcessSpecification{} = eco_res.stage 177 assert stage.id == eco_res.stage_id 178 end 179 180 @tag skip: "TODO: fix EconomicResource factory" 181 test "preloads :state", %{inserted: eco_res} do 182 eco_res = Domain.preload(eco_res, :state) 183 assert action = %Action{} = eco_res.state 184 assert action.id == eco_res.state_id 185 end 186 187 @tag skip: "TODO: fix EconomicResource factory" 188 test "preloads :current_location", %{inserted: eco_res} do 189 eco_res = Domain.preload(eco_res, :current_location) 190 assert cur_loc = %SpatialThing{} = eco_res.current_location 191 assert cur_loc.id == eco_res.current_location_id 192 end 193 194 @tag skip: "TODO: fix EconomicResource factory" 195 test "preloads :lot", %{inserted: eco_res} do 196 eco_res = Domain.preload(eco_res, :lot) 197 assert lot = %ProductBatch{} = eco_res.lot 198 assert lot.id == eco_res.lot_id 199 end 200 201 test "preloads :contained_in", %{inserted: eco_res} do 202 eco_res = Domain.preload(eco_res, :contained_in) 203 if eco_res.contained_in do 204 assert cont_in = %EconomicResource{} = eco_res.contained_in 205 assert cont_in.id == eco_res.contained_in_id 206 end 207 end 208 209 @tag skip: "TODO: fix EconomicResource factory" 210 test "preloads :unit_of_effort", %{inserted: eco_res} do 211 eco_res = Domain.preload(eco_res, :unit_of_effort) 212 assert unit_eff = %Unit{} = eco_res.unit_of_effort 213 assert unit_eff.id == eco_res.unit_of_effort_id 214 end 215 end 216 end