From: Daniele Varrazzo Date: Wed, 20 Apr 2022 11:26:37 +0000 (+0200) Subject: test(enum): add tests to verify the behaviour of unregistered enums X-Git-Tag: 3.1~137^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d922033b9fa23d7d89fb1eb08e50d7edfc4685fc;p=thirdparty%2Fpsycopg.git test(enum): add tests to verify the behaviour of unregistered enums --- diff --git a/tests/types/test_enum.py b/tests/types/test_enum.py index 7301ce0f9..dac41c4fe 100644 --- a/tests/types/test_enum.py +++ b/tests/types/test_enum.py @@ -4,6 +4,7 @@ import pytest from psycopg import pq, sql from psycopg.adapt import PyFormat +from psycopg.types import TypeInfo from psycopg.types.enum import EnumInfo, register_enum @@ -134,16 +135,34 @@ def test_enum_dumper_sqlascii(conn, testenum, fmt_in, fmt_out): @pytest.mark.parametrize("encoding", encodings) @pytest.mark.parametrize("fmt_in", PyFormat) @pytest.mark.parametrize("fmt_out", pq.Format) -def test_generic_enum_loader(conn, testenum, encoding, fmt_in, fmt_out): +def test_generic_enum_dumper(conn, testenum, encoding, fmt_in, fmt_out): conn.execute(f"set client_encoding to {encoding}") name, enum, labels = testenum - info = EnumInfo.fetch(conn, name) - register_enum(info, None, conn) + for item in enum: + if enum is PureTestEnum: + want = item.name + else: + want = item.value + + cur = conn.execute(f"select %{fmt_in}", [item], binary=fmt_out) + assert cur.fetchone()[0] == want + + +@pytest.mark.parametrize("encoding", encodings) +@pytest.mark.parametrize("fmt_in", PyFormat) +@pytest.mark.parametrize("fmt_out", pq.Format) +def test_generic_enum_loader(conn, testenum, encoding, fmt_in, fmt_out): + conn.execute(f"set client_encoding to {encoding}") + + name, enum, labels = testenum for label in labels: cur = conn.execute(f"select %{fmt_in}::{name}", [label], binary=fmt_out) - assert cur.fetchone()[0] == info.enum(label) + want = enum[label].name + if fmt_out == pq.Format.BINARY: + want = want.encode(encoding) + assert cur.fetchone()[0] == want @pytest.mark.parametrize("encoding", encodings) @@ -179,11 +198,13 @@ def test_generic_enum_array_loader(conn, testenum, encoding, fmt_in, fmt_out): conn.execute(f"set client_encoding to {encoding}") name, enum, labels = testenum - info = EnumInfo.fetch(conn, name) - register_enum(info, enum, conn) - + info = TypeInfo.fetch(conn, name) + info.register(conn) + want = [member.name for member in enum] + if fmt_out == pq.Format.BINARY: + want = [item.encode(encoding) for item in want] cur = conn.execute(f"select %{fmt_in}::{name}[]", [labels], binary=fmt_out) - assert cur.fetchone()[0] == list(info.enum) + assert cur.fetchone()[0] == want @pytest.mark.asyncio