Spaces:
Running
Running
File size: 3,718 Bytes
9a255bf |
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
import unittest
import os
import pandas as pd
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from database import initialize_database, add_participant, get_participants_dataframe
# Use a separate test database
TEST_DB_PATH = "test_hackathon.db"
class TestDatabase(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Set up a test database before any tests run."""
cls.db_path = TEST_DB_PATH
# Point the database module to the test DB
import database
database.DB_PATH = cls.db_path
def setUp(self):
"""Initialize a clean database before each test."""
if os.path.exists(self.db_path):
os.remove(self.db_path)
initialize_database()
def tearDown(self):
"""Remove the database file after each test."""
if os.path.exists(self.db_path):
os.remove(self.db_path)
def test_01_initialize_database(self):
"""Test that the database and table are created."""
self.assertTrue(os.path.exists(self.db_path))
df = get_participants_dataframe()
self.assertIsInstance(df, pd.DataFrame)
self.assertEqual(len(df), 0)
def test_02_add_and_get_participant(self):
"""Test adding a single participant and retrieving it."""
participant = {
"email": "[email protected]",
"name": "Test User",
"linkedin_profile": "linkedin.com/in/test",
"background": "Testing background.",
"goals": "Testing goals."
}
add_participant(participant)
df = get_participants_dataframe()
self.assertEqual(len(df), 1)
retrieved = df.iloc[0]
self.assertEqual(retrieved["email"], participant["email"])
self.assertEqual(retrieved["name"], participant["name"])
def test_03_add_multiple_participants(self):
"""Test adding multiple participants."""
participants_to_add = [
{"email": "[email protected]", "name": "User One", "linkedin_profile": "", "background": "", "goals": ""},
{"email": "[email protected]", "name": "User Two", "linkedin_profile": "", "background": "", "goals": ""},
]
for p in participants_to_add:
add_participant(p)
df = get_participants_dataframe()
self.assertEqual(len(df), 2)
self.assertIn("[email protected]", df["email"].values)
self.assertIn("[email protected]", df["email"].values)
def test_04_replace_participant(self):
"""Test that adding a participant with an existing email updates the record."""
original_participant = {
"email": "[email protected]",
"name": "Original Name",
"linkedin_profile": "original_linkedin",
"background": "original_background",
"goals": "original_goals"
}
add_participant(original_participant)
df_before = get_participants_dataframe()
self.assertEqual(len(df_before), 1)
self.assertEqual(df_before.iloc[0]["name"], "Original Name")
updated_participant = {
"email": "[email protected]",
"name": "Updated Name",
"linkedin_profile": "updated_linkedin",
"background": "updated_background",
"goals": "updated_goals"
}
add_participant(updated_participant)
df_after = get_participants_dataframe()
self.assertEqual(len(df_after), 1)
self.assertEqual(df_after.iloc[0]["name"], "Updated Name")
self.assertEqual(df_after.iloc[0]["linkedin_profile"], "updated_linkedin")
if __name__ == '__main__':
unittest.main() |