Debian Codegraph @

t/cdbi-t-ocache/99-misc.t

raw
1 use strict;
2 use Test::More;
3 use Class::DBI::Sweet;
4 Class::DBI::Sweet->default_search_attributes({ use_resultset_cache => 0 });
5 Class::DBI::Sweet->cache(Cache::MemoryCache->new(
6 { namespace => "SweetTest", default_expires_in => 60 } ) );
7 use File::Temp qw/tempdir/;
8
9 #----------------------------------------------------------------------
10 # Test various errors / warnings / deprecations etc
11 #----------------------------------------------------------------------
12
13 BEGIN {
14 eval "use Cache::MemoryCache";
15 plan skip_all => "needs Cache::Cache for testing" if $@;
16 eval "use DBD::SQLite";
17 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 22);
18 }
19
20 use File::Temp qw/tempfile/;
21 my (undef, $DB) = tempfile();
22 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1 });
23
24 END { unlink $DB if -e $DB }
25
26 package Holiday;
27
28 use base 'Class::DBI::Sweet';
29
30 {
31 my $warning;
32 local $SIG{__WARN__} = sub {
33 $warning = $_[0];
34 };
35 no warnings 'once';
36 local *UNIVERSAL::wonka = sub { };
37 Holiday->columns(TEMP => 'wonka');
38 ::like $warning, qr/wonka.*clashes/, "Column clash warning for inherited";
39 undef $warning;
40
41 Holiday->columns(Primary => 'new');
42 ::like $warning, qr/new.*clashes/, "Column clash warning with CDBI";
43 undef $warning;
44
45 Holiday->add_constructor('by_train');
46 Holiday::Camp->add_constructor('by_train');
47 ::is $warning, undef, "subclassed constructor";
48 }
49
50 {
51 local $SIG{__WARN__} = sub {
52 ::like $_[0], qr/deprecated/, "create trigger deprecated";
53 };
54 Holiday->add_trigger('create' => sub { 1 });
55 Holiday->add_trigger('delete' => sub { 1 });
56 }
57
58 {
59 eval { Holiday->add_constraint };
60 ::like $@, qr/needs a name/, "Constraint with no name";
61 eval { Holiday->add_constraint('check_mate') };
62 ::like $@, qr/needs a valid column/, "Constraint needs a column";
63 eval { Holiday->add_constraint('check_mate', 'jamtart') };
64 ::like $@, qr/needs a valid column/, "No such column";
65 eval { Holiday->add_constraint('check_mate', 'new') };
66 ::like $@, qr/needs a code ref/, "Need a coderef";
67 eval { Holiday->add_constraint('check_mate', 'new', {}) };
68 ::like $@, qr/not a code ref/, "Not a coderef";
69
70 eval { Holiday->has_a('new') };
71 ::like $@, qr/associated class/, "has_a needs a class";
72
73 eval { Holiday->add_constructor() };
74 ::like $@, qr/name/, "add_constructor needs a method name";
75
76 eval {
77 Holiday->add_trigger(on_setting => sub { 1 });
78 };
79 ::like $@, qr/no longer exists/, "No on_setting trigger";
80
81 {
82 local $SIG{__WARN__} = sub {
83 ::like $_[0], qr/new.*clashes/, "Column clash warning";
84 };
85 }
86 }
87
88 package main;
89
90 {
91 package Holiday::Camp;
92 use base 'Holiday';
93
94 __PACKAGE__->table("holiday");
95 __PACKAGE__->add_trigger(before_create => sub {
96 my $self = shift;
97 $self->_croak("Problem with $self\n");
98 });
99
100 package main;
101 eval { Holiday::Camp->insert({}) };
102 like $@, qr/Problem with Holiday/, '$self stringifies with no PK values';
103 }
104
105 eval { my $foo = Holiday->retrieve({ id => 1 }) };
106 like $@, qr/retrieve a reference/, "Can't retrieve a reference";
107
108 eval { my $foo = Holiday->insert(id => 10) };
109 like $@, qr/a hashref/, "Can't create without hashref";
110
111 {
112 my $foo = bless {}, 'Holiday';
113 local $SIG{__WARN__} = sub { die $_[0] };
114 eval { $foo->has_a(date => 'Date::Simple') };
115 like $@, qr/object method/, "has_a is class-level: $@";
116 }
117
118 eval { Holiday->update; };
119 like $@, qr/class method/, "Can't call update as class method";
120
121 is(Holiday->table, 'holiday', "Default table name");
122
123 Holiday->_flesh('Blanket');
124
125 eval { Holiday->insert({ yonkey => 84 }) };
126 like $@, qr/not a column/, "Can't create with nonsense column";
127
128 eval { Film->_require_class('Class::DBI::__::Nonsense') };
129 like $@, qr/Can't locate/, "Can't require nonsense class";
130
131 eval { Holiday->search_DeleteMe };
132 like $@, qr/locate.*DeleteMe/, $@;
133