Skip to content

Commit

Permalink
test: added api endpoint unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ongsici committed Oct 21, 2024
1 parent 279e6ec commit fa9819d
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/test_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from app import process_query
import pytest
from flask import Flask
from app import process_query, app


def test_knows_about_dinosaurs():
Expand All @@ -8,3 +10,48 @@ def test_knows_about_dinosaurs():

def test_does_not_know_about_asteroids():
assert process_query("asteroids") == "Unknown"


@pytest.fixture
def client():
with app.test_client() as client:
yield client


def test_hello_world(client):
response = client.get("/")
assert response.status_code == 200


def test_submit_happy(client):
response = client.post("/submit", data={
"name": "Alice",
"age": "30",
"gender": "female",
"happy": "5"
})
assert response.status_code == 200
assert b"Hello Alice" in response.data


def test_submit_sad(client):
response = client.post("/submit", data={
"name": "Bob",
"age": "25",
"gender": "male",
"happy": "-1"
})
assert response.status_code == 200
assert b"Hello Bob" in response.data


def test_query(client):
response = client.get("/query", query_string={"q": "dinosaurs"})
assert response.status_code == 200
assert b"Dinosaurs ruled the Earth 200 million years ago" in response.data


def test_query_unknown(client):
response = client.get("/query", query_string={"q": "asteroid"})
assert response.status_code == 200
assert b"Unknown" in response.data

0 comments on commit fa9819d

Please sign in to comment.