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