Skip to content

Commit

Permalink
updated Model find and create
Browse files Browse the repository at this point in the history
  • Loading branch information
circulon committed Sep 10, 2024
1 parent 1ca31b1 commit 27760ed
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def find(cls, record_id, query=False):
builder = cls().where(cls.get_primary_key(), record_id)

if query:
return builder.to_sql()
return builder
else:
if isinstance(record_id, (list, tuple)):
return builder.get()
Expand Down Expand Up @@ -562,7 +562,7 @@ def create(
if query:
return cls.builder.create(
dictionary, query=True, cast=cast, **kwargs
).to_sql()
)

return cls.builder.create(dictionary, cast=cast, **kwargs)

Expand Down Expand Up @@ -897,7 +897,7 @@ def save(self, query=False):
if self.is_loaded():
result = builder.update(
self.__dirty_attributes__, dry=query, ignore_mass_assignment=True
).to_sql()
)
else:
result = self.create(self.__dirty_attributes__, query=query)

Expand Down
10 changes: 5 additions & 5 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ def test_model_creates_when_new(self):
model = ModelTest.hydrate({"id": 1, "username": "joe", "admin": True})

model.name = "Bill"
sql = model.save(query=True)
sql = model.save(query=True).to_sql()
self.assertTrue(sql.startswith("UPDATE"))

model = ModelTest()

model.name = "Bill"
sql = model.save(query=True)
sql = model.save(query=True).to_sql()
self.assertTrue(sql.startswith("INSERT"))

def test_model_can_cast_attributes(self):
Expand Down Expand Up @@ -170,7 +170,7 @@ def test_model_update_without_changes(self):

model.username = "joe"
model.name = "Bill"
sql = model.save(query=True)
sql = model.save(query=True).to_sql()
self.assertTrue(sql.startswith("UPDATE"))
self.assertNotIn("username", sql)

Expand All @@ -181,7 +181,7 @@ def test_force_update_on_model_class(self):

model.username = "joe"
model.name = "Bill"
sql = model.save(query=True)
sql = model.save(query=True).to_sql()
self.assertTrue(sql.startswith("UPDATE"))
self.assertIn("username", sql)
self.assertIn("name", sql)
Expand All @@ -201,7 +201,7 @@ def test_model_update_without_changes_at_all(self):

model.username = "joe"
model.name = "Joe"
sql = model.save(query=True)
sql = model.save(query=True).to_sql()
self.assertFalse(sql.startswith("UPDATE"))

def test_model_using_or_where(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/mysql/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class TestModel(unittest.TestCase):
def test_create_can_use_fillable(self):
sql = ProfileFillable.create(
{"name": "Joe", "email": "[email protected]"}, query=True
)
).to_sql()

self.assertEqual(
sql, "INSERT INTO `profiles` (`profiles`.`name`) VALUES ('Joe')"
Expand All @@ -90,7 +90,7 @@ def test_create_can_use_fillable(self):
def test_create_can_use_fillable_asterisk(self):
sql = ProfileFillAsterisk.create(
{"name": "Joe", "email": "[email protected]"}, query=True
)
).to_sql()

self.assertEqual(
sql,
Expand All @@ -100,7 +100,7 @@ def test_create_can_use_fillable_asterisk(self):
def test_create_can_use_guarded(self):
sql = ProfileGuarded.create(
{"name": "Joe", "email": "[email protected]"}, query=True
)
).to_sql()

self.assertEqual(
sql, "INSERT INTO `profiles` (`profiles`.`name`) VALUES ('Joe')"
Expand All @@ -109,7 +109,7 @@ def test_create_can_use_guarded(self):
def test_create_can_use_guarded_asterisk(self):
sql = ProfileGuardedAsterisk.create(
{"name": "Joe", "email": "[email protected]"}, query=True
)
).to_sql()

# An asterisk guarded attribute excludes all fields from mass-assignment.
# This would raise a DB error if there are any required fields.
Expand Down Expand Up @@ -326,7 +326,7 @@ def test_can_find_first(self):
def test_can_touch(self):
profile = ProfileFillTimeStamped.hydrate({"name": "Joe", "id": 1})

sql = profile.touch("now", query=True)
sql = profile.touch("now", query=True).to_sql()

self.assertEqual(
sql,
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql/scopes/test_can_use_global_scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_can_use_global_scopes_on_select(self):

def test_can_use_global_scopes_on_time(self):
sql = "INSERT INTO `users` (`users`.`name`, `users`.`updated_at`, `users`.`created_at`) VALUES ('Joe'"
self.assertTrue(User.create({"name": "Joe"}, query=True).startswith(sql))
self.assertTrue(User.create({"name": "Joe"}, query=True).to_sql().startswith(sql))

# def test_can_use_global_scopes_on_inherit(self):
# sql = "SELECT * FROM `user_softs` WHERE `user_softs`.`deleted_at` IS NULL"
Expand Down
4 changes: 2 additions & 2 deletions tests/sqlite/models/test_sqlite_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ def test_update_all_records(self):
self.assertEqual(sql, """UPDATE "users" SET "name" = 'joe'""")

def test_can_find_list(self):
sql = User.find(1, query=True)
sql = User.find(1, query=True).to_sql()

self.assertEqual(sql, """SELECT * FROM "users" WHERE "users"."id" = '1'""")

sql = User.find([1, 2, 3], query=True)
sql = User.find([1, 2, 3], query=True).to_sql()

self.assertEqual(
sql, """SELECT * FROM "users" WHERE "users"."id" IN ('1','2','3')"""
Expand Down

0 comments on commit 27760ed

Please sign in to comment.