From 9e09d9b1591e1013eed797dd2379fc7f80f91fe2 Mon Sep 17 00:00:00 2001 From: Ihor Kalnytskyi Date: Sat, 2 Jul 2022 12:46:11 +0300 Subject: [PATCH] Use encoding="UTF-8" and locale="en_US.UTF-8" If not explicitly specified, PostgreSQL infers both locale and encoding from the locale settings upon database initialization. Speaking about GitHub runners, they all have different locale settings. For instance, Windows runner uses `CP1252` encoding which renders the database unable to deal with non-latin characters. This patch enforces encoding="UTF-8" and locale="en_US.UTF-8" on all supported platforms in order to ensure that the database behaves the same way in certain edge cases. Fixes #3 --- README.md | 6 +++--- action.yml | 2 +- test_action.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5c12c1c..8350a38 100644 --- a/README.md +++ b/README.md @@ -27,19 +27,19 @@ key features: ```yaml steps: - - uses: ikalnytskyi/action-setup-postgres@v2 + - uses: ikalnytskyi/action-setup-postgres@v3 ``` #### Advanced ```yaml steps: - - uses: ikalnytskyi/action-setup-postgres@v2 + - uses: ikalnytskyi/action-setup-postgres@v3 with: username: ci password: sw0rdfish database: test - port: "34837" + port: 34837 id: postgres - run: pytest -vv tests/ diff --git a/action.yml b/action.yml index f8f0076..dbd3ead 100644 --- a/action.yml +++ b/action.yml @@ -42,7 +42,7 @@ runs: - name: Setup and start PostgreSQL run: | export PGDATA="$RUNNER_TEMP/pgdata" - pg_ctl init + pg_ctl init --options="--encoding=UTF-8 --locale=en_US.UTF-8" # Forbid creating unix sockets since they are created by default in the # directory we don't have permissions to. diff --git a/test_action.py b/test_action.py index 4f468a8..057ba24 100644 --- a/test_action.py +++ b/test_action.py @@ -25,6 +25,19 @@ def test_connection_uri(): assert connection_uri == expected_connection_uri +def test_server_encoding(connection: psycopg.Connection): + """Test that PostgreSQL's encoding is 'UTF-8'.""" + + assert connection.execute("SHOW SERVER_ENCODING").fetchone()[0] == "UTF8" + + +def test_locale(connection: psycopg.Connection): + """Test that PostgreSQL's locale is 'en_US.UTF-8'.""" + + assert connection.execute("SHOW LC_COLLATE").fetchone()[0] == "en_US.UTF-8" + assert connection.execute("SHOW LC_CTYPE").fetchone()[0] == "en_US.UTF-8" + + def test_user_permissions(connection: psycopg.Connection): """Test that a user can create databases but is not a superuser.""" @@ -53,6 +66,21 @@ def test_user_create_insert_select(connection: psycopg.Connection): assert records == [(1, "42")] +def test_user_create_insert_non_ascii(connection: psycopg.Connection): + """Test that non-ASCII characters can be stored and fetched.""" + + table_name = "test_setup_postgres" + + with connection, connection.transaction(force_rollback=True): + records = connection \ + .execute(f"CREATE TABLE {table_name}(eggs INTEGER, rice VARCHAR)") \ + .execute(f"INSERT INTO {table_name}(eggs, rice) VALUES (1, 'Україна')") \ + .execute(f"INSERT INTO {table_name}(eggs, rice) VALUES (2, 'ウクライナ')") \ + .execute(f"SELECT * FROM {table_name}") \ + .fetchall() + assert records == [(1, "Україна"), (2, "ウクライナ")] + + def test_user_create_drop_database(connection: psycopg.Connection): """Test that a user has no permissions to create databases."""