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