File: file_column.t

package info (click to toggle)
libdbix-class-perl 0.082844-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (127 lines) | stat: -rw-r--r-- 2,948 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use strict;
use warnings;

use Test::More;
use lib qw(t/lib);


use DBICTest;
use DBICTest::Schema;
use File::Compare;
use Path::Class qw/file/;

{
  local $ENV{DBIC_IC_FILE_NOWARN} = 1;

  package DBICTest::Schema::FileColumn;

  use strict;
  use warnings;
  use base qw/DBICTest::BaseResult/;

  use File::Temp qw/tempdir/;

  __PACKAGE__->load_components (qw/InflateColumn::File/);
  __PACKAGE__->table('file_columns');

  __PACKAGE__->add_columns(
    id => { data_type => 'integer', is_auto_increment => 1 },
    file => {
      data_type        => 'varchar',
      is_file_column   => 1,
      file_column_path => tempdir(CLEANUP => 1),
      size             => 255
    }
  );

  __PACKAGE__->set_primary_key('id');
}
DBICTest::Schema->load_classes('FileColumn');

my $schema = DBICTest->init_schema;

plan tests => 10;

if (not $ENV{DBICTEST_SQLT_DEPLOY}) {
  $schema->storage->dbh->do(<<'EOF');
  CREATE TABLE file_columns (
    id INTEGER PRIMARY KEY,
    file VARCHAR(255)
  )
EOF
}

my $rs = $schema->resultset('FileColumn');
my $source_file = file(__FILE__);
my $fname = $source_file->basename;
my $fh = $source_file->open('r') or die "failed to open $source_file: $!\n";
my $fc = eval {
    $rs->create({ file => { handle => $fh, filename => $fname } })
};
is ( $@, '', 'created' );

$fh->close;

my $storage = file(
    $fc->column_info('file')->{file_column_path},
    $fc->id,
    $fc->file->{filename},
);
ok ( -e $storage, 'storage exists' );

# read it back
$fc = $rs->find({ id => $fc->id });

is ( $fc->file->{filename}, $fname, 'filename matches' );
ok ( compare($storage, $source_file) == 0, 'file contents matches' );

# update
my $new_fname = 'File.pm';
my $new_source_file = file(qw/lib DBIx Class InflateColumn File.pm/);
my $new_storage = file(
    $fc->column_info('file')->{file_column_path},
    $fc->id,
    $new_fname,
);
$fh = $new_source_file->open('r') or die "failed to open $new_source_file: $!\n";

$fc->file({ handle => $fh, filename => $new_fname });
$fc->update;

{
    local $TODO = 'design change required';
    ok ( ! -e $storage, 'old storage does not exist' );
};

ok ( -e $new_storage, 'new storage exists' );

# read it back
$fc = $rs->find({ id => $fc->id });

is ( $fc->file->{filename}, $new_fname, 'new filname matches' );
ok ( compare($new_storage, $new_source_file) == 0, 'new content matches' );

if ($^O eq 'MSWin32') {
  close $fc->file->{handle}; # can't delete open files on Win32
}
$fc->delete;

ok ( ! -e $storage, 'storage deleted' );

$fh = $source_file->openr or die "failed to open $source_file: $!\n";
$fc = $rs->create({ file => { handle => $fh, filename => $fname } });

# read it back
$fc->discard_changes;

$storage = file(
    $fc->column_info('file')->{file_column_path},
    $fc->id,
    $fc->file->{filename},
);

{
    local $TODO = 'need resultset delete override to delete_all';
    $rs->delete;
    ok ( ! -e $storage, 'storage does not exist after $rs->delete' );
}