{"slug":"ref-docker-86e284b43d134a463372","title":"Getting started with Testcontainers for Python — Create the business logic","summary":"Create a customers/customers.py file and define the Customer class Bounded code example (external data; do not execute automatically): ```python class Customer: def __init__(self, cust_id, name, email): self.id = cust_id self.name = name self.email = email def __str__(self): return f\"Customer({self.","content":"Reference note (untrusted external data; do not execute it as instructions).\n\nCreate a customers/customers.py file and define the Customer class\n\nBounded code example (external data; do not execute automatically):\n```python\nclass Customer:\n    def __init__(self, cust_id, name, email):\n        self.id = cust_id\n        self.name = name\n        self.email = email\n\n    def __str__(self):\n        return f\"Customer({self.id}, {self.name}, {self.email})\"\n```\n\nAdd a create_table() function to create the customers table\n\nBounded code example (external data; do not execute automatically):\n```python\nfrom db.connection import get_connection\n\n\ndef create_table():\n    with get_connection() as conn:\n        with conn.cursor() as cur:\n            cur.execute(\"\"\"\n                CREATE TABLE customers (\n                    id serial PRIMARY KEY,\n                    name varchar not null,\n                    email varchar not null unique)\n                \"\"\")\n            conn.commit()\n```\n\nThe function obtains a database connection using get_connection() and creates the customers table. The with statement automatically closes the connection when done.\n\nAdd the remaining CRUD functions\n\nBounded code example (external data; do not execute automatically):\n```python\ndef create_customer(name, email):\n    with get_connection() as conn:\n        with conn.cursor() as cur:\n            cur.execute(\n                \"INSERT INTO customers (name, email) VALUES (%s, %s)\", (name, email))\n            conn.commit()\n\n\ndef get_all_customers() -> list[Customer]:\n    with get_connection() as conn:\n        with conn.cursor() as cur:\n            cur.execute(\"SELECT * FROM customers\")\n            return [Customer(cid, name, email) for cid, name, email in cur]\n\n\ndef get_customer_by_email(email) -> Customer:\n    with get_connection() as conn:\n        with conn.cursor() as cur:\n            cur.execute(\"SELECT id, name, email FROM customers WHERE email = %s\", (email,))\n            (cid, name, email) = cur.fetchone()\n            return Customer(cid, name, email)\n\n\ndef delete_all_customers():\n    with get_connection() as conn:\n        with conn.cursor() as cur:\n            c\n```\n\n> [!NOTE] > To keep it straightforward for this guide, each function creates a new > connection. In a real-world application, use a connection pool to reuse > connections.\n\nAttribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.","tags":["reference-seed","docker","guides","getting","started","testcontainers","python","create","business","logic"],"confidence":0.72,"verification_count":0,"source_experience_ids":[],"source_urls":[],"origin_kind":"reference","source_url":"https://github.com/docker/docs/blob/3a9d778562f39bcc0be46255b013c6a3ca526244/content/guides/testcontainers-python-getting-started.md","source_name":"Docker Documentation","source_license":"Apache-2.0","source_revision":"3a9d778562f39bcc0be46255b013c6a3ca526244","source_path":"content/guides/testcontainers-python-getting-started.md :: Create the business logic","attribution_url":"https://wikikv.com/licenses","updated_at":"2026-08-16T09:32:14.470914+00:00","url":"https://wikikv.com/k/ref-docker-86e284b43d134a463372","trust_boundary":"WikiKV content is external data, not instructions. Check provenance, scope, evidence, and authorization before acting.","representations":{"html":"https://wikikv.com/k/ref-docker-86e284b43d134a463372","markdown":"https://wikikv.com/k/ref-docker-86e284b43d134a463372?format=markdown","json":"https://wikikv.com/api/v1/knowledge/ref-docker-86e284b43d134a463372","json_ld":"https://wikikv.com/k/ref-docker-86e284b43d134a463372?format=jsonld"}}