Commit | Line | Data |
4740bdb7 |
1 | use strict; |
3814fdad |
2 | use warnings; |
4740bdb7 |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
823c91a1 |
6 | |
823c91a1 |
7 | |
4740bdb7 |
8 | use DBICTest; |
123df526 |
9 | use DBICTest::Schema; |
26ddc864 |
10 | use File::Compare; |
11 | use Path::Class qw/file/; |
4740bdb7 |
12 | |
123df526 |
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 | |
823c91a1 |
41 | my $schema = DBICTest->init_schema; |
4740bdb7 |
42 | |
5d8d8611 |
43 | plan tests => 10; |
4740bdb7 |
44 | |
8b40ac1a |
45 | if (not $ENV{DBICTEST_SQLT_DEPLOY}) { |
46 | $schema->storage->dbh->do(<<'EOF'); |
47 | CREATE TABLE file_columns ( |
48 | id INTEGER PRIMARY KEY, |
49 | file VARCHAR(255) |
50 | ) |
51 | EOF |
52 | } |
53 | |
26ddc864 |
54 | my $rs = $schema->resultset('FileColumn'); |
100fd57f |
55 | my $source_file = file(__FILE__); |
56 | my $fname = $source_file->basename; |
26ddc864 |
57 | my $fh = $source_file->open('r') or die "failed to open $source_file: $!\n"; |
58 | my $fc = eval { |
59 | $rs->create({ file => { handle => $fh, filename => $fname } }) |
60 | }; |
61 | is ( $@, '', 'created' ); |
62 | |
63 | $fh->close; |
64 | |
65 | my $storage = file( |
66 | $fc->column_info('file')->{file_column_path}, |
67 | $fc->id, |
68 | $fc->file->{filename}, |
69 | ); |
70 | ok ( -e $storage, 'storage exists' ); |
71 | |
72 | # read it back |
73 | $fc = $rs->find({ id => $fc->id }); |
74 | |
75 | is ( $fc->file->{filename}, $fname, 'filename matches' ); |
76 | ok ( compare($storage, $source_file) == 0, 'file contents matches' ); |
77 | |
78 | # update |
79 | my $new_fname = 'File.pm'; |
80 | my $new_source_file = file(qw/lib DBIx Class InflateColumn File.pm/); |
81 | my $new_storage = file( |
82 | $fc->column_info('file')->{file_column_path}, |
83 | $fc->id, |
84 | $new_fname, |
85 | ); |
86 | $fh = $new_source_file->open('r') or die "failed to open $new_source_file: $!\n"; |
87 | |
88 | $fc->file({ handle => $fh, filename => $new_fname }); |
89 | $fc->update; |
90 | |
4ca1fd6f |
91 | { |
26ddc864 |
92 | local $TODO = 'design change required'; |
93 | ok ( ! -e $storage, 'old storage does not exist' ); |
94 | }; |
95 | |
96 | ok ( -e $new_storage, 'new storage exists' ); |
97 | |
98 | # read it back |
99 | $fc = $rs->find({ id => $fc->id }); |
100 | |
101 | is ( $fc->file->{filename}, $new_fname, 'new filname matches' ); |
102 | ok ( compare($new_storage, $new_source_file) == 0, 'new content matches' ); |
103 | |
9fb04755 |
104 | if ($^O eq 'MSWin32') { |
105 | close $fc->file->{handle}; # can't delete open files on Win32 |
1239d14e |
106 | } |
26ddc864 |
107 | $fc->delete; |
108 | |
109 | ok ( ! -e $storage, 'storage deleted' ); |
30a93e27 |
110 | |
5d8d8611 |
111 | $fh = $source_file->openr or die "failed to open $source_file: $!\n"; |
112 | $fc = $rs->create({ file => { handle => $fh, filename => $fname } }); |
30a93e27 |
113 | |
5d8d8611 |
114 | # read it back |
115 | $fc->discard_changes; |
30a93e27 |
116 | |
5d8d8611 |
117 | $storage = file( |
118 | $fc->column_info('file')->{file_column_path}, |
119 | $fc->id, |
120 | $fc->file->{filename}, |
121 | ); |
30a93e27 |
122 | |
4ca1fd6f |
123 | { |
5d8d8611 |
124 | local $TODO = 'need resultset delete override to delete_all'; |
30a93e27 |
125 | $rs->delete; |
126 | ok ( ! -e $storage, 'storage does not exist after $rs->delete' ); |
4ca1fd6f |
127 | } |