diff --git a/customers/customers.py b/customers/customers.py index dfa2768..bf82685 100644 --- a/customers/customers.py +++ b/customers/customers.py @@ -23,11 +23,11 @@ def create_table(): conn.commit() -def create_customer(c: Customer): +def create_customer(name, email): with get_connection() as conn: with conn.cursor() as cur: cur.execute( - "INSERT INTO customers (name, email) VALUES (%s, %s)", (c.name, c.email)) + "INSERT INTO customers (name, email) VALUES (%s, %s)", (name, email)) conn.commit() diff --git a/guide/getting-started-with-testcontainers-for-python/index.adoc b/guide/getting-started-with-testcontainers-for-python/index.adoc index 4064cda..6bdfd1a 100644 --- a/guide/getting-started-with-testcontainers-for-python/index.adoc +++ b/guide/getting-started-with-testcontainers-for-python/index.adoc @@ -77,18 +77,18 @@ Let's create *customers/customers.py* file and create *Customer* class as follow include::{codebase}/customers/customers.py[lines="4..12"] ---- -Now, let's implement *create_table()* function to create *customers* table as follows: +Now, let's add *create_table()* function in *customers/customers.py* file to create *customers* table as follows: [source,python] ---- include::{codebase}/customers/customers.py[lines="1..3,4..24"] ---- -We have obtained a new database connection using *get_connection()* function and created a *customers* table. -We have used Python context manager *with* statement to automatically close the database connection once the table is created. +We have obtained a new database connection using *get_connection()* function and created the *customers* table. +We have used Python context manager *with* statement to automatically close the database connection. -Now, let's implement *create_customer()*, *get_all_customers()*, *get_customer_by_email()*, -and *delete_all_customers()* functions as follows: +Let's implement *create_customer()*, *get_all_customers()*, *get_customer_by_email()*, +and *delete_all_customers()* functions in *customers/customers.py* file as follows: [source,python] ---- @@ -103,9 +103,8 @@ To keep it simple for the purpose of this guide, we are creating a new connectio In a real-world application, it is recommended to use a connection pool to reuse connections. == Write tests using Testcontainers -To test our functions, we will create a Postgres database in a container using Testcontainers once. -Then we will use the same database for all the tests. -Also, we will delete all the customer records before every test to run the tests in a predictable state. +We will create a PostgreSQL database in a container using Testcontainers and use the same database for all the tests. +Also, we will delete all the customer records before every test so that our tests will run with a clean database. We are going to use pytest fixtures for implementing the setup and teardown logic. @@ -138,7 +137,7 @@ include::{codebase}/tests/test_customers.py[lines="28..40"] As we are deleting all the customer records before every test, the tests can be run in any order. == Run tests -To enable the Pytest https://pytest.org/explanation/goodpractices.html#test-discovery[auto-discovery] mechanism, create *__init__.py* file under *tests* directory with empty content. +To enable the Pytest https://pytest.org/explanation/goodpractices.html#test-discovery[auto-discovery] mechanism, create *\_\_init\_\_.py* file under *tests* directory with empty content. Now let's run the tests using pytest as follows: @@ -152,14 +151,14 @@ You should see the following output: [source,shell] ---- pytest -=============================== test session starts =============================== +=========== test session starts ============== platform darwin -- Python 3.12.0, pytest-7.4.3, pluggy-1.3.0 rootdir: /Users/siva/dev/tc-python-demo collected 2 items -tests/test_customers.py .. [100%] +tests/test_customers.py .. [100%] -================================ 2 passed in 3.02s ================================ +========= 2 passed in 3.02s ================= ---- == Conclusion diff --git a/tests/test_customers.py b/tests/test_customers.py index 2da3cc1..5c2e6e2 100644 --- a/tests/test_customers.py +++ b/tests/test_customers.py @@ -26,14 +26,14 @@ def setup_data(): def test_get_all_customers(): - customers.create_customer(customers.Customer(0, "Siva", "siva@gmail.com")) - customers.create_customer(customers.Customer(0, "James", "james@gmail.com")) + customers.create_customer("Siva", "siva@gmail.com") + customers.create_customer("James", "james@gmail.com") customers_list = customers.get_all_customers() assert len(customers_list) == 2 def test_get_customer_by_email(): - customers.create_customer(customers.Customer(0, "John", "john@gmail.com")) + customers.create_customer("John", "john@gmail.com") customer = customers.get_customer_by_email("john@gmail.com") assert customer.name == "John" assert customer.email == "john@gmail.com"