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