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