root/lang/perl/DBIx-CouchLike/trunk/t/01_crud.t @ 33585

Revision 33585, 2.1 kB (checked in by fujiwara, 6 years ago)

all(), all_designs() 追加

Line 
1# -*- mode:perl -*-
2use strict;
3use Test::More qw/ no_plan /;
4use Test::Exception;
5BEGIN { use_ok 'DBIx::CouchLike' }
6
7my $dbh = require 't/connect.pl';
8ok $dbh;
9
10my $couch = DBIx::CouchLike->new({ dbh => $dbh, table => "foo" });
11isa_ok $couch => "DBIx::CouchLike";
12ok $couch->can('dbh');
13is $couch->dbh => $dbh;
14ok $couch->dbh->ping;
15
16is $couch->table => "foo";
17ok $couch->create_table;
18
19ok !$couch->{id_generator}; # lazy
20ok $couch->id_generator;
21ok $couch->id_generator->get_id;
22
23my $id = $couch->post({ foo => 1, bar => 2 });
24ok $id;
25
26my $obj = $couch->get($id);
27ok $obj;
28is_deeply $obj => { foo => 1, bar => 2, _id => $id };
29
30ok $couch->put( $id, { foo => 2, bar => 3, baz => 4 } );
31$obj = $couch->get($id);
32ok $obj;
33is_deeply $obj => { foo => 2, bar => 3, baz => 4, _id => $id };
34
35$obj->{foo} = "FOO";
36$obj->{bar} = "BAR";
37delete $obj->{baz};
38ok $couch->put($obj);
39ok $obj = $couch->get($id);
40is_deeply $obj => { foo => "FOO", bar => "BAR", _id => $id };
41
42ok $couch->delete($id);
43ok ! $couch->get($id);
44
45ok $couch->post( "foo" => { a => "AAA" } );
46throws_ok {
47    $couch->post( "foo" => { a => "AAA" } )
48} qr/(?:duplicate key value|is not unique)/;
49
50
51# post with id
52$id = $couch->post({ _id => "9999", foo => 1, bar => 2 });
53is $id => "9999";
54$obj = $couch->get("9999");
55is_deeply $obj => { foo => 1, bar => 2, _id => "9999" };
56
57# put with id (not exists)
58$id = $couch->put({ _id => 1234, foo => 9999 });
59is $id => 1234;
60$obj = $couch->get(1234);
61is_deeply $obj => { _id => 1234, foo => 9999 };
62
63my @all = $couch->all;
64is_deeply \@all => [
65    { 'value' => { 'foo' => 9999 }, 'id' => '1234' },
66    { 'value' => { 'bar' => 2, 'foo' => 1 }, 'id' => '9999' },
67    { 'value' => { 'a' => 'AAA' }, id => 'foo' },
68];
69
70@all = $couch->all({ limit => 1 });
71is_deeply \@all => [
72    { 'value' => { 'foo' => 9999 }, 'id' => '1234' },
73];
74
75@all = $couch->all({ offset => 1, limit => 1 });
76is_deeply \@all => [
77    { 'value' => { 'bar' => 2, 'foo' => 1 }, 'id' => '9999' },
78];
79
80@all = $couch->all({ id_like => "1%" });
81is_deeply \@all => [
82    { 'value' => { 'foo' => 9999 }, 'id' => '1234' },
83];
84
85
86$dbh->commit unless $ENV{DSN};
87$dbh->disconnect;
Note: See TracBrowser for help on using the browser.