@@ -11,6 +11,8 @@ defmodule AshPostgres.CalculationTest do
1111 Author ,
1212 Comedian ,
1313 Comment ,
14+ Container ,
15+ Item ,
1416 Post ,
1517 PostTag ,
1618 Record ,
@@ -1464,4 +1466,141 @@ defmodule AshPostgres.CalculationTest do
14641466 end )
14651467 end
14661468 end
1469+
1470+ describe "multiple relationships to the same resource with different filters don't affect calculations" do
1471+ test "calculations use correct read actions from their respective relationships" do
1472+ container = Ash.Seed . seed! ( Container , % { } )
1473+
1474+ item_active =
1475+ Ash.Seed . seed! ( Item , % {
1476+ container_id: container . id ,
1477+ name: "Active Item" ,
1478+ active: true
1479+ } )
1480+
1481+ _item_inactive =
1482+ Ash.Seed . seed! ( Item , % {
1483+ container_id: container . id ,
1484+ name: "Inactive Item" ,
1485+ active: false
1486+ } )
1487+
1488+ loaded_container = Ash . load! ( container , [ :item_all ] )
1489+ assert item_active . id == loaded_container . item_all . id
1490+
1491+ loaded_container = Ash . load! ( container , [ :item_active ] )
1492+ assert item_active . id == loaded_container . item_active . id
1493+
1494+ loaded_container = Ash . load! ( container , [ :active_item_name , :all_item_name ] )
1495+
1496+ assert loaded_container . active_item_name == "Active Item"
1497+
1498+ assert loaded_container . all_item_name == "Active Item"
1499+ end
1500+ end
1501+
1502+ test "calculations use correct read actions from their respective relationships" do
1503+ container = Ash.Seed . seed! ( Container , % { } )
1504+
1505+ item =
1506+ Ash.Seed . seed! ( Item , % {
1507+ container_id: container . id ,
1508+ name: "Inactive Item" ,
1509+ active: false
1510+ } )
1511+
1512+ loaded_container = Ash . load! ( container , [ :item_all ] )
1513+ assert item . id == loaded_container . item_all . id
1514+
1515+ loaded_container = Ash . load! ( container , [ :item_active ] )
1516+ assert nil == loaded_container . item_active
1517+
1518+ loaded_container = Ash . load! ( container , [ :active_item_name , :all_item_name ] )
1519+
1520+ assert loaded_container . active_item_name == nil
1521+
1522+ assert loaded_container . all_item_name == "Inactive Item"
1523+ end
1524+
1525+ test "loading calculations one at a time works" do
1526+ container = Ash.Seed . seed! ( Container , % { } )
1527+
1528+ item =
1529+ Ash.Seed . seed! ( Item , % {
1530+ container_id: container . id ,
1531+ name: "Inactive Item" ,
1532+ active: false
1533+ } )
1534+
1535+ loaded_container = Ash . load! ( container , [ :item_all ] )
1536+ assert item . id == loaded_container . item_all . id
1537+
1538+ loaded_container = Ash . load! ( container , [ :item_active ] )
1539+ assert nil == loaded_container . item_active
1540+
1541+ loaded_container = Ash . load! ( container , [ :active_item_name ] )
1542+ assert loaded_container . active_item_name == nil
1543+
1544+ loaded_container = Ash . load! ( container , [ :all_item_name ] )
1545+ assert loaded_container . all_item_name == "Inactive Item"
1546+ end
1547+
1548+ test "with active item, both calculations return values" do
1549+ container = Ash.Seed . seed! ( Container , % { } )
1550+
1551+ item =
1552+ Ash.Seed . seed! ( Item , % {
1553+ container_id: container . id ,
1554+ name: "Active Item" ,
1555+ active: true
1556+ } )
1557+
1558+ loaded_container = Ash . load! ( container , [ :item_all ] )
1559+ assert item . id == loaded_container . item_all . id
1560+
1561+ loaded_container = Ash . load! ( container , [ :item_active ] )
1562+ assert item . id == loaded_container . item_active . id
1563+
1564+ loaded_container = Ash . load! ( container , [ :active_item_name , :all_item_name ] )
1565+
1566+ assert loaded_container . active_item_name == "Active Item"
1567+ assert loaded_container . all_item_name == "Active Item"
1568+ end
1569+
1570+ test "multiple containers with mixed active/inactive items" do
1571+ container1 = Ash.Seed . seed! ( Container , % { } )
1572+
1573+ Ash.Seed . seed! ( Item , % {
1574+ container_id: container1 . id ,
1575+ name: "Inactive Item 1" ,
1576+ active: false
1577+ } )
1578+
1579+ container2 = Ash.Seed . seed! ( Container , % { } )
1580+
1581+ Ash.Seed . seed! ( Item , % {
1582+ container_id: container2 . id ,
1583+ name: "Active Item 2" ,
1584+ active: true
1585+ } )
1586+
1587+ _container3 = Ash.Seed . seed! ( Container , % { } )
1588+
1589+ containers =
1590+ Container
1591+ |> Ash.Query . sort ( :id )
1592+ |> Ash.Query . load ( [ :active_item_name , :all_item_name ] )
1593+ |> Ash . read! ( )
1594+
1595+ [ loaded1 , loaded2 , loaded3 ] = containers
1596+
1597+ assert loaded1 . active_item_name == nil
1598+ assert loaded1 . all_item_name == "Inactive Item 1"
1599+
1600+ assert loaded2 . active_item_name == "Active Item 2"
1601+ assert loaded2 . all_item_name == "Active Item 2"
1602+
1603+ assert loaded3 . active_item_name == nil
1604+ assert loaded3 . all_item_name == nil
1605+ end
14671606end
0 commit comments