From 48450702a9c393837ecb1d9ef989ff6ef2d16798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kaczmarek?= <12432719+AN0DA@users.noreply.github.com> Date: Sun, 15 Jun 2025 22:55:35 +0200 Subject: [PATCH] Zadanie 4 - testy --- pyproject.toml | 6 + src/main.py | 16 +++ src/templates/about.html | 6 + src/templates/contact.html | 14 ++ src/templates/index.html | 3 + tests/flask_test_server.py | 14 ++ tests/test_pages.py | 27 ++++ tests/test_selenium_contact.py | 43 +++++++ uv.lock | 229 ++++++++++++++++++++++++++++++++- 9 files changed, 354 insertions(+), 4 deletions(-) create mode 100644 src/templates/about.html create mode 100644 src/templates/contact.html create mode 100644 tests/flask_test_server.py create mode 100644 tests/test_pages.py create mode 100644 tests/test_selenium_contact.py diff --git a/pyproject.toml b/pyproject.toml index 04392ed..73c8d4c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,3 +10,9 @@ dependencies = [ "requests>=2.32.4", "sqlalchemy>=2.0.41", ] + +[dependency-groups] +dev = [ + "pytest>=8.4.0", + "selenium>=4.33.0", +] diff --git a/src/main.py b/src/main.py index 6fd7a6c..81b5d85 100644 --- a/src/main.py +++ b/src/main.py @@ -115,6 +115,22 @@ def pogoda(city): ) +@app.route("/about") +def about(): + return render_template("about.html") + + +@app.route("/contact", methods=["GET", "POST"]) +def contact(): + success = False + if request.method == "POST": + name = request.form.get("name") + message = request.form.get("message") + # Można tu dodać zapis do pliku lub bazy, na razie tylko flash/info + success = True + return render_template("contact.html", success=success) + + if __name__ == "__main__": from src.db_utils import Base, engine diff --git a/src/templates/about.html b/src/templates/about.html new file mode 100644 index 0000000..74ff797 --- /dev/null +++ b/src/templates/about.html @@ -0,0 +1,6 @@ +{% extends "index.html" %} +{% block content %} +

O projekcie

+

To jest przykładowa aplikacja stworzona na zajęcia z programowania w Pythonie z użyciem frameworka Flask. +Mikołaj Kaczmarek, 84985 / 124942

+{% endblock %} diff --git a/src/templates/contact.html b/src/templates/contact.html new file mode 100644 index 0000000..3f44170 --- /dev/null +++ b/src/templates/contact.html @@ -0,0 +1,14 @@ +{% extends "index.html" %} +{% block content %} +

Kontakt

+
+
+
+
+
+ +
+{% if success %} +

Dziękujemy za kontakt!

+{% endif %} +{% endblock %} diff --git a/src/templates/index.html b/src/templates/index.html index bdbbacb..62ca798 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -28,6 +28,9 @@
  • Zaloguj (test sesji)
  • Wyloguj (test sesji)
  • +
  • O projekcie
  • +
  • Kontakt
  • + {% block content %}{% endblock %} diff --git a/tests/flask_test_server.py b/tests/flask_test_server.py new file mode 100644 index 0000000..f7b9f83 --- /dev/null +++ b/tests/flask_test_server.py @@ -0,0 +1,14 @@ +import threading +import time +from src.main import app + +def run_app(): + app.run(host="127.0.0.1", port=5000, debug=False, use_reloader=False) + +def start_flask_in_thread(): + thread = threading.Thread(target=run_app) + thread.daemon = True + thread.start() + # Wait a bit for the server to start + time.sleep(2) + return thread diff --git a/tests/test_pages.py b/tests/test_pages.py new file mode 100644 index 0000000..41e2247 --- /dev/null +++ b/tests/test_pages.py @@ -0,0 +1,27 @@ +import pytest +from src.main import app + +@pytest.fixture +def client(): + app.testing = True + with app.test_client() as client: + yield client + +def test_about_page_status_and_content(client): + response = client.get('/about') + assert response.status_code == 200 + assert b'O projekcie' in response.data + +def test_contact_page_get(client): + response = client.get('/contact') + assert response.status_code == 200 + assert b'