Skip to content

Commit

Permalink
Merge pull request #869 from pmn4/pmn4.base-relationship
Browse files Browse the repository at this point in the history
BaseRelationship: s/self/instance
  • Loading branch information
josephmancuso authored Apr 7, 2024
2 parents 6f93468 + 1a3b0c4 commit c9e7617
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ If you are interested in the project then it would be a great idea to read the "

## Issues

Everything really should start with opening an issue or finding an issue. If you feel you have an idea for how the project can be improved, no matter how small, you should open an issue so we can have an open dicussion with the maintainers of the project.
Everything really should start with opening an issue or finding an issue. If you feel you have an idea for how the project can be improved, no matter how small, you should open an issue so we can have an open discussion with the maintainers of the project.

We can discuss in that issue the solution to the problem or feature you have. If we do not feel it fits within the project then we will close the issue. Feel free to open a new issue if new information comes up.

Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/relationships/BaseRelationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __get__(self, instance, owner):
object -- Either returns a builder or a hydrated model.
"""
attribute = self.fn.__name__
relationship = self.fn(self)()
relationship = self.fn(instance)()
self.set_keys(instance, attribute)
self._related_builder = relationship.builder

Expand Down
28 changes: 28 additions & 0 deletions tests/mssql/builder/test_mssql_query_builder_relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def articles(self):
def profile(self):
return Profile

@belongs_to("id", "parent_dynamic_id")
def parent_dynamic(self):
return self.__class__

@belongs_to("id", "parent_specified_id")
def parent_specified(self):
return User


class BaseTestQueryRelationships(unittest.TestCase):
maxDiff = None
Expand All @@ -64,6 +72,26 @@ def test_has(self):
""")""",
)

def test_has_reference_to_self(self):
builder = self.get_builder()
sql = builder.has("parent_dynamic").to_sql()
self.assertEqual(
sql,
"""SELECT * FROM [users] WHERE EXISTS ("""
"""SELECT * FROM [users] WHERE [users].[parent_dynamic_id] = [users].[id]"""
""")""",
)

def test_has_reference_to_self_using_class(self):
builder = self.get_builder()
sql = builder.has("parent_specified").to_sql()
self.assertEqual(
sql,
"""SELECT * FROM [users] WHERE EXISTS ("""
"""SELECT * FROM [users] WHERE [users].[parent_specified_id] = [users].[id]"""
""")""",
)

def test_where_has_query(self):
builder = self.get_builder()
sql = builder.where_has("articles", lambda q: q.where("active", 1)).to_sql()
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def test_attribute_check_with_hasattr(self):
self.assertFalse(hasattr(Profile(), "__password__"))


if os.getenv("RUN_MYSQL_DATABASE", None).lower() == "true":
if os.getenv("RUN_MYSQL_DATABASE", "false").lower() == "true":

class MysqlTestModel(unittest.TestCase):
# TODO: these tests aren't getting run in CI... is that intentional?
Expand Down

0 comments on commit c9e7617

Please sign in to comment.