File size: 2,677 Bytes
a289188 8625b3d a289188 8625b3d a289188 | 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | import pytest
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app import create_app
@pytest.fixture
def app():
app = create_app()
app.config['TESTING'] = True
return app
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture(autouse=True)
def load_data(client):
"""Ensure data is loaded before model tests."""
client.post('/api/pipeline/run')
class TestModelTraining:
def test_train_linear_regression(self, client):
response = client.post('/api/model/train',
json={'model_type': 'linear_regression'})
assert response.status_code == 200
data = response.get_json()
assert 'metrics' in data
assert 'r2_score' in data['metrics']
def test_train_random_forest(self, client):
response = client.post('/api/model/train',
json={'model_type': 'random_forest'})
assert response.status_code == 200
data = response.get_json()
assert 'metrics' in data
def test_train_kmeans(self, client):
response = client.post('/api/model/train',
json={'model_type': 'kmeans'})
assert response.status_code == 200
data = response.get_json()
assert 'clusters' in data or 'metrics' in data
def test_predict(self, client):
# Train first
client.post('/api/model/train',
json={'model_type': 'linear_regression'})
# Then predict
response = client.post('/api/model/predict', json={
'location': 'Downtown',
'flat_type': '4 ROOM',
'floor_area_sqm': 95,
'storey_mid': 10,
'remaining_lease_years': 85
})
assert response.status_code == 200
data = response.get_json()
assert 'predicted_price' in data
assert data['predicted_price'] > 0
def test_model_history(self, client):
client.post('/api/model/train',
json={'model_type': 'linear_regression'})
response = client.get('/api/model/history')
assert response.status_code == 200
data = response.get_json()
assert 'history' in data
assert len(data['history']) > 0
class TestNLP:
def test_text_classification(self, client):
response = client.post('/api/model/train', json={
'model_type': 'text_classifier',
'text': 'The metro was very crowded today'
})
# This may or may not be a supported endpoint — check gracefully
assert response.status_code in [200, 400]
|