Skip to content

Commit e0f0055

Browse files
committed
move tests into existing calc test file
1 parent 29db3e6 commit e0f0055

File tree

2 files changed

+139
-124
lines changed

2 files changed

+139
-124
lines changed

test/calculation_test.exs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ defmodule AshPostgres.CalculationTest do
1111
Author,
1212
Comedian,
1313
Comment,
14+
Container,
15+
Item,
1416
Post,
1517
PostTag,
1618
Record,
@@ -1241,4 +1243,141 @@ defmodule AshPostgres.CalculationTest do
12411243

12421244
assert [] == Ash.read!(query)
12431245
end
1246+
1247+
describe "multiple relationships to the same resource with different filters don't affect calculations" do
1248+
test "calculations use correct read actions from their respective relationships" do
1249+
container = Ash.Seed.seed!(Container, %{})
1250+
1251+
item_active =
1252+
Ash.Seed.seed!(Item, %{
1253+
container_id: container.id,
1254+
name: "Active Item",
1255+
active: true
1256+
})
1257+
1258+
_item_inactive =
1259+
Ash.Seed.seed!(Item, %{
1260+
container_id: container.id,
1261+
name: "Inactive Item",
1262+
active: false
1263+
})
1264+
1265+
loaded_container = Ash.load!(container, [:item_all])
1266+
assert item_active.id == loaded_container.item_all.id
1267+
1268+
loaded_container = Ash.load!(container, [:item_active])
1269+
assert item_active.id == loaded_container.item_active.id
1270+
1271+
loaded_container = Ash.load!(container, [:active_item_name, :all_item_name])
1272+
1273+
assert loaded_container.active_item_name == "Active Item"
1274+
1275+
assert loaded_container.all_item_name == "Active Item"
1276+
end
1277+
end
1278+
1279+
test "calculations use correct read actions from their respective relationships" do
1280+
container = Ash.Seed.seed!(Container, %{})
1281+
1282+
item =
1283+
Ash.Seed.seed!(Item, %{
1284+
container_id: container.id,
1285+
name: "Inactive Item",
1286+
active: false
1287+
})
1288+
1289+
loaded_container = Ash.load!(container, [:item_all])
1290+
assert item.id == loaded_container.item_all.id
1291+
1292+
loaded_container = Ash.load!(container, [:item_active])
1293+
assert nil == loaded_container.item_active
1294+
1295+
loaded_container = Ash.load!(container, [:active_item_name, :all_item_name])
1296+
1297+
assert loaded_container.active_item_name == nil
1298+
1299+
assert loaded_container.all_item_name == "Inactive Item"
1300+
end
1301+
1302+
test "loading calculations one at a time works" do
1303+
container = Ash.Seed.seed!(Container, %{})
1304+
1305+
item =
1306+
Ash.Seed.seed!(Item, %{
1307+
container_id: container.id,
1308+
name: "Inactive Item",
1309+
active: false
1310+
})
1311+
1312+
loaded_container = Ash.load!(container, [:item_all])
1313+
assert item.id == loaded_container.item_all.id
1314+
1315+
loaded_container = Ash.load!(container, [:item_active])
1316+
assert nil == loaded_container.item_active
1317+
1318+
loaded_container = Ash.load!(container, [:active_item_name])
1319+
assert loaded_container.active_item_name == nil
1320+
1321+
loaded_container = Ash.load!(container, [:all_item_name])
1322+
assert loaded_container.all_item_name == "Inactive Item"
1323+
end
1324+
1325+
test "with active item, both calculations return values" do
1326+
container = Ash.Seed.seed!(Container, %{})
1327+
1328+
item =
1329+
Ash.Seed.seed!(Item, %{
1330+
container_id: container.id,
1331+
name: "Active Item",
1332+
active: true
1333+
})
1334+
1335+
loaded_container = Ash.load!(container, [:item_all])
1336+
assert item.id == loaded_container.item_all.id
1337+
1338+
loaded_container = Ash.load!(container, [:item_active])
1339+
assert item.id == loaded_container.item_active.id
1340+
1341+
loaded_container = Ash.load!(container, [:active_item_name, :all_item_name])
1342+
1343+
assert loaded_container.active_item_name == "Active Item"
1344+
assert loaded_container.all_item_name == "Active Item"
1345+
end
1346+
1347+
test "multiple containers with mixed active/inactive items" do
1348+
container1 = Ash.Seed.seed!(Container, %{})
1349+
1350+
Ash.Seed.seed!(Item, %{
1351+
container_id: container1.id,
1352+
name: "Inactive Item 1",
1353+
active: false
1354+
})
1355+
1356+
container2 = Ash.Seed.seed!(Container, %{})
1357+
1358+
Ash.Seed.seed!(Item, %{
1359+
container_id: container2.id,
1360+
name: "Active Item 2",
1361+
active: true
1362+
})
1363+
1364+
_container3 = Ash.Seed.seed!(Container, %{})
1365+
1366+
containers =
1367+
Container
1368+
|> Ash.Query.sort(:id)
1369+
|> Ash.Query.load([:active_item_name, :all_item_name])
1370+
|> Ash.read!()
1371+
1372+
[loaded1, loaded2, loaded3] = containers
1373+
1374+
assert loaded1.active_item_name == nil
1375+
assert loaded1.all_item_name == "Inactive Item 1"
1376+
1377+
assert loaded2.active_item_name == "Active Item 2"
1378+
assert loaded2.all_item_name == "Active Item 2"
1379+
1380+
assert loaded3.active_item_name == nil
1381+
assert loaded3.all_item_name == nil
1382+
end
12441383
end

test/calculation_with_multiple_relationships_test.exs

Lines changed: 0 additions & 124 deletions
This file was deleted.

0 commit comments

Comments
 (0)