Merge 'trunk' into 'storage-interbase'
[dbsrgits/DBIx-Class.git] / t / inflate / file_column.t
CommitLineData
4740bdb7 1use strict;
3814fdad 2use warnings;
4740bdb7 3
4use Test::More;
5use lib qw(t/lib);
823c91a1 6
7# inject IC::File into the result baseclass for testing
8BEGIN {
9 $ENV{DBIC_IC_FILE_NOWARN} = 1;
10 require DBICTest::BaseResult;
11 DBICTest::BaseResult->load_components (qw/InflateColumn::File/);
12}
13
14
4740bdb7 15use DBICTest;
26ddc864 16use File::Compare;
17use Path::Class qw/file/;
4740bdb7 18
823c91a1 19my $schema = DBICTest->init_schema;
4740bdb7 20
5d8d8611 21plan tests => 10;
4740bdb7 22
26ddc864 23my $rs = $schema->resultset('FileColumn');
100fd57f 24my $source_file = file(__FILE__);
25my $fname = $source_file->basename;
26ddc864 26my $fh = $source_file->open('r') or die "failed to open $source_file: $!\n";
27my $fc = eval {
28 $rs->create({ file => { handle => $fh, filename => $fname } })
29};
30is ( $@, '', 'created' );
31
32$fh->close;
33
34my $storage = file(
35 $fc->column_info('file')->{file_column_path},
36 $fc->id,
37 $fc->file->{filename},
38);
39ok ( -e $storage, 'storage exists' );
40
41# read it back
42$fc = $rs->find({ id => $fc->id });
43
44is ( $fc->file->{filename}, $fname, 'filename matches' );
45ok ( compare($storage, $source_file) == 0, 'file contents matches' );
46
47# update
48my $new_fname = 'File.pm';
49my $new_source_file = file(qw/lib DBIx Class InflateColumn File.pm/);
50my $new_storage = file(
51 $fc->column_info('file')->{file_column_path},
52 $fc->id,
53 $new_fname,
54);
55$fh = $new_source_file->open('r') or die "failed to open $new_source_file: $!\n";
56
57$fc->file({ handle => $fh, filename => $new_fname });
58$fc->update;
59
60TODO: {
61 local $TODO = 'design change required';
62 ok ( ! -e $storage, 'old storage does not exist' );
63};
64
65ok ( -e $new_storage, 'new storage exists' );
66
67# read it back
68$fc = $rs->find({ id => $fc->id });
69
70is ( $fc->file->{filename}, $new_fname, 'new filname matches' );
71ok ( compare($new_storage, $new_source_file) == 0, 'new content matches' );
72
73$fc->delete;
74
75ok ( ! -e $storage, 'storage deleted' );
30a93e27 76
5d8d8611 77$fh = $source_file->openr or die "failed to open $source_file: $!\n";
78$fc = $rs->create({ file => { handle => $fh, filename => $fname } });
30a93e27 79
5d8d8611 80# read it back
81$fc->discard_changes;
30a93e27 82
5d8d8611 83$storage = file(
84 $fc->column_info('file')->{file_column_path},
85 $fc->id,
86 $fc->file->{filename},
87);
30a93e27 88
5d8d8611 89TODO: {
90 local $TODO = 'need resultset delete override to delete_all';
30a93e27 91 $rs->delete;
92 ok ( ! -e $storage, 'storage does not exist after $rs->delete' );
93};