]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add tests
authorCarlos Sousa <edu-eduardo99@hotmail.com>
Wed, 20 Sep 2023 23:51:31 +0000 (20:51 -0300)
committerCarlos Sousa <edu-eduardo99@hotmail.com>
Wed, 20 Sep 2023 23:51:31 +0000 (20:51 -0300)
test/ext/asyncio/test_session_py3k.py
test/ext/test_horizontal_shard.py
test/orm/test_query.py
test/orm/test_session.py

index 8fa174eebaaea5f0e3f8ed220f8e86cb03bd1922..b208619f814f075dc9c7097f503a1a187425f30f 100644 (file)
@@ -193,6 +193,25 @@ class AsyncSessionQueryTest(AsyncFixture):
         u3 = await async_session.get(User, 12)
         is_(u3, None)
 
+
+    @async_test
+    async def test_get_one(self, async_session):
+        User = self.classes.User
+
+        u1 = await async_session.get_one(User, 7)
+        u2 = await async_session.get_one(User, 10)
+        u3 = await async_session.get_one(User, 7)
+
+        is_(u1, u3)
+        eq_(u1.name, "jack")
+        eq_(u2.name, "chuck")
+
+        with testing.expect_raises_message(
+                exc.NoResultFound,
+                "No row was found when one was required",
+        ):
+            await async_session.get_one(User, 12)
+
     @async_test
     async def test_force_a_lazyload(self, async_session):
         """test for #9298"""
index 389dbe00a0894155ef33b46b20a8286941391f89..70400e366aadf3afe1bdf72a2b0821b8d7dc3d42 100644 (file)
@@ -196,7 +196,7 @@ class ShardTest:
         toronto = WeatherLocation("North America", "Toronto")
         london = WeatherLocation("Europe", "London")
         dublin = WeatherLocation("Europe", "Dublin")
-        brasilia = WeatherLocation("South America", "Brasila")
+        brasilia = WeatherLocation("South America", "Brasilia")
         quito = WeatherLocation("South America", "Quito")
         tokyo.reports.append(Report(80.0, id_=1))
         newyork.reports.append(Report(75, id_=1))
@@ -226,6 +226,16 @@ class ShardTest:
         t2 = sess.get(WeatherLocation, 1)
         is_(t2, tokyo)
 
+    def test_get_one(self):
+        sess = self._fixture_data()
+        brasilia = sess.get_one(WeatherLocation, 6)
+        eq_(brasilia.id, 6)
+        eq_(brasilia.city, "Brasilia")
+
+        toronto = sess.get_one(WeatherLocation, 3)
+        eq_(toronto.id, 3)
+        eq_(toronto.city, "Toronto")
+
     def test_get_explicit_shard(self):
         sess = self._fixture_data()
         tokyo = (
index ce5c64a43af6b52a98b6a89cb67364e006cfdd97..42a74d364a5e48ed8f5e2a6631a641e128d748b9 100644 (file)
@@ -1225,6 +1225,17 @@ class GetTest(QueryTest):
         u2 = s.get(User, 7)
         assert u is not u2
 
+    def test_get_one(self):
+        User = self.classes.User
+
+        s = fixture_session()
+        u = s.get_one(User, 7)
+        u2 = s.get_one(User, 7)
+        assert u is u2
+        s.expunge_all()
+        u2 = s.get_one(User, 7)
+        assert u is not u2
+
     def test_get_synonym_direct_name(self, decl_base):
         """test #8753"""
 
index b304ac574540085e2f5837fbfc35657fe617b5c9..f04e0701988e013e1a81f36c25e030e3d8d9ef63 100644 (file)
@@ -609,6 +609,24 @@ class SessionUtilTest(_fixtures.FixtureTest):
 
         is_true(called)
 
+    def test_get_one_no_result(self):
+        users, User = self.tables.users, self.classes.User
+        self.mapper_registry.map_imperatively(User, users)
+
+        sess = fixture_session()
+        user1 = User(id=1, name="u1")
+
+        sess.add(user1)
+        sess.commit()
+
+        u1 = sess.get_one(User, user1.id)
+        assert user1.name == u1.name
+
+        with expect_raises_message(
+            sa.exc.NoResultFound, "No row was found when one was required"
+        ):
+            sess.get_one(User, 2)
+
 
 class SessionStateTest(_fixtures.FixtureTest):
     run_inserts = None
@@ -1928,7 +1946,14 @@ class SessionInterface(fixtures.MappedTest):
     def _public_session_methods(self):
         Session = sa.orm.session.Session
 
-        blocklist = {"begin", "query", "bind_mapper", "get", "bind_table"}
+        blocklist = {
+            "begin",
+            "query",
+            "bind_mapper",
+            "get",
+            "get_one",
+            "bind_table",
+        }
         specials = {"__iter__", "__contains__"}
         ok = set()
         for name in dir(Session):