def test_result_type_not_keyword_result_line(self): msg = r"Error in <literal>:2:12 - expected keyword" with pytest.raises(ParserError, match=msg): parser.parse( '-- :name foo\n' '-- :result raw\n' 'select 1')
def test_extra_after_result_nameline(self): msg = ('Error in <literal>:1:24 - encountered ' 'unexpected input after result type.') with pytest.raises(ParserError, match=msg): parser.parse( '-- :name foo :affected things\n' 'select 1')
def test_missing_result_type_result_line(self): msg = "Error in <literal>:2:11 - expected keyword" with pytest.raises(ParserError, match=msg): parser.parse( '-- :name foo\n' '-- :result \n' 'select 1')
def test_unrecognized_keyword_resultline(self): msg = "Error in <literal>:2:12 - unrecognized keyword ':nope'" with pytest.raises(ParserError, match=msg): parser.parse( '-- :name foo\n' '-- :result :nope\n' 'select 1')
def test_name_only_extra(self): msg = ('Error in <literal>:1:14 - encountered ' 'unexpected input after query name.') with pytest.raises(ParserError, match=msg): parser.parse( '-- :name foo and some other stuff\n' 'select 1')
def test_extra_after_result_result_line(self): msg = ('Error in <literal>:2:16 - encountered unexpected input after ' 'result type') with pytest.raises(ParserError, match=msg): parser.parse( '-- :name foo\n' '-- :result :raw thing\n' 'select 1')
def test_works_internal_whitespace(self): s = parser.parse('-- :name foo\n' '-- :result :1 \n' 'select 1') self.assertIsInstance(s.result, statement.One)
def test_unrecognized_keyword_nameline(self): msg = "Error in <literal>:1:14 - unrecognized keyword ':wrong'" with pytest.raises(ParserError, match=msg): parser.parse('-- :name foo :wrong\n' 'select 1')
def test_no_name(self): msg = 'Error in <literal>:1:9 - expected a query name.' with pytest.raises(ParserError, match=msg): parser.parse('-- :name \n' 'select 1;')
def parse(path): return parser.parse(sql(path))
def test_leading_underscores(self): s = parser.parse('-- :name _foo_bar\n' 'select 1') self.assertEqual('_foo_bar', s.name)
def test_dashes(self): with pytest.raises(ParserError, match=self.errmsg('foo-bar')): parser.parse('-- :name foo-bar\n' 'select 1')
def test_multiple_params(self): s = parser.parse('-- :name foo\n' 'select * from users where x=:x and y=:y') self.assertEqual( 'pugsql.statement.Statement: foo(x=None, y=None) :: raw', str(s))
def test_repr(self): s = parser.parse('-- :name foo\nselect * from users') self.assertEqual('pugsql.statement.Statement: foo() :: raw', repr(s))
def test_nonalphaunderscore(self): with pytest.raises(ParserError, match=self.errmsg('foo#')): parser.parse('-- :name foo#\n' 'select 1')
def test_begins_with_number(self): with pytest.raises(ParserError, match=self.errmsg('9foo')): parser.parse('-- :name 9foo\n' 'select 1')
def test_param_order(self): s = parser.parse('-- :name foo\n' 'select * from users where y=:y and z=:b and a=:a') self.assertEqual( 'pugsql.statement.Statement: foo(y=None, b=None, a=None) :: raw', str(s))
def test_numbers(self): s = parser.parse('-- :name foo1\n' 'select 1') self.assertEqual('foo1', s.name)
def test_row(self): s = parser.parse('-- :name foo :1\nselect * from users limit 1') self.assertEqual('pugsql.statement.Statement: foo() :: row', str(s))
def test_uppercase(self): s = parser.parse('-- :name _FOO_BAR\n' 'select 1') self.assertEqual('_FOO_BAR', s.name)
def test_rowcount(self): s = parser.parse('-- :name foo :affected\nselect * from users') self.assertEqual('pugsql.statement.Statement: foo() :: rowcount', str(s))