summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2020-05-24 15:51:24 +0000
committerGerrit Code Review <gerrit@bbpush.zzzcomputing.com>2020-05-24 15:51:24 +0000
commit1502b5b3e4e4b93021eb927a6623f288ef006ba6 (patch)
tree48980e3f698b8113474b9da2f1c831b60e6d89cd /test
parent1d12bc1381c7ff6748eb63754405019f0ded29e4 (diff)
parentb46978dc63788152b170c64ae6ddd876a024c5df (diff)
downloadsqlalchemy-1502b5b3e4e4b93021eb927a6623f288ef006ba6.tar.gz
Merge "inline one_or_none"
Diffstat (limited to 'test')
-rw-r--r--test/aaa_profiling/test_resultset.py29
-rw-r--r--test/base/test_result.py31
-rw-r--r--test/profiles.txt24
3 files changed, 84 insertions, 0 deletions
diff --git a/test/aaa_profiling/test_resultset.py b/test/aaa_profiling/test_resultset.py
index 0fdc2b498..b22676ad7 100644
--- a/test/aaa_profiling/test_resultset.py
+++ b/test/aaa_profiling/test_resultset.py
@@ -120,6 +120,35 @@ class ResultSetTest(fixtures.TestBase, AssertsExecutionResults):
for row in conn.execute(t.select()).mappings().fetchall():
[row["field%d" % fnum] for fnum in range(NUM_FIELDS)]
+ @testing.combinations(
+ (False, 0), (True, 1), (False, 1), (False, 2),
+ )
+ def test_one_or_none(self, one_or_first, rows_present):
+ # TODO: this is not testing the ORM level "scalar_mapping"
+ # mode which has a different performance profile
+ with testing.db.connect() as conn:
+ stmt = t.select()
+ if rows_present == 0:
+ stmt = stmt.where(1 == 0)
+ elif rows_present == 1:
+ stmt = stmt.limit(1)
+
+ result = conn.execute(stmt)
+
+ @profiling.function_call_count()
+ def go():
+ if one_or_first:
+ result.one()
+ else:
+ result.first()
+
+ try:
+ go()
+ finally:
+ # hmmmm, connection close context manager does not
+ # seem to be handling this for a profile that skips
+ result.close()
+
def test_contains_doesnt_compile(self):
row = t.select().execute().first()
c1 = Column("some column", Integer) + Column(
diff --git a/test/base/test_result.py b/test/base/test_result.py
index 7628318a5..b179c3462 100644
--- a/test/base/test_result.py
+++ b/test/base/test_result.py
@@ -399,6 +399,37 @@ class ResultTest(fixtures.TestBase):
eq_(result.all(), [])
+ def test_one_unique(self):
+ # assert that one() counts rows after uniquness has been applied.
+ # this would raise if we didnt have unique
+ result = self._fixture(data=[(1, 1, 1), (1, 1, 1)])
+
+ row = result.unique().one()
+ eq_(row, (1, 1, 1))
+
+ def test_one_unique_tricky_one(self):
+ # one() needs to keep consuming rows in order to find a non-unique
+ # one. unique() really slows things down
+ result = self._fixture(
+ data=[(1, 1, 1), (1, 1, 1), (1, 1, 1), (2, 1, 1)]
+ )
+
+ assert_raises(exc.MultipleResultsFound, result.unique().one)
+
+ def test_one_unique_mapping(self):
+ # assert that one() counts rows after uniquness has been applied.
+ # this would raise if we didnt have unique
+ result = self._fixture(data=[(1, 1, 1), (1, 1, 1)])
+
+ row = result.mappings().unique().one()
+ eq_(row, {"a": 1, "b": 1, "c": 1})
+
+ def test_one_mapping(self):
+ result = self._fixture(num_rows=1)
+
+ row = result.mappings().one()
+ eq_(row, {"a": 1, "b": 1, "c": 1})
+
def test_one(self):
result = self._fixture(num_rows=1)
diff --git a/test/profiles.txt b/test/profiles.txt
index 1831ee9e4..24c4294e3 100644
--- a/test/profiles.txt
+++ b/test/profiles.txt
@@ -533,6 +533,30 @@ test.aaa_profiling.test_resultset.ResultSetTest.test_fetch_by_key_mappings 3.8_p
test.aaa_profiling.test_resultset.ResultSetTest.test_fetch_by_key_mappings 3.8_sqlite_pysqlite_dbapiunicode_cextensions 2469
test.aaa_profiling.test_resultset.ResultSetTest.test_fetch_by_key_mappings 3.8_sqlite_pysqlite_dbapiunicode_nocextensions 15476
+# TEST: test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-0]
+
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-0] 2.7_sqlite_pysqlite_dbapiunicode_nocextensions 14
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-0] 3.8_sqlite_pysqlite_dbapiunicode_cextensions 15
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-0] 3.8_sqlite_pysqlite_dbapiunicode_nocextensions 15
+
+# TEST: test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-1]
+
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-1] 2.7_sqlite_pysqlite_dbapiunicode_nocextensions 17
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-1] 3.8_sqlite_pysqlite_dbapiunicode_cextensions 16
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-1] 3.8_sqlite_pysqlite_dbapiunicode_nocextensions 18
+
+# TEST: test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-2]
+
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-2] 2.7_sqlite_pysqlite_dbapiunicode_nocextensions 17
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-2] 3.8_sqlite_pysqlite_dbapiunicode_cextensions 16
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[False-2] 3.8_sqlite_pysqlite_dbapiunicode_nocextensions 18
+
+# TEST: test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[True-1]
+
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[True-1] 2.7_sqlite_pysqlite_dbapiunicode_nocextensions 20
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[True-1] 3.8_sqlite_pysqlite_dbapiunicode_cextensions 19
+test.aaa_profiling.test_resultset.ResultSetTest.test_one_or_none[True-1] 3.8_sqlite_pysqlite_dbapiunicode_nocextensions 21
+
# TEST: test.aaa_profiling.test_resultset.ResultSetTest.test_raw_string
test.aaa_profiling.test_resultset.ResultSetTest.test_raw_string 2.7_mssql_pyodbc_dbapiunicode_cextensions 276