CodeSampleX

示例

sqlalchemy 2.0.51: Connection

已验证示例 — pypi sqlalchemy 2.0.51: Connection. contract 在 python 3.12 · linux debian/x64 · docker 上运行并通过: Connection executes SQL statements returning Result…

sha256:ab99f1edcf8b8c154c8b3328fe3b12391efa216e7f37522cc28e697a1f805b3c

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 python linux 24 · ubuntu · glibc 2.39 x64 python python pip

验证运行环境

环境 契约 阶段 运行日期
python 3.12 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1python:3.12-slim@sha256:09f7da3bc104…
2026-09-06

案例

HOW
目标
verify sqlalchemy.Connection in pkg:pypi/sqlalchemy@2.0.51
包
符号
  • sqlalchemy.Connection
环境
python
创建时间
2026-09-06T02:57:46Z

契约

  1. Connection executes SQL statements returning Result objects, supporting parameter binding and scalar extraction.
  2. Connection context manager automatically closes the connection upon exiting the block.
  3. Statements executed on Connection trigger autobegin, and calling begin() while autobegin is active raises InvalidRequestError.
  4. Connection.begin_nested() creates a savepoint transaction that can be committed or rolled back independently.
  5. Operations on a closed Connection raise ResourceClosedError.

文件

  • NOTES.md
  • PROMPT.md
  • csx.json
  • requirements.txt
  • spec.json
  • test/contract.py

下载源代码构件 (tar.gz)

源代码

NOTES.md
# sqlalchemy Connection API

This sample pins `sqlalchemy` 2.0.51 and verifies `sqlalchemy.Connection` for database execution and transaction lifecycle management.

The contract verifies:
- Connection executes SQL statements returning Result objects, supporting parameter binding and scalar extraction.
- Connection context manager automatically closes the connection upon exiting the block.
- Statements executed on Connection trigger autobegin, and calling begin() while autobegin is active raises InvalidRequestError.
- Connection.begin_nested() creates a savepoint transaction that can be committed or rolled back independently.
- Operations on a closed Connection raise ResourceClosedError.
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify sqlalchemy.Connection in pkg:pypi/sqlalchemy@2.0.51
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:pypi/sqlalchemy@2.0.51
Demonstrate these symbols/APIs:
  - sqlalchemy.Connection

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:a843d007b6fff36dafaaa76ac7a915fd7ca3ab271e5fc460989ce80c9f312953","contract":["Connection executes SQL statements returning Result objects, supporting parameter binding and scalar extraction.","Connection context manager automatically closes the connection upon exiting the block.","Statements executed on Connection trigger autobegin, and calling begin() while autobegin is active raises InvalidRequestError.","Connection.begin_nested() creates a savepoint transaction that can be committed or rolled back independently.","Operations on a closed Connection raise ResourceClosedError."],"goal":"verify sqlalchemy.Connection in pkg:pypi/sqlalchemy@2.0.51","kind":"HOW","packages":["pkg:pypi/sqlalchemy@2.0.51"],"schemaVersion":1,"symbols":["sqlalchemy.Connection"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"pypi","executionContext":"python","language":"python","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"pip","runtime":"python","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/sqlalchemy@2.0.51"],"schemaVersion":1,"subject":"pkg:pypi/sqlalchemy@2.0.51","symbols":["sqlalchemy.Connection"],"verifierAdapter":"python@1"}
requirements.txt
sqlalchemy==2.0.51
spec.json
{
  "schemaVersion": 1,
  "goal": "verify sqlalchemy.Connection in pkg:pypi/sqlalchemy@2.0.51",
  "kind": "HOW",
  "packages": [
    "pkg:pypi/sqlalchemy@2.0.51"
  ],
  "symbols": [
    "sqlalchemy.Connection"
  ]
}
test/contract.py
import sqlalchemy
from sqlalchemy import Connection, create_engine, text
from sqlalchemy.exc import InvalidRequestError, ResourceClosedError

# 1. Verify Connection creation, type, and context manager auto-close
engine = create_engine("sqlite:///:memory:")
with engine.connect() as conn:
    assert isinstance(conn, Connection), f"Expected Connection instance, got {type(conn)}"
    assert conn.engine is engine
    assert not conn.closed
    assert not conn.in_transaction()
assert conn.closed

# 2. Verify SQL execution, parameter binding, scalar extraction, and autobegin behavior
conn = engine.connect()
assert not conn.in_transaction()

# Executing a statement initiates autobegin
result = conn.execute(text("SELECT :x AS val"), {"x": 42})
assert result.scalar_one() == 42
assert conn.in_transaction()

# Calling begin() while autobegin is active raises InvalidRequestError
try:
    conn.begin()
    assert False, "Expected InvalidRequestError when calling begin() on an active transaction"
except InvalidRequestError:
    pass

# Commit ends the active transaction
conn.commit()
assert not conn.in_transaction()

# 3. Verify table operations, multiple parameter execution, and scalar queries
with conn.begin():
    conn.execute(text("CREATE TABLE items (id INT, label TEXT)"))
    conn.execute(
        text("INSERT INTO items (id, label) VALUES (:id, :label)"),
        [{"id": 1, "label": "item1"}, {"id": 2, "label": "item2"}, {"id": 3, "label": "item3"}],
    )

assert not conn.in_transaction()
count = conn.scalar(text("SELECT COUNT(*) FROM items"))
assert count == 3
labels = list(conn.scalars(text("SELECT label FROM items ORDER BY id")))
assert labels == ["item1", "item2", "item3"]
conn.commit()

# 4. Verify nested transaction (savepoint) rollback and commit
with conn.begin():
    sp = conn.begin_nested()
    conn.execute(text("INSERT INTO items (id, label) VALUES (4, 'item4')"))
    assert conn.scalar(text("SELECT COUNT(*) FROM items")) == 4
    sp.rollback()
    assert conn.scalar(text("SELECT COUNT(*) FROM items")) == 3

# 5. Verify operations on a closed Connection raise ResourceClosedError
conn.close()
assert conn.closed
try:
    conn.execute(text("SELECT 1"))
    assert False, "Expected ResourceClosedError when executing on a closed Connection"
except ResourceClosedError:
    pass

print("CONTRACT PASS: sqlalchemy.Connection verified offline")

原始种子者

匿名