zf

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

intent.test.exs (7307B)


      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.Intent do
     19 use ZenflowsTest.Help.EctoCase, async: true
     20 
     21 alias Ecto.Changeset
     22 alias Zenflows.VF.Intent
     23 
     24 setup do
     25 	%{params: %{
     26 		name: Factory.uniq("name"),
     27 		action_id: Factory.build(:action_id),
     28 		input_of_id: Factory.insert!(:process).id,
     29 		output_of_id: Factory.insert!(:process).id,
     30 		resource_classified_as: Factory.uniq_list("uri"),
     31 		resource_conforms_to_id: Factory.insert!(:resource_specification).id,
     32 		resource_inventoried_as_id: Factory.insert!(:economic_resource).id,
     33 		resource_quantity: %{
     34 			has_unit_id: Factory.insert!(:unit).id,
     35 			has_numerical_value: Factory.float(),
     36 		},
     37 		effort_quantity: %{
     38 			has_unit_id: Factory.insert!(:unit).id,
     39 			has_numerical_value: Factory.float(),
     40 		},
     41 		available_quantity: %{
     42 			has_unit_id: Factory.insert!(:unit).id,
     43 			has_numerical_value: Factory.float(),
     44 		},
     45 		at_location_id: Factory.insert!(:spatial_thing).id,
     46 		has_beginning: DateTime.utc_now(),
     47 		has_end: DateTime.utc_now(),
     48 		has_point_in_time: DateTime.utc_now(),
     49 		due: DateTime.utc_now(),
     50 		finished: Factory.bool(),
     51 		image: Factory.img(),
     52 		note: Factory.uniq("note"),
     53 		# in_scope_of_id:
     54 		agreed_in: Factory.uniq("uri"),
     55 	}}
     56 end
     57 
     58 describe "create Intent" do
     59 	test "with only :provider", %{params: params} do
     60 		params = Map.put(params, :provider_id, Factory.insert!(:agent).id)
     61 
     62 		assert {:ok, %Intent{} = int} =
     63 			params
     64 			|> Intent.chgset()
     65 			|> Repo.insert()
     66 
     67 		assert int.name == params.name
     68 		assert int.action_id == params.action_id
     69 		assert int.provider_id == params.provider_id
     70 		assert int.receiver_id == nil
     71 		assert int.input_of_id == params.input_of_id
     72 		assert int.output_of_id == params.output_of_id
     73 		assert int.resource_classified_as == params.resource_classified_as
     74 		assert int.resource_conforms_to_id == params.resource_conforms_to_id
     75 		assert int.resource_inventoried_as_id == params.resource_inventoried_as_id
     76 		assert int.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id
     77 		assert int.resource_quantity_has_numerical_value == params.resource_quantity.has_numerical_value
     78 		assert int.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id
     79 		assert int.effort_quantity_has_numerical_value == params.effort_quantity.has_numerical_value
     80 		assert int.available_quantity_has_unit_id == params.available_quantity.has_unit_id
     81 		assert int.available_quantity_has_numerical_value == params.available_quantity.has_numerical_value
     82 		assert int.at_location_id == params.at_location_id
     83 		assert int.has_beginning == params.has_beginning
     84 		assert int.has_end == params.has_end
     85 		assert int.has_point_in_time == params.has_point_in_time
     86 		assert int.due == params.due
     87 		assert int.finished == params.finished
     88 		assert int.image == params.image
     89 		assert int.note == params.note
     90 		# assert in_scope_of_id
     91 		assert int.agreed_in == params.agreed_in
     92 	end
     93 
     94 	test "with only :receiver", %{params: params} do
     95 		params = Map.put(params, :receiver_id, Factory.insert!(:agent).id)
     96 
     97 		assert {:ok, %Intent{} = int} =
     98 			params
     99 			|> Intent.chgset()
    100 			|> Repo.insert()
    101 
    102 		assert int.name == params.name
    103 		assert int.action_id == params.action_id
    104 		assert int.provider_id == nil
    105 		assert int.receiver_id == params.receiver_id
    106 		assert int.input_of_id == params.input_of_id
    107 		assert int.output_of_id == params.output_of_id
    108 		assert int.resource_classified_as == params.resource_classified_as
    109 		assert int.resource_conforms_to_id == params.resource_conforms_to_id
    110 		assert int.resource_inventoried_as_id == params.resource_inventoried_as_id
    111 		assert int.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id
    112 		assert int.resource_quantity_has_numerical_value == params.resource_quantity.has_numerical_value
    113 		assert int.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id
    114 		assert int.effort_quantity_has_numerical_value == params.effort_quantity.has_numerical_value
    115 		assert int.available_quantity_has_unit_id == params.available_quantity.has_unit_id
    116 		assert int.available_quantity_has_numerical_value == params.available_quantity.has_numerical_value
    117 		assert int.at_location_id == params.at_location_id
    118 		assert int.has_beginning == params.has_beginning
    119 		assert int.has_end == params.has_end
    120 		assert int.has_point_in_time == params.has_point_in_time
    121 		assert int.due == params.due
    122 		assert int.finished == params.finished
    123 		assert int.image == params.image
    124 		assert int.note == params.note
    125 		# assert in_scope_of_id
    126 		assert int.agreed_in == params.agreed_in
    127 	end
    128 
    129 	test "with both :provider and :receiver", %{params: params} do
    130 		params =
    131 			params
    132 			|> Map.put(:provider_id, Factory.insert!(:agent).id)
    133 			|> Map.put(:receiver_id, Factory.insert!(:agent).id)
    134 
    135 		assert {:error, %Changeset{errors: errs}} =
    136 			params
    137 			|> Intent.chgset()
    138 			|> Repo.insert()
    139 
    140 		assert {:ok, _} = Keyword.fetch(errs, :provider_id)
    141 		assert {:ok, _} = Keyword.fetch(errs, :receiver_id)
    142 	end
    143 end
    144 
    145 test "update Intent", %{params: params} do
    146 	old = Factory.insert!(:intent)
    147 
    148 	assert {:ok, %Intent{} = new} =
    149 		old
    150 		|> Intent.chgset(params)
    151 		|> Repo.update()
    152 
    153 	assert new.name == params.name
    154 	assert new.action_id == params.action_id
    155 	assert new.provider_id == old.provider_id
    156 	assert new.receiver_id == old.receiver_id
    157 	assert new.input_of_id == params.input_of_id
    158 	assert new.output_of_id == params.output_of_id
    159 	assert new.resource_classified_as == params.resource_classified_as
    160 	assert new.resource_conforms_to_id == params.resource_conforms_to_id
    161 	assert new.resource_inventoried_as_id == params.resource_inventoried_as_id
    162 	assert new.resource_quantity_has_unit_id == params.resource_quantity.has_unit_id
    163 	assert new.resource_quantity_has_numerical_value == params.resource_quantity.has_numerical_value
    164 	assert new.effort_quantity_has_unit_id == params.effort_quantity.has_unit_id
    165 	assert new.effort_quantity_has_numerical_value == params.effort_quantity.has_numerical_value
    166 	assert new.available_quantity_has_unit_id == params.available_quantity.has_unit_id
    167 	assert new.available_quantity_has_numerical_value == params.available_quantity.has_numerical_value
    168 	assert new.at_location_id == params.at_location_id
    169 	assert new.has_beginning == params.has_beginning
    170 	assert new.has_end == params.has_end
    171 	assert new.has_point_in_time == params.has_point_in_time
    172 	assert new.due == params.due
    173 	assert new.finished == params.finished
    174 	assert new.image == params.image
    175 	assert new.note == params.note
    176 	# assert in_scope_of_id
    177 	assert new.agreed_in == params.agreed_in
    178 end
    179 end