Coverage for tests/test_query.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from typing import Optional 1ghijkl

2 

3import pytest 1ghijkl

4from sqlmodel import Field, Session, SQLModel, create_engine 1ghijkl

5 

6 

7def test_query(clear_sqlmodel): 1ghijkl

8 class Hero(SQLModel, table=True): 1abcdef

9 id: Optional[int] = Field(default=None, primary_key=True) 1abcdef

10 name: str 1abcdef

11 secret_name: str 1abcdef

12 age: Optional[int] = None 1abcdef

13 

14 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1abcdef

15 

16 engine = create_engine("sqlite://") 1abcdef

17 

18 SQLModel.metadata.create_all(engine) 1abcdef

19 with Session(engine) as session: 1abcdef

20 session.add(hero_1) 1abcdef

21 session.commit() 1abcdef

22 session.refresh(hero_1) 1abcdef

23 

24 with Session(engine) as session: 1abcdef

25 with pytest.warns(DeprecationWarning): 1abcdef

26 query_hero = session.query(Hero).first() 1abcdef

27 assert query_hero 1abcdef

28 assert query_hero.name == hero_1.name 1abcdef