Add a test for query key sorting in URL.render_as_string()

Aperta Adatta ai principianti
#13,784 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, sqlalchemy
Ambito
databases, testing

Direzione di ricerca

Leggi lib/sqlalchemy/engine/url.py e i test URLTest in test/engine/test_parseconnect.py. Aggiungi un caso con le stesse chiavi di query in ordini diversi, come i due URL nell’issue, e verifica che gli URL uguali abbiano lo stesso hash. Esegui python -m pytest test/engine/test_parseconnect.py; il lavoro è completato quando il test di regressione passa e fallirebbe se venisse rimossa l’ordinazione delle chiavi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

requires triage
Describe the bug

There is no test to check if keys.sort() works in URL.render_as_string() (lib/sqlalchemy/engine/url.py).
The function itself is working correctly, so this is a suggestion to add a test to ensure the sort is working.

Sorting is necessary because __hash__() uses str(self). For example, without sorting, make_url("d://h/db?a=1&b=2") and make_url("d://h/db?b=2&a=1") are equal because __eq__() compares the query as a dict, but they have different hashes. So they would be treated as different keys in a dict or set.

All query strings in test/engine/test_parseconnect.py are already in alphabetical order, so the tests pass even if the sort is removed. (The full test suite also passes with the sort removed.)

I would be happy to send a PR with a test in URLTest if this is labeled "open for pull requests".

Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected

No response

SQLAlchemy Version in Use

2.1.9 (main branch, commit 4e974a083)

DBAPI (i.e. the database driver)

pysqlite (Python’s built-in sqlite3), but no database connection is needed for this issue

Database Vendor and Major Version

SQLite (used to run the test suite; not related to this issue)

Python Version

3.12.4

Operating system

macOS 26.3.1

To Reproduce
from sqlalchemy.engine.url import make_url

a = make_url("d://h/db?a=1&b=2")
b = make_url("d://h/db?b=2&a=1")
print("equal:", a == b, "/ same hash:", hash(a) == hash(b), "/ set size:", len({a, b}))

# 1. With the current code:
#    equal: True / same hash: True / set size: 1
#
# 2. Remove `keys.sort()` in URL.render_as_string() and run again:
#    equal: True / same hash: False / set size: 2
#
# 3. With `keys.sort()` removed, the tests still pass:
#    python -m pytest test/engine/test_parseconnect.py  -> all passed
#    python -m pytest test/ -n auto                    -> 0 failed
Error

No error is raised. The problem is that the tests still pass when keys.sort() is removed.

Additional context

No response

Lingua principale
Python
Stelle
12.3k
Fork
1.8k
Merge medio
2g 10h
PR unite (30g)
9

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di sqlalchemy/sqlalchemy

Tutte le issue di sqlalchemy/sqlalchemy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.