Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / inflate / file_column.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
4740bdb7 3use strict;
3814fdad 4use warnings;
4740bdb7 5
6use Test::More;
4740bdb7 7use DBICTest;
123df526 8use DBICTest::Schema;
26ddc864 9use File::Compare;
10use Path::Class qw/file/;
4740bdb7 11
123df526 12{
13 local $ENV{DBIC_IC_FILE_NOWARN} = 1;
14
15 package DBICTest::Schema::FileColumn;
16
17 use strict;
18 use warnings;
19 use base qw/DBICTest::BaseResult/;
20
21 use File::Temp qw/tempdir/;
22
23 __PACKAGE__->load_components (qw/InflateColumn::File/);
24 __PACKAGE__->table('file_columns');
25
26 __PACKAGE__->add_columns(
27 id => { data_type => 'integer', is_auto_increment => 1 },
28 file => {
29 data_type => 'varchar',
30 is_file_column => 1,
31 file_column_path => tempdir(CLEANUP => 1),
32 size => 255
33 }
34 );
35
36 __PACKAGE__->set_primary_key('id');
37}
38DBICTest::Schema->load_classes('FileColumn');
39
823c91a1 40my $schema = DBICTest->init_schema;
4740bdb7 41
5d8d8611 42plan tests => 10;
4740bdb7 43
8b40ac1a 44if (not $ENV{DBICTEST_SQLT_DEPLOY}) {
45 $schema->storage->dbh->do(<<'EOF');
46 CREATE TABLE file_columns (
47 id INTEGER PRIMARY KEY,
48 file VARCHAR(255)
49 )
50EOF
51}
52
26ddc864 53my $rs = $schema->resultset('FileColumn');
100fd57f 54my $source_file = file(__FILE__);
55my $fname = $source_file->basename;
26ddc864 56my $fh = $source_file->open('r') or die "failed to open $source_file: $!\n";
57my $fc = eval {
58 $rs->create({ file => { handle => $fh, filename => $fname } })
59};
60is ( $@, '', 'created' );
61
62$fh->close;
63
64my $storage = file(
65 $fc->column_info('file')->{file_column_path},
66 $fc->id,
67 $fc->file->{filename},
68);
69ok ( -e $storage, 'storage exists' );
70
71# read it back
72$fc = $rs->find({ id => $fc->id });
73
74is ( $fc->file->{filename}, $fname, 'filename matches' );
75ok ( compare($storage, $source_file) == 0, 'file contents matches' );
76
77# update
78my $new_fname = 'File.pm';
79my $new_source_file = file(qw/lib DBIx Class InflateColumn File.pm/);
80my $new_storage = file(
81 $fc->column_info('file')->{file_column_path},
82 $fc->id,
83 $new_fname,
84);
85$fh = $new_source_file->open('r') or die "failed to open $new_source_file: $!\n";
86
87$fc->file({ handle => $fh, filename => $new_fname });
88$fc->update;
89
4ca1fd6f 90{
26ddc864 91 local $TODO = 'design change required';
92 ok ( ! -e $storage, 'old storage does not exist' );
93};
94
95ok ( -e $new_storage, 'new storage exists' );
96
97# read it back
98$fc = $rs->find({ id => $fc->id });
99
100is ( $fc->file->{filename}, $new_fname, 'new filname matches' );
101ok ( compare($new_storage, $new_source_file) == 0, 'new content matches' );
102
9fb04755 103if ($^O eq 'MSWin32') {
104 close $fc->file->{handle}; # can't delete open files on Win32
1239d14e 105}
26ddc864 106$fc->delete;
107
108ok ( ! -e $storage, 'storage deleted' );
30a93e27 109
5d8d8611 110$fh = $source_file->openr or die "failed to open $source_file: $!\n";
111$fc = $rs->create({ file => { handle => $fh, filename => $fname } });
30a93e27 112
5d8d8611 113# read it back
114$fc->discard_changes;
30a93e27 115
5d8d8611 116$storage = file(
117 $fc->column_info('file')->{file_column_path},
118 $fc->id,
119 $fc->file->{filename},
120);
30a93e27 121
4ca1fd6f 122{
5d8d8611 123 local $TODO = 'need resultset delete override to delete_all';
30a93e27 124 $rs->delete;
125 ok ( ! -e $storage, 'storage does not exist after $rs->delete' );
4ca1fd6f 126}