b0a24ac1490f34996234ae24a01d1ca96d334a04
[gitmo/MooseX-Storage.git] / t / 061_basic_deferred_w_io.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Deep;
8 use File::Temp qw(tempdir);
9 use File::Spec::Functions;
10
11 my $dir = tempdir;
12
13 use Test::Requires {
14     'IO::AtomicFile' => 0.01, # skip all if not installed
15     'JSON::Any' => 0.01,
16 };
17
18 BEGIN {
19     plan tests => 20;
20     use_ok('MooseX::Storage');
21 }
22
23 {
24     package Foo;
25     use Moose;
26     use MooseX::Storage;
27     
28     with 'MooseX::Storage::Deferred';
29     
30     has 'number' => (is => 'ro', isa => 'Int');
31     has 'string' => (is => 'ro', isa => 'Str');
32     has 'float'  => (is => 'ro', isa => 'Num');        
33     has 'array'  => (is => 'ro', isa => 'ArrayRef');
34     has 'hash'   => (is => 'ro', isa => 'HashRef');    
35         has 'object' => (is => 'ro', isa => 'Object');    
36 }
37
38 my $file = catfile($dir, 'temp.json');
39
40 {
41     my $foo = Foo->new(
42         number => 10,
43         string => 'foo',
44         float  => 10.5,
45         array  => [ 1 .. 10 ],
46         hash   => { map { $_ => undef } (1 .. 10) },
47         object => Foo->new( number => 2 ),
48     );
49     isa_ok($foo, 'Foo');
50
51     $foo->store($file, { format => 'JSON', io => 'File' });
52 }
53
54 {
55     my $foo = Foo->load($file, { format => 'JSON', io => 'File' });
56     isa_ok($foo, 'Foo');
57
58     is($foo->number, 10, '... got the right number');
59     is($foo->string, 'foo', '... got the right string');
60     is($foo->float, 10.5, '... got the right float');
61     cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
62     cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
63
64     isa_ok($foo->object, 'Foo');
65     is($foo->object->number, 2, '... got the right number (in the embedded object)');
66 }
67
68 unlink $file;
69 ok(!(-e $file), '... the file has been deleted');
70
71 {
72     my $foo = Foo->new(
73         number => 10,
74         string => 'foo',
75         float  => 10.5,
76         array  => [ 1 .. 10 ],
77         hash   => { map { $_ => undef } (1 .. 10) },
78         object => Foo->new( number => 2 ),
79     );
80     isa_ok($foo, 'Foo');
81
82     $foo->store($file, { format => 'JSON', io => 'AtomicFile' });
83 }
84
85 {
86     my $foo = Foo->load($file, { format => 'JSON', io => 'AtomicFile' });
87     isa_ok($foo, 'Foo');
88
89     is($foo->number, 10, '... got the right number');
90     is($foo->string, 'foo', '... got the right string');
91     is($foo->float, 10.5, '... got the right float');
92     cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
93     cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
94
95     isa_ok($foo->object, 'Foo');
96     is($foo->object->number, 2, '... got the right number (in the embedded object)');
97 }
98
99