AnyMongo

 view release on metacpan or  search on metacpan

t/perl-driver-api/collection.t  view on Meta::CPAN

@indexes = $coll->get_indexes;
is(scalar @indexes, 3);
is_deeply(
    [sort keys %{ $indexes[1]->{key} }],
    [sort qw/foo bar/],
);

$coll->drop;
ok(!$coll->get_indexes, 'no indexes after dropping');

# make sure this still works
$coll->ensure_index({"foo" => 1});
@indexes = $coll->get_indexes;
is(scalar @indexes, 2, '1 custom index and the default _id_ index');
$coll->drop;

# test new form of ensure index
{
    $ok = $coll->ensure_index({foo => 1, bar => -1, baz => 1});
    ok($ok);
    $ok = $coll->ensure_index({foo => 1, bar => 1});
    ok($ok);
    $coll->insert({foo => 1, bar => 1, baz => 1, boo => 1});
    $coll->insert({foo => 1, bar => 1, baz => 1, boo => 2});
    is($coll->count, 2);
    
    # unique index
    $coll->ensure_index({boo => 1}, {unique => 1});
    $coll->insert({foo => 3, bar => 3, baz => 3, boo => 2});
    is($coll->count, 2, 'unique index');
}
$coll->drop;


# test doubles
my $pi = 3.14159265;
ok($id = $coll->insert({ data => 'pi', pi => $pi }), "inserting float number value");
ok($obj = $coll->find_one({ data => 'pi' }));
is($obj->{pi}, $pi);

$coll->drop;
my $object = {};
$object->{'autoPartNum'} = '123456';
$object->{'price'} = 123.19;
$coll->insert($object);
my $auto = $coll->find_one;
ok(is_float($auto->{'price'}));
is($auto->{'price'}, $object->{'price'});

# test undefined values
ok($id  = $coll->insert({ data => 'null', none => undef }), 'inserting undefined data');
ok($obj = $coll->find_one({ data => 'null' }), 'finding undefined row');
ok(exists $obj->{none}, 'got null field');
ok(!defined $obj->{none}, 'null field is undefined');

$coll->drop;

# ord("\x9F") is 159
$coll->insert({foo => "\x9F" });
my $utfblah = $coll->find_one;
is(ord($utfblah->{'foo'}), 159, 'translate non-utf8 to utf8 char');

$coll->drop;
$coll->insert({"\x9F" => "hi"});
$utfblah = $coll->find_one;
is($utfblah->{chr(159)}, "hi", 'translate non-utf8 key');


$coll->drop;
my $keys = tie(my %idx, 'Tie::IxHash');
%idx = ('sn' => 1, 'ts' => -1);

$coll->ensure_index($keys);

my @tied = $coll->get_indexes;
is(scalar @tied, 2, 'num indexes');
is($tied[1]->{'ns'}, 'test_database.test_collection', 'namespace');
is($tied[1]->{'name'}, 'sn_1_ts_-1', 'namespace');

$coll->drop;

$coll->insert({x => 1, y => 2, z => 3, w => 4});
my $cursor = $coll->query->fields({'y' => 1});
$obj = $cursor->next;
is(exists $obj->{'y'}, 1, 'y exists');
is(exists $obj->{'_id'}, 1, '_id exists');
is(exists $obj->{'x'}, '', 'x doesn\'t exist');
is(exists $obj->{'z'}, '', 'z doesn\'t exist');
is(exists $obj->{'w'}, '', 'w doesn\'t exist');

# batch insert
$coll->drop;
my $ids = $coll->batch_insert([{'x' => 1}, {'x' => 2}, {'x' => 3}]);
is($coll->count, 3, 'batch_insert');

$cursor = $coll->query->sort({'x' => 1});
my $i = 1;
while ($obj = $cursor->next) {
    is($obj->{'x'}, $i++);
}

# find_one fields
$coll->drop;
$coll->insert({'x' => 1, 'y' => 2, 'z' => 3});
my $yer = $coll->find_one({}, {'y' => 1});

ok(exists $yer->{'y'}, 'y exists');
ok(!exists $yer->{'x'}, 'x doesn\'t');
ok(!exists $yer->{'z'}, 'z doesn\'t');

$coll->drop;
$coll->batch_insert([{"x" => 1}, {"x" => 1}, {"x" => 1}]);
$coll->remove({"x" => 1}, 1);
is ($coll->count, 2, 'remove just one');

# tie::ixhash for update/insert
$coll->drop;
my $hash = Tie::IxHash->new("f" => 1, "s" => 2, "fo" => 4, "t" => 3);
$id = $coll->insert($hash);
isa_ok($id, 'MongoDB::OID');
my $tied = $coll->find_one;
is($tied->{'_id'}."", "$id");
is($tied->{'f'}, 1);
is($tied->{'s'}, 2);
is($tied->{'fo'}, 4);
is($tied->{'t'}, 3);

t/perl-driver-api/collection.t  view on Meta::CPAN

    $coll->insert({x=>1});
    $ok = $coll->update({}, {'$inc' => {x => 1}});
    is($ok, 1);

    $ok = $coll->update({}, {'$inc' => {x => 2}}, {safe => 1});
    is($ok, 1);
}

# save
{
    $coll->drop;

    my $x = {"hello" => "world"};
    $coll->save($x);
    is($coll->count, 1, 'save');

    my $y = $coll->find_one;
    $y->{"hello"} = 3;
    $coll->save($y);
    is($coll->count, 1);

    my $z = $coll->find_one;
    is($z->{"hello"}, 3);

    my $syscoll = $db->get_collection('system.indexes');
    eval {
        $ok = $syscoll->save({_id => 'foo'}, {safe => 1});
    };

    ok($@ && $@ =~ 'cannot update system collection');
}

# find
{
    $coll->drop;

    $coll->insert({x => 1});
    $coll->insert({x => 4});
    $coll->insert({x => 5});

    my $cursor = $coll->find({x=>4});
    my $result = $cursor->next;
    is($result->{'x'}, 4, 'find');

    $cursor = $coll->find({x=>{'$gt' => 1}})->sort({x => -1});
    $result = $cursor->next;
    is($result->{'x'}, 5);
    $result = $cursor->next;
    is($result->{'x'}, 4);
}

# # autoload
SKIP: {
    skip 'Autoload not support anymore, skip',2 if 1;
    my $coll1 = $conn->foo->bar->baz;
    is($coll1->name, "bar.baz");
    is($coll1->full_name, "foo.bar.baz");
}

# ns hack
# check insert utf8
{
    my $coll = $db->get_collection('test_collection');
    $coll->drop;
    # turn off utf8 flag now
    $MongoDB::BSON::utf8_flag_on = 0;
    $coll->insert({ foo => "\x{4e2d}\x{56fd}"});
    $utfblah = $coll->find_one;
    $coll->drop;
    $coll->insert({foo2 => $utfblah->{foo}});
    $utfblah = $coll->find_one;
    # use utf8;
    my $utfv2 = encode('utf8',"\x{4e2d}\x{56fd}");
    # my $utfv2 = encode('utf8',"中国");
    # diag(Dumper(\$utfv2));
    is($utfblah->{foo2},$utfv2,'turn utf8 flag off,return perl internal form(bytes)');
    # restore;
    $MongoDB::BSON::utf8_flag_on = 1;
    $coll->drop;
}


END {
    if ($db) {
        $db->drop;
    }
}



( run in 0.613 second using v1.01-cache-2.11-cpan-39bf76dae61 )