Skip to content

Commit

Permalink
Update content
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaprasadreddy committed Dec 5, 2023
1 parent 3a05967 commit c7d2ae3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
4 changes: 2 additions & 2 deletions customers/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
23 changes: 11 additions & 12 deletions guide/getting-started-with-testcontainers-for-python/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
----
Expand All @@ -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.

Expand Down Expand Up @@ -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:

Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/test_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def setup_data():


def test_get_all_customers():
customers.create_customer(customers.Customer(0, "Siva", "[email protected]"))
customers.create_customer(customers.Customer(0, "James", "[email protected]"))
customers.create_customer("Siva", "[email protected]")
customers.create_customer("James", "[email protected]")
customers_list = customers.get_all_customers()
assert len(customers_list) == 2


def test_get_customer_by_email():
customers.create_customer(customers.Customer(0, "John", "[email protected]"))
customers.create_customer("John", "[email protected]")
customer = customers.get_customer_by_email("[email protected]")
assert customer.name == "John"
assert customer.email == "[email protected]"

0 comments on commit c7d2ae3

Please sign in to comment.