Coverage for tests/test_generation/test_schema_dsl_parser.py: 100%

29 statements  

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

1from __future__ import annotations 

2 

3from inline_snapshot import snapshot 

4 

5from prisma.generator import parse_schema_dsl 

6 

7 

8def test_with_single_arg() -> None: 

9 assert parse_schema_dsl("""@Python(name: "UserFoo")""") == snapshot( 

10 {'type': 'ok', 'value': {'arguments': {'name': 'UserFoo'}}} 

11 ) 

12 

13 

14def test_with_multiple_args() -> None: 

15 assert parse_schema_dsl("""@Python(name: "UserFoo", instance_name: "user_foo")""") == snapshot( 

16 {'type': 'ok', 'value': {'arguments': {'name': 'UserFoo', 'instance_name': 'user_foo'}}} 

17 ) 

18 

19 

20def test_string_value_no_quotes() -> None: 

21 assert parse_schema_dsl("""@Python(name: UserFoo)""") == snapshot( 

22 { 

23 'type': 'invalid', 

24 'error': """\ 

25Unexpected token Token('CNAME', 'UserFoo') at line 1, column 15. 

26Expected one of:  

27 * ESCAPED_STRING 

28Previous tokens: [Token('COLON', ':')] 

29@Python(name: UserFoo) 

30 ^ 

31""", 

32 } 

33 ) 

34 

35 

36def test_string_value_double_quotes() -> None: 

37 assert parse_schema_dsl("""@Python(name: "UserFoo")""") == snapshot( 

38 {'type': 'ok', 'value': {'arguments': {'name': 'UserFoo'}}} 

39 ) 

40 

41 

42def test_string_value_single_quotes() -> None: 

43 assert parse_schema_dsl("""@Python(name: 'UserFoo')""") == snapshot( 

44 {'type': 'ok', 'value': {'arguments': {'name': 'UserFoo'}}} 

45 ) 

46 

47 

48def test_with_number_arg() -> None: 

49 assert parse_schema_dsl("""@Python(foo: 1)""") == snapshot( 

50 { 

51 'type': 'invalid', 

52 'error': """\ 

53No terminal matches '1' in the current parser context, at line 1 col 14 

54 

55@Python(foo: 1) 

56 ^ 

57Expected one of:  

58 * ESCAPED_STRING 

59 

60Previous tokens: Token('COLON', ':') 

61@Python(foo: 1) 

62 ^ 

63""", 

64 } 

65 ) 

66 

67 

68def test_no_args() -> None: 

69 assert parse_schema_dsl("""@Python()""") == snapshot({'type': 'not_applicable'}) 

70 

71 

72def test_no_pattern() -> None: 

73 assert parse_schema_dsl("""foo bar baz!""") == snapshot({'type': 'not_applicable'}) 

74 

75 

76def test_at_symbol_different_ident() -> None: 

77 assert parse_schema_dsl("""@Foo(bar: baz)""") == snapshot({'type': 'not_applicable'}) 

78 

79 

80def test_multiple() -> None: 

81 assert parse_schema_dsl( 

82 """ 

83 @Python(bar: baz) 

84 @Python(foo: bar) 

85 """ 

86 ) == snapshot({'type': 'invalid', 'error': 'Encountered multiple `@Python` declarations'}) 

87 

88 

89def test_missing_arg_colon_sep() -> None: 

90 assert parse_schema_dsl("""@Python(name "UserFoo")""") == snapshot( 

91 { 

92 'type': 'invalid', 

93 'error': """\ 

94Unexpected token Token('ESCAPED_STRING', '"UserFoo"') at line 1, column 14. 

95Expected one of:  

96 * COLON 

97Previous tokens: [Token('CNAME', 'name')] 

98@Python(name "UserFoo") 

99 ^ 

100""", 

101 } 

102 ) 

103 

104 

105def test_with_arbitrary_surrounding_text() -> None: 

106 assert parse_schema_dsl("""foo bar! () @Python(name: 'UserFoo') baz bing 18738/.m{]}""") == snapshot( 

107 {'type': 'ok', 'value': {'arguments': {'name': 'UserFoo'}}} 

108 ) 

109 

110 

111def test_error_with_arbitrary_surrounding_text() -> None: 

112 assert parse_schema_dsl("""foo bar! () @Python(name 'UserFoo') baz bing 18738/.m{]}""") == snapshot( 

113 { 

114 'type': 'invalid', 

115 'error': """\ 

116Unexpected token Token('ESCAPED_STRING', "'UserFoo'") at line 1, column 14. 

117Expected one of:  

118 * COLON 

119Previous tokens: [Token('CNAME', 'name')] 

120@Python(name 'UserFoo') 

121 ^ 

122""", 

123 } 

124 )