Changeset 19718

Show
Ignore:
Timestamp:
09/22/08 12:04:47 (5 years ago)
Author:
masaki
Message:

bulk_docs のテスト追加

Location:
lang/perl/CouchDB-Object/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/CouchDB-Object/trunk/lib/CouchDB/Object/Database.pm

    r19688 r19718  
    9292 
    9393sub bulk_docs { 
    94     my ($self, @docs) = @_; 
     94    my ($self, $docs) = @_; 
    9595 
    96     my $body = sprintf '{ "docs": [%s] }', join ',', map { $_->to_json } @docs; 
     96    my $body = { docs => [ map { $_->to_hash } @$docs ] }; 
     97    $body = CouchDB::Object::JSON->encode($body); 
     98 
    9799    my $header = HTTP::Headers->new('Content-Type' => 'application/json'); 
    98100    my $res = $self->request(POST => $self->uri_for('_bulk_docs'), $header, $body); 
     
    100102    # merge 
    101103    if ($res->is_success) { 
    102         my $ea = each_array(@docs, @{ $res->content->{new_revs} }); 
     104        my $ea = each_array(@$docs, @{ $res->content->{new_revs} }); 
    103105        while (my ($doc, $content) = $ea->()) { 
    104106            $doc->id($content->{id})   if exists $content->{id}; 
  • lang/perl/CouchDB-Object/trunk/lib/CouchDB/Object/Document.pm

    r19447 r19718  
    4444sub to_json { 
    4545    my $self = shift; 
     46    return CouchDB::Object::JSON->encode($self->to_hash); 
     47} 
     48 
     49sub to_hash { 
     50    my $self = shift; 
    4651 
    4752    my $hash = {}; 
     
    4954    $hash->{_rev} = $self->rev if $self->has_rev; 
    5055 
    51     $hash = Hash::Merge::merge({%{ $self->_fields }}, $hash); 
    52     return CouchDB::Object::JSON->encode($hash); 
     56    return Hash::Merge::merge({%{ $self->_fields }}, $hash); 
    5357} 
    5458 
  • lang/perl/CouchDB-Object/trunk/t/04_documents.t

    r19688 r19718  
    99} 
    1010else { 
    11     plan tests => 287; 
     11    plan tests => 316; 
    1212} 
    1313 
     
    1616END { $db->drop if $couch->ping } 
    1717 
    18 my $json = read_json('t/docs.json'); 
    19 my @docs = map { CouchDB::Object::Document->new_from_json($_) } @$json; 
    20  
    2118# /{dbname}/_bulk_docs (bulk_docs) 
    22 { # 3 
    23     my $res = $db->bulk_docs(@docs); 
     19{ # 32 
     20    my $json = read_json('t/docs.json'); 
     21    my @docs = map { CouchDB::Object::Document->new_from_json($_) } @$json; 
     22    my $res = $db->bulk_docs(\@docs); 
    2423    ok $res->is_success; 
    2524    ok $res->content->{ok}; 
    26     ok $res->content->{new_revs}; 
     25 
     26    for my $doc (@docs) { # 1 x 30 = 30 
     27        ok $doc->has_rev; 
     28    } 
    2729} 
    2830