File: test_instance_no_args.py

package info (click to toggle)
sqlmodel 0.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,400 kB
  • sloc: python: 11,752; javascript: 278; sh: 42; makefile: 8
file content (27 lines) | stat: -rw-r--r-- 777 bytes parent folder | download
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
from typing import Optional

from sqlalchemy import create_engine, select
from sqlalchemy.orm import Session
from sqlmodel import Field, SQLModel


def test_allow_instantiation_without_arguments(clear_sqlmodel):
    class Item(SQLModel):
        id: Optional[int] = Field(default=None, primary_key=True)
        name: str
        description: Optional[str] = None

        class Config:
            table = True

    engine = create_engine("sqlite:///:memory:")
    SQLModel.metadata.create_all(engine)
    with Session(engine) as db:
        item = Item()
        item.name = "Rick"
        db.add(item)
        db.commit()
        result = db.execute(select(Item)).scalars().all()
    assert len(result) == 1
    assert isinstance(item.id, int)
    SQLModel.metadata.clear()