Restore backwards compatibility - introduce intent to collapse flag
[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 use DBICTest;
7 use File::Compare;
8 use Path::Class qw/file/;
9
10 my $schema = DBICTest->init_schema();
11
12 plan tests => 10;
13
14 my $rs = $schema->resultset('FileColumn');
15 my $source_file = file(__FILE__);
16 my $fname = $source_file->basename;
17 my $fh = $source_file->open('r') or die "failed to open $source_file: $!\n";
18 my $fc = eval {
19     $rs->create({ file => { handle => $fh, filename => $fname } })
20 };
21 is ( $@, '', 'created' );
22
23 $fh->close;
24
25 my $storage = file(
26     $fc->column_info('file')->{file_column_path},
27     $fc->id,
28     $fc->file->{filename},
29 );
30 ok ( -e $storage, 'storage exists' );
31
32 # read it back
33 $fc = $rs->find({ id => $fc->id });
34
35 is ( $fc->file->{filename}, $fname, 'filename matches' );
36 ok ( compare($storage, $source_file) == 0, 'file contents matches' );
37
38 # update
39 my $new_fname = 'File.pm';
40 my $new_source_file = file(qw/lib DBIx Class InflateColumn File.pm/);
41 my $new_storage = file(
42     $fc->column_info('file')->{file_column_path},
43     $fc->id,
44     $new_fname,
45 );
46 $fh = $new_source_file->open('r') or die "failed to open $new_source_file: $!\n";
47
48 $fc->file({ handle => $fh, filename => $new_fname });
49 $fc->update;
50
51 TODO: {
52     local $TODO = 'design change required';
53     ok ( ! -e $storage, 'old storage does not exist' );
54 };
55
56 ok ( -e $new_storage, 'new storage exists' );
57
58 # read it back
59 $fc = $rs->find({ id => $fc->id });
60
61 is ( $fc->file->{filename}, $new_fname, 'new filname matches' );
62 ok ( compare($new_storage, $new_source_file) == 0, 'new content matches' );
63
64 $fc->delete;
65
66 ok ( ! -e $storage, 'storage deleted' );
67
68 $fh = $source_file->openr or die "failed to open $source_file: $!\n";
69 $fc = $rs->create({ file => { handle => $fh, filename => $fname } });
70
71 # read it back
72 $fc->discard_changes;
73
74 $storage = file(
75     $fc->column_info('file')->{file_column_path},
76     $fc->id,
77     $fc->file->{filename},
78 );
79
80 TODO: {
81     local $TODO = 'need resultset delete override to delete_all';
82     $rs->delete;
83     ok ( ! -e $storage, 'storage does not exist after $rs->delete' );
84 };