import pytest
import os
from simple_orm import DatabaseConnection, Model, IntegerField, CharField
from dotenv import load_dotenv

load_dotenv()

class User(Model):
    name = CharField(max_length=100)
    age = IntegerField()

DB_NAME = os.getenv("DB_NAME")
DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")


# This fixture runs once per test session
@pytest.fixture(scope="module", autouse=True)
def setup_database():
    # Adjust these arguments based on your actual connect() signature
    DatabaseConnection.connect(DB_NAME, DB_USER, DB_PASSWORD)
    User.create_table()
    yield
    # Optional: cleanup logic here


def test_create_and_retrieve():
    user = User.objects.create(name='Alice', age=30)
    retrieved = User.objects.get(id=user.id)

    assert retrieved.name == 'Alice'
    assert retrieved.age == 30


def test_filter():
    User.objects.create(name='Bob', age=25)

    users_over_20 = list(User.objects.filter(age__gt=20))

    assert len(users_over_20) > 0