3cc66af136b225c8232def5b29fe7b42d80ea0a1
[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
8 BEGIN {
9     eval "use IO::AtomicFile";
10     plan skip_all => "IO::AtomicFile is required for this test" if $@;        
11     plan tests => 21;    
12     use_ok('MooseX::Storage');
13 }
14
15 {
16     package Foo;
17     use Moose;
18     use MooseX::Storage;
19     
20     with 'MooseX::Storage::Deferred';
21     
22     has 'number' => (is => 'ro', isa => 'Int');
23     has 'string' => (is => 'ro', isa => 'Str');
24     has 'float'  => (is => 'ro', isa => 'Num');        
25     has 'array'  => (is => 'ro', isa => 'ArrayRef');
26     has 'hash'   => (is => 'ro', isa => 'HashRef');    
27         has 'object' => (is => 'ro', isa => 'Object');    
28 }
29
30 my $file = 'temp.json';
31
32 {
33     my $foo = Foo->new(
34         number => 10,
35         string => 'foo',
36         float  => 10.5,
37         array  => [ 1 .. 10 ],
38         hash   => { map { $_ => undef } (1 .. 10) },
39         object => Foo->new( number => 2 ),
40     );
41     isa_ok($foo, 'Foo');
42
43     $foo->store($file, { format => 'JSON', io => 'File' });
44 }
45
46 {
47     my $foo = Foo->load($file, { format => 'JSON', io => 'File' });
48     isa_ok($foo, 'Foo');
49
50     is($foo->number, 10, '... got the right number');
51     is($foo->string, 'foo', '... got the right string');
52     is($foo->float, 10.5, '... got the right float');
53     is_deeply($foo->array, [ 1 .. 10], '... got the right array');
54     is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
55
56     isa_ok($foo->object, 'Foo');
57     is($foo->object->number, 2, '... got the right number (in the embedded object)');
58 }
59
60 unlink $file;
61 ok(!(-e $file), '... the file has been deleted');
62
63 {
64     my $foo = Foo->new(
65         number => 10,
66         string => 'foo',
67         float  => 10.5,
68         array  => [ 1 .. 10 ],
69         hash   => { map { $_ => undef } (1 .. 10) },
70         object => Foo->new( number => 2 ),
71     );
72     isa_ok($foo, 'Foo');
73
74     $foo->store($file, { format => 'JSON', io => 'AtomicFile' });
75 }
76
77 {
78     my $foo = Foo->load($file, { format => 'JSON', io => 'AtomicFile' });
79     isa_ok($foo, 'Foo');
80
81     is($foo->number, 10, '... got the right number');
82     is($foo->string, 'foo', '... got the right string');
83     is($foo->float, 10.5, '... got the right float');
84     is_deeply($foo->array, [ 1 .. 10], '... got the right array');
85     is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
86
87     isa_ok($foo->object, 'Foo');
88     is($foo->object->number, 2, '... got the right number (in the embedded object)');
89 }
90
91 unlink $file;
92 ok(!(-e $file), '... the file has been deleted');
93