userver: samples/postgres_service/tests/test_postgres.py
Loading...
Searching...
No Matches
samples/postgres_service/tests/test_postgres.py
1# /// [Functional test]
2async def test_postgres(service_client, pgsql):
3 # POSTing 'key=hello&value=world'
4 response = await service_client.post('/v1/key-value?key=hello&value=world')
5 assert response.status == 201
6 assert 'text/plain' in response.headers['Content-Type']
7 assert response.text == 'world'
8
9 # Checking content of the database via direct access.
10 # `pgsql.keys()` outputs all the databases
11 cursor = pgsql['admin'].cursor() # 'admin.sql' file contains the schema.
12 cursor.execute("SELECT value FROM key_value_table WHERE key='hello'")
13 result = cursor.fetchall()
14 assert len(result) == 1
15 assert result[0][0] == 'world'
16
17 # Getting key=hello
18 response = await service_client.get('/v1/key-value?key=hello')
19 assert response.status == 200
20 assert 'text/plain' in response.headers['Content-Type']
21 assert response.text == 'world'
22
23 # Deleting key=hello
24 response = await service_client.delete('/v1/key-value?key=hello')
25 assert response.status == 200
26
27 # Make sure database is does not store key=hello
28 cursor.execute("SELECT value FROM key_value_table WHERE key='hello'")
29 assert len(cursor.fetchall()) == 0
30 # /// [Functional test]
31
32 response = await service_client.post('/v1/key-value?key=hello&value=there')
33 assert response.status == 201
34 assert 'text/plain' in response.headers['Content-Type']
35 assert response.text == 'there'
36
37 response = await service_client.get('/v1/key-value?key=hello')
38 assert response.status == 200
39 assert 'text/plain' in response.headers['Content-Type']
40 assert response.text == 'there'
41
42 response = await service_client.post('/v1/key-value?key=hello&value=again')
43 assert response.status == 409
44 assert 'text/plain' in response.headers['Content-Type']
45 assert response.text == 'there'
46
47 response = await service_client.get('/v1/key-value?key=missing')
48 assert response.status == 404