File: Catmandu-Plugin-Datestamps.t

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (93 lines) | stat: -rw-r--r-- 2,738 bytes parent folder | download
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
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;
use Catmandu::Store::Hash;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::Plugin::Datestamps';
    use_ok $pkg;
}
require_ok $pkg;

my $store = Catmandu::Store::Hash->new(
    bags => {data => {plugins => [qw(Datestamps)]}});

ok $store->does('Catmandu::Store'),
    'create Catmandu-Store with Datestamps plugin';
ok $store->bag->add({_id => '001', name => 'Penguin'}), 'store something';
ok $store->bag->get('001'),                             'get 001';
ok $store->bag->get('001')->{date_created},             'has date_created';
ok $store->bag->get('001')->{date_updated},             'has date_updated';

my $created = $store->bag->get('001')->{date_created};
my $updated = $store->bag->get('001')->{date_updated};
my $rec     = $store->bag->get('001');
$rec->{name} = 'John';

sleep 2;

ok $store->bag->add($rec), 'update something';
$rec = $store->bag->get('001');
ok $rec->{date_updated},             'has date_updated';
ok $rec->{date_updated} ne $updated, 'dates change';
is $rec->{date_created}, $created, 'but created dates dont change';

# formats
like $rec->{date_created}, qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/;
like $rec->{date_updated}, qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/;

$store = Catmandu::Store::Hash->new(
    bags => {
        data => {
            plugins          => [qw(Datestamps)],
            datestamp_format => 'iso_date_time'
        }
    }
);
$store->bag->add({_id => '001', name => 'Penguin'});
$rec = $store->bag->get('001');
like $rec->{date_created}, qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/;

$store = Catmandu::Store::Hash->new(
    bags => {
        data => {
            plugins          => [qw(Datestamps)],
            datestamp_format => 'iso_date_time_millis'
        }
    }
);
$store->bag->add({_id => '001', name => 'Penguin'});
$rec = $store->bag->get('001');
like $rec->{date_created}, qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/;

$store = Catmandu::Store::Hash->new(
    bags => {
        data => {plugins => [qw(Datestamps)], datestamp_format => '%Y/%m/%d'}
    }
);
$store->bag->add({_id => '001', name => 'Penguin'});
$rec = $store->bag->get('001');
like $rec->{date_created}, qr/^\d{4}\/\d{2}\/\d{2}/;

#fields
$store = Catmandu::Store::Hash->new(
    bags => {
        data => {
            plugins                 => [qw(Datestamps)],
            datestamp_created_field => 'created',
            datestamp_updated_field => 'updated'
        }
    }
);
$store->bag->add({_id => '001', name => 'Penguin'});
$rec = $store->bag->get('001');
like $rec->{created}, qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/;
like $rec->{updated}, qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/;

done_testing;