-
Notifications
You must be signed in to change notification settings - Fork 563
Expand file tree
/
Copy pathtest_api.py
More file actions
43 lines (37 loc) · 1.69 KB
/
test_api.py
File metadata and controls
43 lines (37 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pytest
import json
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from app import create_app
@pytest.fixture
def client():
app = create_app()
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' # Use in-memory DB for tests
with app.test_client() as client:
with app.app_context():
yield client
def test_post_note(client):
payload = {"title": "Test Note", "content": "This is a test note."}
response = client.post("/api/notes/", data=json.dumps(payload), content_type="application/json")
assert response.status_code == 201
assert b'Note created' in response.data
def test_get_notes(client):
# Ensure at least one note exists
client.post("/api/notes/", data=json.dumps({"title": "Temp", "content": "Temp"}), content_type="application/json")
response = client.get("/api/notes/")
assert response.status_code == 200
assert isinstance(response.get_json(), list)
def test_update_note(client):
# Create a note first
client.post("/api/notes/", data=json.dumps({"title": "Temp", "content": "Temp"}), content_type="application/json")
response = client.put("/api/notes/1", data=json.dumps({"title": "Updated", "content": "Updated"}), content_type="application/json")
assert response.status_code == 200
assert b'Note updated' in response.data
def test_delete_note(client):
# Create a note first
client.post("/api/notes/", data=json.dumps({"title": "Temp", "content": "Temp"}), content_type="application/json")
response = client.delete("/api/notes/1")
assert response.status_code == 200
assert b'Note deleted' in response.data