forked from MasoniteFramework/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for HasOneThrough eager loading
- Loading branch information
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
tests/sqlite/relationships/test_sqlite_has_through_relationships.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import unittest | ||
|
||
from build.lib.masoniteorm.collection import Collection | ||
from build.lib.masoniteorm.scopes import scope | ||
from src.masoniteorm.models import Model | ||
from src.masoniteorm.relationships import has_one_through | ||
from tests.integrations.config.database import DB | ||
from tests.integrations.config.database import DATABASES | ||
from src.masoniteorm.schema import Schema | ||
from src.masoniteorm.schema.platforms import SQLitePlatform | ||
|
||
|
||
class Port(Model): | ||
__table__ = "ports" | ||
__connection__ = "dev" | ||
__fillable__ = ["port_id", "name", "port_country_id"] | ||
|
||
|
||
class Country(Model): | ||
__table__ = "countries" | ||
__connection__ = "dev" | ||
__fillable__ = ["country_id", "name"] | ||
|
||
|
||
class IncomingShipment(Model): | ||
__table__ = "incoming_shipments" | ||
__connection__ = "dev" | ||
__fillable__ = ["shipment_id", "name", "from_port_id"] | ||
|
||
@has_one_through(None, "from_port_id", "port_country_id", "port_id", "country_id") | ||
def from_country(self): | ||
return [Country, Port] | ||
|
||
|
||
class TestRelationships(unittest.TestCase): | ||
def setUp(self): | ||
self.schema = Schema( | ||
connection="dev", | ||
connection_details=DATABASES, | ||
platform=SQLitePlatform, | ||
).on("dev") | ||
|
||
with self.schema.create_table_if_not_exists("incoming_shipments") as table: | ||
table.integer("shipment_id").primary() | ||
table.string("name") | ||
table.integer("from_port_id") | ||
|
||
with self.schema.create_table_if_not_exists("ports") as table: | ||
table.integer("port_id").primary() | ||
table.string("name") | ||
table.integer("port_country_id") | ||
|
||
with self.schema.create_table_if_not_exists("countries") as table: | ||
table.integer("country_id").primary() | ||
table.string("name") | ||
|
||
if not Country.count(): | ||
Country.builder.new().bulk_create( | ||
[ | ||
{"country_id": 10, "name": "Australia"}, | ||
{"country_id": 20, "name": "USA"}, | ||
{"country_id": 30, "name": "Canada"}, | ||
{"country_id": 40, "name": "United Kingdom"}, | ||
] | ||
) | ||
|
||
if not Port.count(): | ||
Port.builder.new().bulk_create( | ||
[ | ||
{"port_id": 100, "name": "Melbourne", "port_country_id": 10}, | ||
{"port_id": 200, "name": "Darwin", "port_country_id": 10}, | ||
{"port_id": 300, "name": "South Louisiana", "port_country_id": 20}, | ||
{"port_id": 400, "name": "Houston", "port_country_id": 20}, | ||
{"port_id": 500, "name": "Montreal", "port_country_id": 30}, | ||
{"port_id": 600, "name": "Vancouver", "port_country_id": 30}, | ||
{"port_id": 700, "name": "Southampton", "port_country_id": 40}, | ||
{"port_id": 800, "name": "London Gateway", "port_country_id": 40}, | ||
] | ||
) | ||
|
||
if not IncomingShipment.count(): | ||
IncomingShipment.builder.new().bulk_create( | ||
[ | ||
{"name": "Bread", "from_port_id": 300}, | ||
{"name": "Milk", "from_port_id": 100}, | ||
{"name": "Tractor Parts", "from_port_id": 100}, | ||
{"name": "Fridges", "from_port_id": 700}, | ||
{"name": "Wheat", "from_port_id": 600}, | ||
{"name": "Kettles", "from_port_id": 400}, | ||
{"name": "Bread", "from_port_id": 700}, | ||
] | ||
) | ||
|
||
def test_has_one_through_can_eager_load(self): | ||
shipments = IncomingShipment.where("name", "Bread").with_("from_country").get() | ||
self.assertEqual(shipments.count(), 2) | ||
|
||
shipment1 = shipments.shift() | ||
self.assertIsInstance(shipment1.from_country, Country) | ||
self.assertEqual(shipment1.from_country.country_id, 20) | ||
|
||
shipment2 = shipments.shift() | ||
self.assertIsInstance(shipment2.from_country, Country) | ||
self.assertEqual(shipment2.from_country.country_id, 40) | ||
|
||
def test_has_one_through_eager_load_can_be_empty(self): | ||
shipments = ( | ||
IncomingShipment.where("name", "Bread") | ||
.with_( | ||
"from_country", | ||
) | ||
.get() | ||
) | ||
self.assertEqual(shipments.count(), 2) | ||
|
||
def test_has_one_through_can_get_related(self): | ||
shipment = IncomingShipment.where("name", "Milk").first() | ||
self.assertIsInstance(shipment.from_country, Country) | ||
self.assertEqual(shipment.from_country.country_id, 10) |