zf

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

type.test.exs (4835B)


      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.Type do
     19 use ZenflowsTest.Help.AbsinCase, async: true
     20 
     21 setup do
     22 	%{
     23 		params: %{
     24 			"name" => Factory.str("name"),
     25 			"note" => Factory.str("note"),
     26 			"hasBeginning" => Factory.iso_now(),
     27 			"hasEnd" => Factory.iso_now(),
     28 			"unitBased" => Factory.bool(),
     29 			"eligibleLocation" => Factory.insert!(:spatial_thing).id,
     30 		},
     31 		inserted: Factory.insert!(:proposal),
     32 	}
     33 end
     34 
     35 @frag """
     36 fragment proposal on Proposal {
     37 	id
     38 	name
     39 	note
     40 	hasBeginning
     41 	hasEnd
     42 	unitBased
     43 	created
     44 	eligibleLocation {id}
     45 }
     46 """
     47 
     48 describe "Query" do
     49 	test "proposal", %{inserted: new} do
     50 		assert %{data: %{"proposal" => data}} =
     51 			run!("""
     52 				#{@frag}
     53 				query ($id: ID!) {
     54 					proposal(id: $id) {...proposal}
     55 				}
     56 			""", vars: %{"id" => new.id})
     57 
     58 		assert data["id"] == new.id
     59 		assert data["name"] == new.name
     60 		assert data["note"] == new.note
     61 		assert data["unitBased"] == new.unit_based
     62 		assert data["eligibleLocation"]["id"] == new.eligible_location_id
     63 		assert {:ok, created, 0} = DateTime.from_iso8601(data["created"])
     64 		assert DateTime.compare(DateTime.utc_now(), created) != :lt
     65 		assert {:ok, has_beginning, 0} = DateTime.from_iso8601(data["hasBeginning"])
     66 		assert DateTime.compare(DateTime.utc_now(), has_beginning) != :lt
     67 		assert {:ok, has_end, 0} = DateTime.from_iso8601(data["hasEnd"])
     68 		assert DateTime.compare(DateTime.utc_now(), has_end) != :lt
     69 	end
     70 
     71 	test "offers" do
     72 		assert %{data: %{"offers" => data}} =
     73 			run!("""
     74 				#{@frag}
     75 				query {
     76 					offers {
     77 						pageInfo {
     78 							startCursor
     79 							endCursor
     80 							hasPreviousPage
     81 							hasNextPage
     82 							totalCount
     83 							pageLimit
     84 						}
     85 						edges {
     86 							cursor
     87 							node {...proposal}
     88 						}
     89 					}
     90 				}
     91 			""")
     92 		assert %{
     93 			"pageInfo" => %{
     94 				"startCursor" => nil,
     95 				"endCursor" => nil,
     96 				"hasPreviousPage" => false,
     97 				"hasNextPage" => false,
     98 				"totalCount" => nil,
     99 				"pageLimit" => nil,
    100 			},
    101 			"edges" => [],
    102 		} = data
    103 	end
    104 
    105 	test "requests" do
    106 		assert %{data: %{"requests" => data}} =
    107 			run!("""
    108 				#{@frag}
    109 				query {
    110 					requests {
    111 						pageInfo {
    112 							startCursor
    113 							endCursor
    114 							hasPreviousPage
    115 							hasNextPage
    116 							totalCount
    117 							pageLimit
    118 						}
    119 						edges {
    120 							cursor
    121 							node {...proposal}
    122 						}
    123 					}
    124 				}
    125 			""")
    126 		assert %{
    127 			"pageInfo" => %{
    128 				"startCursor" => nil,
    129 				"endCursor" => nil,
    130 				"hasPreviousPage" => false,
    131 				"hasNextPage" => false,
    132 				"totalCount" => nil,
    133 				"pageLimit" => nil,
    134 			},
    135 			"edges" => [],
    136 		} = data
    137 	end
    138 end
    139 
    140 describe "Mutation" do
    141 	test "createProposal", %{params: params} do
    142 		assert %{data: %{"createProposal" => %{"proposal" => data}}} =
    143 			run!("""
    144 				#{@frag}
    145 				mutation ($proposal: ProposalCreateParams!) {
    146 					createProposal(proposal: $proposal) {
    147 						proposal {...proposal}
    148 					}
    149 				}
    150 			""", vars: %{"proposal" => params})
    151 
    152 		assert {:ok, _} = Zenflows.DB.ID.cast(data["id"])
    153 		keys = ~w[name note unitBased hasBeginning hasEnd]
    154 		assert Map.take(data, keys) == Map.take(params, keys)
    155 		assert data["eligibleLocation"]["id"] == params["eligibleLocation"]
    156 		assert {:ok, created, 0} = DateTime.from_iso8601(data["created"])
    157 		assert DateTime.compare(DateTime.utc_now(), created) != :lt
    158 	end
    159 
    160 	test "updateProposal", %{params: params, inserted: old} do
    161 		assert %{data: %{"updateProposal" => %{"proposal" => data}}} =
    162 			run!("""
    163 				#{@frag}
    164 				mutation ($proposal: ProposalUpdateParams!) {
    165 					updateProposal(proposal: $proposal) {
    166 						proposal {...proposal}
    167 					}
    168 				}
    169 			""", vars: %{"proposal" => Map.put(params, "id", old.id)})
    170 
    171 		assert data["id"] == old.id
    172 		keys = ~w[name note unitBased hasBeginning hasEnd]
    173 		assert Map.take(data, keys) == Map.take(params, keys)
    174 		assert data["eligibleLocation"]["id"] == params["eligibleLocation"]
    175 	end
    176 
    177 	test "deleteProposal", %{inserted: %{id: id}} do
    178 		assert %{data: %{"deleteProposal" => true}} =
    179 			run!("""
    180 				mutation ($id: ID!) {
    181 					deleteProposal(id: $id)
    182 				}
    183 			""", vars: %{"id" => id})
    184 	end
    185 end
    186 end