From 40b218441d5f53dd84ada48b90ca87a5f49071d2 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Sat, 23 Nov 2024 09:52:18 -0500 Subject: [PATCH] wip --- src/masoniteorm/models/Model.py | 4 +-- tests/playground/test_playground.py | 46 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/playground/test_playground.py diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index 674b1e00..43f905e3 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -1216,11 +1216,11 @@ def __call__(self): this is returned when you do model.relationship() """ print("Calling relationship") - return self.relationship + return self.relationship.apply_query() # def __repr__(self): # return repr(self.relationship) def __iter__(self): print("iterating relationship propery") - return iter(self.relationship) # Use the iterator of the list \ No newline at end of file + return iter(self.relationship) \ No newline at end of file diff --git a/tests/playground/test_playground.py b/tests/playground/test_playground.py new file mode 100644 index 00000000..b9602a12 --- /dev/null +++ b/tests/playground/test_playground.py @@ -0,0 +1,46 @@ +import unittest +from src.masoniteorm.models import Model + +class CreditCard(Model): + __table__ = "company_credit_cards" + __primary_key__ = "credit_card_id" + + +class Company(Model): + __table__ = "tbl_companies" + __primary_key__ = "company_id" + + @property + def cards(self): + """ + Return a RelationshipProperty wrapper for dual behavior. + """ + return self.has_many(CreditCard, "company_id", "company_id") + +class User(Model): + __table__ = "tbl_users" + __primary_key__ = "user_id" + + @property + def company(self): + """ + Return a RelationshipProperty wrapper for dual behavior. + """ + return self.has_one(Company, "company_id", "company_id") + + +class PlaygroundTest(unittest.TestCase): + def test_has_one_can_access_related_model_attribute(self): + user = User.find(667) + + # Access the related company instance as a property + related_company = user.company + self.assertEqual(related_company.company_name, "ROTHCO ACCOUNT") + + def test_has_one_can_build_off_query_builder(self): + user = User.find(667) + + # Access the related company instance as a property + related_company = user.company + # self.assertEqual(related_company.company_name, "ROTHCO ACCOUNT") + self.assertEqual(user.company().to_sql(), "SELECT * FROM `tbl_companies` WHERE `tbl_companies`.`company_id` = '373849'") \ No newline at end of file