|
1 | 1 | # app.py |
2 | 2 | ```python |
3 | 3 | from flask import Flask, request, jsonify |
4 | | -import db |
| 4 | +from integration_test.db import insert_user |
5 | 5 |
|
6 | | -app = Flask(__name__) |
| 6 | +flask_app = Flask(__name__) |
7 | 7 |
|
8 | | -@app.route("/register", methods=["POST"]) |
| 8 | +@flask_app.route("/register", methods=["POST"]) |
9 | 9 | def register(): |
10 | 10 | data = request.get_json() |
11 | 11 | username = data.get("username") |
12 | 12 | if not username: |
13 | 13 | return jsonify({"error": "Username required"}), 400 |
14 | | - db.insert_user(username) |
| 14 | + insert_user(username) |
15 | 15 | return jsonify({"message": f"User {username} registered"}), 201 |
16 | 16 | ``` |
17 | 17 |
|
@@ -42,27 +42,25 @@ def get_users(): |
42 | 42 | users = [row[0] for row in db_connection_cursor.fetchall()] |
43 | 43 | db_connection.close() |
44 | 44 | return users |
45 | | - |
46 | 45 | ``` |
47 | 46 |
|
48 | 47 | # test_integration.py |
49 | 48 |
|
50 | 49 | ```python |
51 | 50 | import pytest |
52 | | -from integration_test import app |
53 | | -import db |
| 51 | + |
| 52 | +from integration_test.db import (init_db, get_users) |
| 53 | +from integration_test.app import (flask_app) |
54 | 54 |
|
55 | 55 |
|
56 | 56 | @pytest.fixture(scope="module") |
57 | 57 | def client(): |
58 | | - db.init_db() |
59 | | - with app.flask_app.test_client() as client: |
| 58 | + init_db() |
| 59 | + with flask_app.test_client() as client: |
60 | 60 | yield client |
61 | 61 |
|
62 | | - |
63 | 62 | def test_register_user(client): |
64 | 63 | response = client.post("/register", json={"username": "Alice"}) |
65 | 64 | assert response.status_code == 201 |
66 | | - assert "Alice" in db.get_users() |
67 | | - |
| 65 | + assert "Alice" in get_users() |
68 | 66 | ``` |
0 commit comments