file.test.exs (6579B)
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.File do 19 use ZenflowsTest.Help.EctoCase, async: true 20 21 alias Ecto.Changeset 22 alias Zenflows.File 23 alias Zenflows.VF.{ 24 EconomicEvent, 25 EconomicResource, 26 Intent, 27 Organization, 28 Person, 29 RecipeResource, 30 ResourceSpecification, 31 } 32 33 test "works on RecipeResource" do 34 assert {:ok, %RecipeResource{} = rec_res} = 35 RecipeResource.Domain.create(%{ 36 name: Factory.str("name"), 37 images: [ 38 %{ 39 hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 40 name: "aaaaaaaaaaaaaaa", 41 description: "aaaaaaaa", 42 mime_type: "aaaaaa", 43 extension: "aaaaaaa", 44 size: 42, 45 signature: "aaaaaaaaaaaaaaaaaaaaaa", 46 width: 42, 47 height: 42, 48 }, 49 %{ 50 hash: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 51 name: "bbbbbbbbbbbbbbb", 52 description: "bbbbbbbb", 53 mime_type: "bbbbbb", 54 extension: "bbbbbbb", 55 size: 42, 56 signature: "bbbbbbbbbbbbbbbbbbbbbb", 57 width: 42, 58 height: 42, 59 }, 60 ], 61 }) 62 63 assert [%File{}, %File{}] = rec_res.images 64 end 65 66 test "works on EconomicResource" do 67 agent = Factory.insert!(:agent) 68 unit = Factory.insert!(:unit) 69 spec = Factory.insert!(:resource_specification) 70 {:ok, %EconomicEvent{} = evt} = 71 EconomicEvent.Domain.create( 72 %{ 73 action_id: "raise", 74 provider_id: agent.id, 75 receiver_id: agent.id, 76 has_point_in_time: Factory.now(), 77 resource_conforms_to_id: spec.id, 78 resource_quantity: %{ 79 has_numerical_value: 1, 80 has_unit_id: unit.id, 81 }, 82 }, 83 %{ 84 name: Factory.str("name"), 85 images: [ 86 %{ 87 hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 88 name: "aaaaaaaaaaaaaaa", 89 description: "aaaaaaaa", 90 mime_type: "aaaaaa", 91 extension: "aaaaaaa", 92 size: 42, 93 signature: "aaaaaaaaaaaaaaaaaaaaaa", 94 width: 42, 95 height: 42, 96 }, 97 %{ 98 hash: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 99 name: "bbbbbbbbbbbbbbb", 100 description: "bbbbbbbb", 101 mime_type: "bbbbbb", 102 extension: "bbbbbbb", 103 size: 42, 104 signature: "bbbbbbbbbbbbbbbbbbbbbb", 105 width: 42, 106 height: 42, 107 }, 108 ], 109 }) 110 111 evt = EconomicEvent.Domain.preload(evt, :resource_inventoried_as) 112 res = EconomicResource.Domain.preload(evt.resource_inventoried_as, :images) 113 114 [%File{}, %File{}] = res.images 115 end 116 117 test "works on Person Agent" do 118 assert {:ok, %Person{} = per} = 119 Person.Domain.create(%{ 120 name: Factory.str("name"), 121 user: Factory.str("user"), 122 email: "#{Factory.str("user")}@example.com", 123 images: [ 124 %{ 125 hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 126 name: "aaaaaaaaaaaaaaa", 127 description: "aaaaaaaa", 128 mime_type: "aaaaaa", 129 extension: "aaaaaaa", 130 size: 42, 131 signature: "aaaaaaaaaaaaaaaaaaaaaa", 132 width: 42, 133 height: 42, 134 }, 135 %{ 136 hash: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 137 name: "bbbbbbbbbbbbbbb", 138 description: "bbbbbbbb", 139 mime_type: "bbbbbb", 140 extension: "bbbbbbb", 141 size: 42, 142 signature: "bbbbbbbbbbbbbbbbbbbbbb", 143 width: 42, 144 height: 42, 145 }, 146 ], 147 }) 148 149 assert [%File{}, %File{}] = per.images 150 end 151 152 test "works on Organization Agent" do 153 assert {:ok, %Organization{} = org} = 154 Organization.Domain.create(%{ 155 name: Factory.str("name"), 156 images: [ 157 %{ 158 hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 159 name: "aaaaaaaaaaaaaaa", 160 description: "aaaaaaaa", 161 mime_type: "aaaaaa", 162 extension: "aaaaaaa", 163 size: 42, 164 signature: "aaaaaaaaaaaaaaaaaaaaaa", 165 width: 42, 166 height: 42, 167 }, 168 %{ 169 hash: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 170 name: "bbbbbbbbbbbbbbb", 171 description: "bbbbbbbb", 172 mime_type: "bbbbbb", 173 extension: "bbbbbbb", 174 size: 42, 175 signature: "bbbbbbbbbbbbbbbbbbbbbb", 176 width: 42, 177 height: 42, 178 }, 179 ], 180 }) 181 182 assert [%File{}, %File{}] = org.images 183 end 184 185 test "works on ResourceSpecification" do 186 assert {:ok, %ResourceSpecification{} = spec} = 187 ResourceSpecification.Domain.create(%{ 188 name: Factory.str("name"), 189 images: [ 190 %{ 191 hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 192 name: "aaaaaaaaaaaaaaa", 193 description: "aaaaaaaa", 194 mime_type: "aaaaaa", 195 extension: "aaaaaaa", 196 size: 42, 197 signature: "aaaaaaaaaaaaaaaaaaaaaa", 198 width: 42, 199 height: 42, 200 }, 201 %{ 202 hash: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 203 name: "bbbbbbbbbbbbbbb", 204 description: "bbbbbbbb", 205 mime_type: "bbbbbb", 206 extension: "bbbbbbb", 207 size: 42, 208 signature: "bbbbbbbbbbbbbbbbbbbbbb", 209 width: 42, 210 height: 42, 211 }, 212 ], 213 }) 214 215 assert [%File{}, %File{}] = spec.images 216 end 217 218 test "works on Intent" do 219 assert {:ok, %Intent{} = int} = 220 Intent.Domain.create(%{ 221 action_id: "raise", 222 provider_id: Factory.insert!(:agent).id, 223 images: [ 224 %{ 225 hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 226 name: "aaaaaaaaaaaaaaa", 227 description: "aaaaaaaa", 228 mime_type: "aaaaaa", 229 extension: "aaaaaaa", 230 size: 42, 231 signature: "aaaaaaaaaaaaaaaaaaaaaa", 232 width: 42, 233 height: 42, 234 }, 235 %{ 236 hash: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 237 name: "bbbbbbbbbbbbbbb", 238 description: "bbbbbbbb", 239 mime_type: "bbbbbb", 240 extension: "bbbbbbb", 241 size: 42, 242 signature: "bbbbbbbbbbbbbbbbbbbbbb", 243 width: 42, 244 height: 42, 245 }, 246 ], 247 }) 248 249 assert [%File{}, %File{}] = int.images 250 end 251 252 test "doesn't work without a belongs_to field" do 253 {:error, %Changeset{errors: errs}} = 254 File.changeset(%File{}, %{ 255 hash: "asnotehusnatoheusntaoehusntaeohu", 256 name: "satoehusnoaethu", 257 description: "foobaour", 258 mime_type: "foobar", 259 extension: "jpeeeeg", 260 size: 25, 261 signature: "faaosnetuhaoenthuousth", 262 width: 200, 263 height: 300, 264 }) 265 |> Repo.insert() 266 assert Keyword.has_key?(errs, :general) 267 end 268 end