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