Skip to content

Commit 9e09d9b

Browse files
committed
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
1 parent 7e58e4b commit 9e09d9b

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ key features:
2727

2828
```yaml
2929
steps:
30-
- uses: ikalnytskyi/action-setup-postgres@v2
30+
- uses: ikalnytskyi/action-setup-postgres@v3
3131
```
3232
3333
#### Advanced
3434
3535
```yaml
3636
steps:
37-
- uses: ikalnytskyi/action-setup-postgres@v2
37+
- uses: ikalnytskyi/action-setup-postgres@v3
3838
with:
3939
username: ci
4040
password: sw0rdfish
4141
database: test
42-
port: "34837"
42+
port: 34837
4343
id: postgres
4444

4545
- run: pytest -vv tests/

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ runs:
4242
- name: Setup and start PostgreSQL
4343
run: |
4444
export PGDATA="$RUNNER_TEMP/pgdata"
45-
pg_ctl init
45+
pg_ctl init --options="--encoding=UTF-8 --locale=en_US.UTF-8"
4646
4747
# Forbid creating unix sockets since they are created by default in the
4848
# directory we don't have permissions to.

test_action.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ def test_connection_uri():
2525
assert connection_uri == expected_connection_uri
2626

2727

28+
def test_server_encoding(connection: psycopg.Connection):
29+
"""Test that PostgreSQL's encoding is 'UTF-8'."""
30+
31+
assert connection.execute("SHOW SERVER_ENCODING").fetchone()[0] == "UTF8"
32+
33+
34+
def test_locale(connection: psycopg.Connection):
35+
"""Test that PostgreSQL's locale is 'en_US.UTF-8'."""
36+
37+
assert connection.execute("SHOW LC_COLLATE").fetchone()[0] == "en_US.UTF-8"
38+
assert connection.execute("SHOW LC_CTYPE").fetchone()[0] == "en_US.UTF-8"
39+
40+
2841
def test_user_permissions(connection: psycopg.Connection):
2942
"""Test that a user can create databases but is not a superuser."""
3043

@@ -53,6 +66,21 @@ def test_user_create_insert_select(connection: psycopg.Connection):
5366
assert records == [(1, "42")]
5467

5568

69+
def test_user_create_insert_non_ascii(connection: psycopg.Connection):
70+
"""Test that non-ASCII characters can be stored and fetched."""
71+
72+
table_name = "test_setup_postgres"
73+
74+
with connection, connection.transaction(force_rollback=True):
75+
records = connection \
76+
.execute(f"CREATE TABLE {table_name}(eggs INTEGER, rice VARCHAR)") \
77+
.execute(f"INSERT INTO {table_name}(eggs, rice) VALUES (1, 'Україна')") \
78+
.execute(f"INSERT INTO {table_name}(eggs, rice) VALUES (2, 'ウクライナ')") \
79+
.execute(f"SELECT * FROM {table_name}") \
80+
.fetchall()
81+
assert records == [(1, "Україна"), (2, "ウクライナ")]
82+
83+
5684
def test_user_create_drop_database(connection: psycopg.Connection):
5785
"""Test that a user has no permissions to create databases."""
5886

0 commit comments

Comments
 (0)