Move the test bits related to deprecated IC::File out of the main testschema
[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 my $rs = $schema->resultset('FileColumn');
46 my $source_file = file(__FILE__);
47 my $fname = $source_file->basename;
48 my $fh = $source_file->open('r') or die "failed to open $source_file: $!\n";
49 my $fc = eval {
50     $rs->create({ file => { handle => $fh, filename => $fname } })
51 };
52 is ( $@, '', 'created' );
53
54 $fh->close;
55
56 my $storage = file(
57     $fc->column_info('file')->{file_column_path},
58     $fc->id,
59     $fc->file->{filename},
60 );
61 ok ( -e $storage, 'storage exists' );
62
63 # read it back
64 $fc = $rs->find({ id => $fc->id });
65
66 is ( $fc->file->{filename}, $fname, 'filename matches' );
67 ok ( compare($storage, $source_file) == 0, 'file contents matches' );
68
69 # update
70 my $new_fname = 'File.pm';
71 my $new_source_file = file(qw/lib DBIx Class InflateColumn File.pm/);
72 my $new_storage = file(
73     $fc->column_info('file')->{file_column_path},
74     $fc->id,
75     $new_fname,
76 );
77 $fh = $new_source_file->open('r') or die "failed to open $new_source_file: $!\n";
78
79 $fc->file({ handle => $fh, filename => $new_fname });
80 $fc->update;
81
82 TODO: {
83     local $TODO = 'design change required';
84     ok ( ! -e $storage, 'old storage does not exist' );
85 };
86
87 ok ( -e $new_storage, 'new storage exists' );
88
89 # read it back
90 $fc = $rs->find({ id => $fc->id });
91
92 is ( $fc->file->{filename}, $new_fname, 'new filname matches' );
93 ok ( compare($new_storage, $new_source_file) == 0, 'new content matches' );
94
95 if ($^O =~ /win32|cygwin/i) {
96   close $fc->file->{handle}; # can't delete open files on Windows
97 }
98 $fc->delete;
99
100 ok ( ! -e $storage, 'storage deleted' );
101
102 $fh = $source_file->openr or die "failed to open $source_file: $!\n";
103 $fc = $rs->create({ file => { handle => $fh, filename => $fname } });
104
105 # read it back
106 $fc->discard_changes;
107
108 $storage = file(
109     $fc->column_info('file')->{file_column_path},
110     $fc->id,
111     $fc->file->{filename},
112 );
113
114 TODO: {
115     local $TODO = 'need resultset delete override to delete_all';
116     $rs->delete;
117     ok ( ! -e $storage, 'storage does not exist after $rs->delete' );
118 };