Skip to content

Commit 47dd71e

Browse files
author
Peng Ren
committed
Fixed test case issue
1 parent 3d6ef7e commit 47dd71e

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ Parameters are substituted into the MongoDB filter during execution, providing p
216216

217217
### INSERT Statements
218218

219-
PyMongoSQL supports inserting documents into MongoDB collections using PartiQL-style object and bag literals.
219+
PyMongoSQL supports inserting documents into MongoDB collections using both PartiQL-style object literals and standard SQL INSERT VALUES syntax.
220+
221+
#### PartiQL-Style Object Literals
220222

221223
**Single Document**
222224

@@ -239,12 +241,44 @@ cursor.execute(
239241
```python
240242
# Positional parameters using ? placeholders
241243
cursor.execute(
242-
"INSERT INTO Music {'title': ?, 'artist': ?, 'year': ?}",
244+
"INSERT INTO Music {'title': '?', 'artist': '?', 'year': '?'}",
243245
["Song D", "Diana", 2020]
244246
)
245247
```
246248

247-
> **Note**: For parameterized INSERT, use positional parameters (`?`). Named placeholders (`:name`) are supported for SELECT, UPDATE, and DELETE queries.
249+
#### Standard SQL INSERT VALUES
250+
251+
**Single Row with Column List**
252+
253+
```python
254+
cursor.execute(
255+
"INSERT INTO Music (title, artist, year) VALUES ('Song E', 'Eve', 2022)"
256+
)
257+
```
258+
259+
**Multiple Rows**
260+
261+
```python
262+
cursor.execute(
263+
"INSERT INTO Music (title, artist, year) VALUES ('Song F', 'Frank', 2023), ('Song G', 'Grace', 2024)"
264+
)
265+
```
266+
267+
**Parameterized INSERT VALUES**
268+
269+
```python
270+
# Positional parameters (?)
271+
cursor.execute(
272+
"INSERT INTO Music (title, artist, year) VALUES (?, ?, ?)",
273+
["Song H", "Henry", 2025]
274+
)
275+
276+
# Named parameters (:name)
277+
cursor.execute(
278+
"INSERT INTO Music (title, artist) VALUES (:title, :artist)",
279+
{"title": "Song I", "artist": "Iris"}
280+
)
281+
```
248282

249283
### UPDATE Statements
250284

tests/conftest.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
from pymongosql.connection import Connection
77

8-
# Silence SQLAlchemy 1.4 deprecation warnings in tests
9-
os.environ.setdefault("SQLALCHEMY_SILENCE_UBER_WARNING", "1")
10-
118
# Centralized test configuration sourced from environment to allow running tests
129
# against remote MongoDB (e.g. Atlas) or local test instance.
1310
TEST_URI = os.environ.get("PYMONGOSQL_TEST_URI") or os.environ.get("MONGODB_URI")

tests/test_sqlalchemy_dml.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,16 @@ class TestSQLAlchemyDML:
4343

4444
def test_insert_single_row_explicit_columns(self, sqlalchemy_engine, conn):
4545
"""Test INSERT with single row and explicit columns via SQLAlchemy."""
46-
with sqlalchemy_engine.connect() as connection:
46+
with sqlalchemy_engine.begin() as connection:
4747
# Clean up test data first
4848
try:
4949
connection.execute(text("DELETE FROM test_insert_values"))
50-
connection.commit()
5150
except Exception:
5251
pass
5352

5453
# Insert single row with VALUES clause
5554
sql = "INSERT INTO test_insert_values (id, name, age) VALUES (1, 'Alice', 30)"
5655
connection.execute(text(sql))
57-
connection.commit()
5856

5957
# Verify the insert
6058
result = connection.execute(text("SELECT id, name, age FROM test_insert_values WHERE id = 1"))
@@ -72,22 +70,19 @@ def test_insert_single_row_explicit_columns(self, sqlalchemy_engine, conn):
7270

7371
# Clean up
7472
connection.execute(text("DELETE FROM test_insert_values"))
75-
connection.commit()
7673

7774
def test_insert_multiple_rows_explicit_columns(self, sqlalchemy_engine, conn):
7875
"""Test INSERT with multiple rows and explicit columns via SQLAlchemy."""
79-
with sqlalchemy_engine.connect() as connection:
76+
with sqlalchemy_engine.begin() as connection:
8077
# Clean up test data first
8178
try:
8279
connection.execute(text("DELETE FROM test_insert_values"))
83-
connection.commit()
8480
except Exception:
8581
pass
8682

8783
# Insert multiple rows with VALUES clause
8884
sql = "INSERT INTO test_insert_values (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25)"
8985
connection.execute(text(sql))
90-
connection.commit()
9186

9287
# Verify the inserts
9388
result = connection.execute(text("SELECT id, name, age FROM test_insert_values ORDER BY id"))
@@ -113,22 +108,19 @@ def test_insert_multiple_rows_explicit_columns(self, sqlalchemy_engine, conn):
113108

114109
# Clean up
115110
connection.execute(text("DELETE FROM test_insert_values"))
116-
connection.commit()
117111

118112
def test_insert_single_row_implicit_columns(self, sqlalchemy_engine, conn):
119113
"""Test INSERT with single row without column list via SQLAlchemy."""
120-
with sqlalchemy_engine.connect() as connection:
114+
with sqlalchemy_engine.begin() as connection:
121115
# Clean up test data first
122116
try:
123117
connection.execute(text("DELETE FROM test_insert_implicit"))
124-
connection.commit()
125118
except Exception:
126119
pass
127120

128121
# Insert single row with VALUES clause (implicit columns)
129122
sql = "INSERT INTO test_insert_implicit VALUES (1, 'Alice', 30)"
130123
connection.execute(text(sql))
131-
connection.commit()
132124

133125
# Verify the insert (auto-named columns: col0, col1, col2)
134126
result = connection.execute(text("SELECT col0, col1, col2 FROM test_insert_implicit WHERE col0 = 1"))
@@ -146,22 +138,19 @@ def test_insert_single_row_implicit_columns(self, sqlalchemy_engine, conn):
146138

147139
# Clean up
148140
connection.execute(text("DELETE FROM test_insert_implicit"))
149-
connection.commit()
150141

151142
def test_insert_with_null_values(self, sqlalchemy_engine, conn):
152143
"""Test INSERT with NULL values via SQLAlchemy."""
153-
with sqlalchemy_engine.connect() as connection:
144+
with sqlalchemy_engine.begin() as connection:
154145
# Clean up test data first
155146
try:
156147
connection.execute(text("DELETE FROM test_insert_null"))
157-
connection.commit()
158148
except Exception:
159149
pass
160150

161151
# Insert with NULL value
162152
sql = "INSERT INTO test_insert_null (id, name, email) VALUES (1, 'Alice', NULL)"
163153
connection.execute(text(sql))
164-
connection.commit()
165154

166155
# Verify the insert
167156
result = connection.execute(text("SELECT id, name, email FROM test_insert_null WHERE id = 1"))
@@ -179,22 +168,19 @@ def test_insert_with_null_values(self, sqlalchemy_engine, conn):
179168

180169
# Clean up
181170
connection.execute(text("DELETE FROM test_insert_null"))
182-
connection.commit()
183171

184172
def test_insert_with_boolean_values(self, sqlalchemy_engine, conn):
185173
"""Test INSERT with boolean values via SQLAlchemy."""
186-
with sqlalchemy_engine.connect() as connection:
174+
with sqlalchemy_engine.begin() as connection:
187175
# Clean up test data first
188176
try:
189177
connection.execute(text("DELETE FROM test_insert_bool"))
190-
connection.commit()
191178
except Exception:
192179
pass
193180

194181
# Insert with boolean values
195182
sql = "INSERT INTO test_insert_bool (id, is_active, is_deleted) VALUES (1, TRUE, FALSE)"
196183
connection.execute(text(sql))
197-
connection.commit()
198184

199185
# Verify the insert
200186
result = connection.execute(text("SELECT id, is_active, is_deleted FROM test_insert_bool WHERE id = 1"))
@@ -212,7 +198,6 @@ def test_insert_with_boolean_values(self, sqlalchemy_engine, conn):
212198

213199
# Clean up
214200
connection.execute(text("DELETE FROM test_insert_bool"))
215-
connection.commit()
216201

217202
def test_orm_table_insert_single_row(self, session_maker, conn):
218203
"""Test INSERT VALUES with ORM table using raw SQL."""

0 commit comments

Comments
 (0)