zf

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

domain.test.exs (5778B)


      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.Proposal.Domain do
     19 use ZenflowsTest.Help.EctoCase, async: true
     20 
     21 alias Zenflows.VF.{
     22 	Proposal,
     23 	Proposal.Domain,
     24 	SpatialThing,
     25 }
     26 
     27 setup do
     28 	%{
     29 		params: %{
     30 			name: Factory.str("name"),
     31 			note: Factory.str("note"),
     32 			has_beginning: Factory.now(),
     33 			has_end: Factory.now(),
     34 			unit_based: Factory.bool(),
     35 			eligible_location_id: Factory.insert!(:spatial_thing).id,
     36 		},
     37 		inserted: Factory.insert!(:proposal),
     38 	}
     39 end
     40 
     41 describe "one/1" do
     42 	test "with good id: finds the Proposal", %{inserted: %{id: id}} do
     43 		assert {:ok, %Proposal{}} = Domain.one(id)
     44 	end
     45 
     46 	test "with bad id: doesn't find the Proposal" do
     47 		assert {:error, "not found"} = Domain.one(Factory.id())
     48 	end
     49 end
     50 
     51 describe "status/1" do
     52 	test "pending", %{inserted: proposal} do
     53 		int = Factory.insert!(:intent, finished: false)
     54 		Factory.insert!(:proposed_intent, published_in: proposal, publishes: int)
     55 		assert {:ok, :pending} = Domain.status(proposal.id)
     56 	end
     57 
     58 	test "accepted", %{inserted: proposal} do
     59 		int = Factory.insert!(:intent, finished: false)
     60 		Factory.insert!(:proposed_intent, published_in: proposal, publishes: int)
     61 		Factory.insert!(:satisfaction, satisfies: int)
     62 		assert {:ok, :accepted} = Domain.status(proposal.id)
     63 	end
     64 
     65 	test "refused", %{inserted: proposal} do
     66 		int = Factory.insert!(:intent, finished: true)
     67 		Factory.insert!(:proposed_intent, published_in: proposal, publishes: int)
     68 		assert {:ok, :refused} = Domain.status(proposal.id)
     69 	end
     70 end
     71 
     72 describe "create/1" do
     73 	test "with good params: creates a Proposal", %{params: params} do
     74 		assert {:ok, %Proposal{} = new} = Domain.create(params)
     75 		assert new.name == params.name
     76 		assert new.note == params.note
     77 		assert new.has_beginning == params.has_beginning
     78 		assert new.has_end == params.has_end
     79 		assert new.unit_based == params.unit_based
     80 		assert new.eligible_location_id == params.eligible_location_id
     81 	end
     82 
     83 	test "with empty params: creates a Proposal" do
     84 		assert {:ok, %Proposal{} = new} = Domain.create(%{})
     85 		assert new.name == nil
     86 		assert new.note == nil
     87 		assert new.has_beginning == nil
     88 		assert new.has_end == nil
     89 		assert new.unit_based == false # since it defaults
     90 		assert new.eligible_location_id == nil
     91 	end
     92 end
     93 
     94 describe "update/2" do
     95 	test "with good params: updates the Proposal", %{params: params, inserted: old} do
     96 		assert {:ok, %Proposal{} = new} = Domain.update(old.id, params)
     97 		assert new.name == params.name
     98 		assert new.note == params.note
     99 		assert new.has_beginning == params.has_beginning
    100 		assert new.has_end == params.has_end
    101 		assert new.unit_based == params.unit_based
    102 		assert new.eligible_location_id == params.eligible_location_id
    103 	end
    104 
    105 	test "with bad params: doesn't update the Proposal", %{inserted: old} do
    106 		assert {:ok, %Proposal{} = new} = Domain.update(old.id, %{})
    107 		assert new.name == old.name
    108 		assert new.note == old.note
    109 		assert new.has_beginning == old.has_beginning
    110 		assert new.has_end == old.has_end
    111 		assert new.unit_based == old.unit_based
    112 		assert new.eligible_location_id == old.eligible_location_id
    113 	end
    114 end
    115 
    116 describe "delete/1" do
    117 	test "with good id: deletes the Proposal", %{inserted: %{id: id}} do
    118 		assert {:ok, %Proposal{id: ^id}} = Domain.delete(id)
    119 		assert {:error, "not found"} = Domain.one(id)
    120 	end
    121 
    122 	test "with bad id: doesn't delete the Proposal" do
    123 		assert {:error, "not found"} = Domain.delete(Factory.id())
    124 	end
    125 end
    126 
    127 describe "preload/2" do
    128 	test "preloads `:eligible_location`", %{inserted: %{id: id}} do
    129 		assert {:ok, prop} =  Domain.one(id)
    130 		prop = Domain.preload(prop, :eligible_location)
    131 		assert %SpatialThing{} = prop.eligible_location
    132  	end
    133 
    134 	test "preloads `:publishes`", %{inserted: %{id: id}} do
    135 		assert {:ok, prop} =  Domain.one(id)
    136 
    137 		assert [] = Domain.preload(prop, :publishes).publishes
    138 
    139 		left =
    140 			Enum.map(0..9, fn _ ->
    141 				Factory.insert!(:proposed_intent, %{published_in: prop}).id
    142 			end)
    143 		right =
    144 			Domain.preload(prop, :publishes)
    145 			|> Map.fetch!(:publishes)
    146 			|> Enum.map(& &1.id)
    147 		assert left -- right == []
    148  	end
    149 
    150 	test "preloads `:primary_intents`", %{inserted: %{id: id}} do
    151 		assert {:ok, prop} =  Domain.one(id)
    152 
    153 		assert [] = Domain.preload(prop, :primary_intents).primary_intents
    154 
    155 		left =
    156 			Enum.map(0..9, fn _ ->
    157 				Factory.insert!(:proposed_intent, %{published_in: prop, reciprocal: false}).publishes_id
    158 			end)
    159 		right =
    160 			Domain.preload(prop, :primary_intents)
    161 			|> Map.fetch!(:primary_intents)
    162 			|> Enum.map(& &1.id)
    163 		assert left -- right == []
    164  	end
    165 
    166 	test "preloads `:reciprocal_intents`", %{inserted: %{id: id}} do
    167 		assert {:ok, prop} =  Domain.one(id)
    168 
    169 		assert [] = Domain.preload(prop, :reciprocal_intents).reciprocal_intents
    170 
    171 		left =
    172 			Enum.map(0..9, fn _ ->
    173 				Factory.insert!(:proposed_intent, %{published_in: prop, reciprocal: true}).publishes_id
    174 			end)
    175 		right =
    176 			Domain.preload(prop, :reciprocal_intents)
    177 			|> Map.fetch!(:reciprocal_intents)
    178 			|> Enum.map(& &1.id)
    179 		assert left -- right == []
    180  	end
    181 end
    182 end