Skip to content

Commit 7eaf7c2

Browse files
committed
Day 3 of pytest
1 parent f7e32e5 commit 7eaf7c2

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

days/10-12-pytest/code/db_object.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import psycopg2
2+
3+
4+
class MyDB:
5+
def __init__(self):
6+
self.conn = None
7+
8+
def __enter__(self):
9+
self.conn = psycopg2.connect("host=localhost dbname=test user=postgres "
10+
"password=docker")
11+
self.conn.autocommit = True
12+
13+
def __exit__(self, *args):
14+
if self.conn:
15+
self.conn.close()
16+
self.conn = None

days/10-12-pytest/code/hello.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

days/10-12-pytest/code/hello_test.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

days/10-12-pytest/code/test_db.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from db_object import MyDB
2+
import psycopg2
3+
4+
import pytest
5+
6+
7+
@pytest.fixture(scope='module')
8+
def cur():
9+
print("creating connection")
10+
db = MyDB()
11+
db.conn = psycopg2.connect("host=localhost dbname=test user=postgres "
12+
"password=docker")
13+
cur = db.conn.cursor()
14+
yield cur
15+
cur.close()
16+
db.conn.close()
17+
print("closing db")
18+
19+
20+
def test_table_count(cur):
21+
cur.execute("SELECT COUNT(*) FROM test_table")
22+
result = cur.fetchone()
23+
24+
assert int(result[0]) == 1
25+
26+
27+
def test_table_value(cur):
28+
cur.execute("SELECT * FROM test_table")
29+
result = cur.fetchone()
30+
31+
assert result == (1, 'a')

0 commit comments

Comments
 (0)