SHL / test_basic.py
Harsh-1132's picture
Clean deployment
d18c374
#!/usr/bin/env python3
"""
Test script for SHL Assessment Recommender System
Tests basic functionality without requiring full model downloads.
"""
import sys
import os
def test_imports():
"""Test that all modules can be imported"""
print("Testing imports...")
try:
import pandas
import numpy
import sklearn
from bs4 import BeautifulSoup
import requests
print("βœ“ Data processing packages")
except ImportError as e:
print(f"βœ— Data processing packages: {e}")
return False
try:
from src import crawler, preprocess
print("βœ“ Core modules (crawler, preprocess)")
except ImportError as e:
print(f"βœ— Core modules: {e}")
return False
try:
import fastapi
import uvicorn
import streamlit
print("βœ“ API and UI packages")
except ImportError as e:
print(f"βœ— API and UI packages: {e}")
return False
return True
def test_data_files():
"""Test that required data files exist"""
print("\nTesting data files...")
# Check training data
if os.path.exists('Data/Gen_AI Dataset.xlsx'):
print("βœ“ Training dataset found")
else:
print("βœ— Training dataset not found (Data/Gen_AI Dataset.xlsx)")
# Check catalog
if os.path.exists('data/shl_catalog.csv'):
print("βœ“ SHL catalog found")
import pandas as pd
df = pd.read_csv('data/shl_catalog.csv')
print(f" - {len(df)} assessments")
print(f" - K assessments: {len(df[df['test_type'] == 'K'])}")
print(f" - P assessments: {len(df[df['test_type'] == 'P'])}")
else:
print("⚠ SHL catalog not found (run: python src/crawler.py)")
return True
def test_crawler():
"""Test the crawler module"""
print("\nTesting crawler...")
try:
from src.crawler import SHLCrawler
crawler = SHLCrawler()
# Test text classification
assert crawler.determine_test_type("Java programming test") == "K"
assert crawler.determine_test_type("Personality assessment") == "P"
print("βœ“ Test type classification works")
# Test category extraction
cat = crawler.extract_category("Leadership management")
assert cat == "Leadership"
print("βœ“ Category extraction works")
return True
except Exception as e:
print(f"βœ— Crawler test failed: {e}")
return False
def test_preprocessor():
"""Test the preprocessor module"""
print("\nTesting preprocessor...")
try:
from src.preprocess import DataPreprocessor
preprocessor = DataPreprocessor()
# Test text cleaning
clean = preprocessor.clean_text(" Hello, WORLD! ")
assert clean == "hello, world!"
print("βœ“ Text cleaning works")
# Test URL extraction
urls = preprocessor.extract_urls_from_text("Check https://example.com and http://test.com")
assert len(urls) == 2
print("βœ“ URL extraction works")
return True
except Exception as e:
print(f"βœ— Preprocessor test failed: {e}")
return False
def test_api_structure():
"""Test that API is properly structured"""
print("\nTesting API structure...")
try:
from api.main import app
# Check endpoints exist
routes = [route.path for route in app.routes]
assert "/health" in routes
print("βœ“ /health endpoint exists")
assert "/recommend" in routes
print("βœ“ /recommend endpoint exists")
return True
except Exception as e:
print(f"βœ— API structure test failed: {e}")
return False
def test_streamlit_app():
"""Test that Streamlit app can be imported"""
print("\nTesting Streamlit app...")
try:
# Just check the file exists and is valid Python
with open('app.py', 'r') as f:
content = f.read()
assert 'st.set_page_config' in content
print("βœ“ Streamlit app file valid")
assert 'SHL Assessment Recommender' in content
print("βœ“ App title configured")
return True
except Exception as e:
print(f"βœ— Streamlit app test failed: {e}")
return False
def main():
"""Run all tests"""
print("="*60)
print("SHL ASSESSMENT RECOMMENDER - BASIC TESTS")
print("="*60)
tests = [
("Imports", test_imports),
("Data Files", test_data_files),
("Crawler", test_crawler),
("Preprocessor", test_preprocessor),
("API Structure", test_api_structure),
("Streamlit App", test_streamlit_app)
]
results = []
for test_name, test_func in tests:
try:
result = test_func()
results.append((test_name, result))
except Exception as e:
print(f"\nβœ— {test_name} failed with exception: {e}")
results.append((test_name, False))
# Summary
print("\n" + "="*60)
print("TEST SUMMARY")
print("="*60)
passed = sum(1 for _, result in results if result)
total = len(results)
for test_name, result in results:
status = "βœ“ PASS" if result else "βœ— FAIL"
print(f"{status}: {test_name}")
print(f"\nTotal: {passed}/{total} tests passed")
if passed == total:
print("\nβœ“ All basic tests passed!")
print("\nNote: Full system tests require:")
print(" - Internet connection (for model downloads)")
print(" - Running: python setup.py")
return 0
else:
print("\nβœ— Some tests failed")
return 1
if __name__ == "__main__":
sys.exit(main())