9c5203dcceb294d868ba5504ef8c2ab2a25a1c1a
[dbsrgits/DBIx-Class.git] / t / inflate / file_column.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBICTest;
9 use DBICTest::Schema;
10 use File::Temp ();
11 use File::Compare;
12 use Path::Class qw/file/;
13
14 {
15   local $ENV{DBIC_IC_FILE_NOWARN} = 1;
16
17   package DBICTest::Schema::FileColumn;
18
19   use strict;
20   use warnings;
21   use base qw/DBICTest::BaseResult/;
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 => File::Temp->newdir( CLEANUP => 1, DIR => DBICTest::Util::tmpdir() ),
32       size             => 255
33     }
34   );
35
36   __PACKAGE__->set_primary_key('id');
37 }
38 DBICTest::Schema->load_classes('FileColumn');
39
40 my $schema = DBICTest->init_schema;
41
42 plan tests => 10;
43
44 if (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   )
50 EOF
51 }
52
53 my $rs = $schema->resultset('FileColumn');
54 my $source_file = file(__FILE__);
55 my $fname = $source_file->basename;
56 my $fh = $source_file->open('r') or die "failed to open $source_file: $!\n";
57 my $fc = eval {
58     $rs->create({ file => { handle => $fh, filename => $fname } })
59 };
60 is ( $@, '', 'created' );
61
62 $fh->close;
63
64 my $storage = file(
65     $fc->column_info('file')->{file_column_path},
66     $fc->id,
67     $fc->file->{filename},
68 );
69 ok ( -e $storage, 'storage exists' );
70
71 # read it back
72 $fc = $rs->find({ id => $fc->id });
73
74 is ( $fc->file->{filename}, $fname, 'filename matches' );
75 ok ( compare($storage, $source_file) == 0, 'file contents matches' );
76
77 # update
78 my $new_fname = 'File.pm';
79 my $new_source_file = file(qw/lib DBIx Class InflateColumn File.pm/);
80 my $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
90 {
91     local $TODO = 'design change required';
92     ok ( ! -e $storage, 'old storage does not exist' );
93 };
94
95 ok ( -e $new_storage, 'new storage exists' );
96
97 # read it back
98 $fc = $rs->find({ id => $fc->id });
99
100 is ( $fc->file->{filename}, $new_fname, 'new filname matches' );
101 ok ( compare($new_storage, $new_source_file) == 0, 'new content matches' );
102
103 if ($^O eq 'MSWin32') {
104   close $fc->file->{handle}; # can't delete open files on Win32
105 }
106 $fc->delete;
107
108 ok ( ! -e $storage, 'storage deleted' );
109
110 $fh = $source_file->openr or die "failed to open $source_file: $!\n";
111 $fc = $rs->create({ file => { handle => $fh, filename => $fname } });
112
113 # read it back
114 $fc->discard_changes;
115
116 $storage = file(
117     $fc->column_info('file')->{file_column_path},
118     $fc->id,
119     $fc->file->{filename},
120 );
121
122 {
123     local $TODO = 'need resultset delete override to delete_all';
124     $rs->delete;
125     ok ( ! -e $storage, 'storage does not exist after $rs->delete' );
126 }