Simple work on update errors
[dbsrgits/SQL-Abstract-2.0-ish.git] / t / 900_errors.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 7;
5 use Test::Exception;
6
7 use FindBin;
8 use lib "$FindBin::Bin/lib";
9 use Test::SQL::Abstract::Util qw/
10   mk_name
11   mk_value
12   mk_alias
13 /;
14
15 use_ok('SQL::Abstract') or BAIL_OUT( "$@" );
16
17 my $sqla = SQL::Abstract->create(1);
18
19 throws_ok {
20   $sqla->dispatch(
21     { -type => 'expr', op => '==',
22       args => [
23         { -type => 'identifier', elements => [qw/me id/] },
24         { -type => 'alias', ident => { -type => 'identifier', elements => [qw/me id/] }, as => 'bar' }
25       ]
26     }
27   )
28 } qr/^'alias' is not a valid AST type in an expression/, "Error from invalid part in where";
29
30 throws_ok {
31   $sqla->dispatch(
32     { -type => 'expr', op => '~' }
33   )
34 } qr/^'~' is not a valid operator in an expression/;
35
36 {
37 local $TODO = "Work out how to get nice errors for these";
38
39 throws_ok {
40   $sqla->dispatch(
41     { -type => 'alias', ident => 2 } # no as, inavlid ident
42   )
43 } qr/foobar/, "alias: no as, invalid ident";
44
45 throws_ok {
46   $sqla->dispatch(
47     { -type => 'alias', iden => { -type => 'identifier', elements => ['id'] }, as => 'foo' } # iden not ident
48   )
49 } qr/foobar/, "alias: iden instead of ident";
50
51 }
52
53 throws_ok {
54   $sqla->dispatch(
55     { -type => 'update',
56       tablespec => mk_name('test'),
57       columns => [
58         mk_name(qw/me id/),
59         mk_alias(mk_name(qw/foo id/) ,'foo_id')
60       ]
61     }
62   )
63 } qr/^'values' is required in update AST/, "Invalid clause in update";
64
65
66 throws_ok { $sqla->dispatch(
67   { -type => 'update',
68     tablespec => mk_name('test'),
69     columns => [
70       mk_name(qw/me id/),
71       mk_name(qw/hostname/),
72     ],
73     values => [
74       mk_value('localhost'),
75     ]
76   } )
77 } qr/Number of values does not match columns/,
78   "Column/values count mismatch in update";