Coverage for tests/test_misc.py: 100%

33 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1import pytest 

2from _pytest.capture import CaptureFixture 

3 

4from prisma import Prisma 

5from prisma.models import User 

6 

7from .utils import Testdir 

8 

9 

10def test_create_partial_raises_outside_generation() -> None: 

11 """Trying to create a partial type outside of client generation raises an error""" 

12 with pytest.raises(RuntimeError) as exc: 

13 User.create_partial('PartialUser', exclude={'name'}) 

14 assert 'outside of client generation' in str(exc.value) 

15 

16 

17@pytest.mark.asyncio 

18async def test_query_logging_disabled(client: Prisma, capsys: CaptureFixture[str]) -> None: 

19 """No queries are logged when query logging is disabled""" 

20 await client.user.create({'name': 'Robert'}) 

21 captured = capsys.readouterr() 

22 assert captured.out == '' 

23 assert captured.err == '' 

24 

25 

26@pytest.mark.asyncio 

27async def test_logs_sql_queries(testdir: Testdir) -> None: 

28 """SQL queries are logged when enabled""" 

29 client = Prisma(log_queries=True) 

30 

31 # we have to redirect stdout to a file to capture it as 

32 # we are passing stdout to a subprocess 

33 with testdir.redirect_stdout_to_file() as file: 

34 await client.connect() 

35 

36 user = await client.user.find_unique(where={'id': 'jsdhsjd'}) 

37 assert user is None 

38 

39 await client.disconnect() 

40 

41 assert 'SELECT `main`.`User`.`id' in file.read_text() 

42 

43 

44@pytest.mark.asyncio 

45async def test_unmarked_test_disallowed_client() -> None: 

46 """Test case that isn't marked with @pytest.mark.prisma cannot access the client""" 

47 with pytest.raises(RuntimeError) as exc: 

48 await User.prisma().create({'name': 'Robert'}) 

49 

50 assert '@pytest.mark.prisma' in exc.value.args[0] 

51 

52 

53def test_missing_autogenerated_import_message(testdir: Testdir) -> None: 

54 """If the Client has not been generated yet but is attempting to be imported 

55 then a helpful error message should be raised pointing to a fix. 

56 """ 

57 testdir.copy_pkg() 

58 result = testdir.runpython_c('from prisma import Prisma') 

59 result.stderr.re_match_lines(['.*you must run `prisma generate` before you can use the client.*'])