blob: 04ff11d7498ebc8c632925d8af0566d38ea71255 [file] [log] [blame]
#!/usr/bin/perl
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
use Usergrid::Client;
use IO::Socket::INET;
use Test::More;
# TEST DATA
my $hostname = 'localhost';
my $port = '8080';
my $api_url = "http://$hostname:$port";
my $organization = 'test-organization';
my $application = 'test-app';
my $username = 'testuser';
my $password = 'Testuser123$';
###########
if (_check_port($hostname, $port)) {
plan tests => 34;
} else {
plan skip_all => "server $api_url not reachable"
}
sub _check_port {
my ($hostname, $port) = @_;
new IO::Socket::INET ( PeerAddr => $hostname, PeerPort => $port,
Proto => 'tcp' ) || return 0;
return 1;
}
my ($user, $token, $book, $collection, $count);
# Create the client object that will be used for all subsequent requests
my $client = Usergrid::Client->new(
organization => $organization,
application => $application,
api_url => $api_url,
trace => 0
);
# Create a test user
$user = $client->add_entity("users", { username=>$username, password=>$password });
$token = $client->login($username, $password);
eval {
$collection = $client->get_collection("books");
ok ( $collection->count() == 0, "count must be initially zero" );
for (my $i = 0; $i < 30; $i++) {
$client->add_entity("books", { name => "book $i", index => $i });
}
$collection = $client->get_collection("books", 30);
ok ( $collection->count() == 30, "count must now be 30" );
$client->update_collection("books", { in_stock => 1 });
$collection = $client->get_collection("books", 30);
while ($collection->has_next_entity()) {
$book = $collection->get_next_entity();
ok ( $book->get('in_stock') == 1 );
}
$client->update_collection("books", { in_stock => 0 }, "select * where index = '1' or index = '2' or index = '3' or index = '4' or index = '5'");
$collection = $client->get_collection("books", 30);
while ($collection->has_next_entity()) {
$book = $collection->get_next_entity();
$count++ if ($book->get('index') =~ /[12345]/ && $book->get('in_stock') == 0);
$client->delete_entity($book);
}
ok ( $count == 5, "batch update only 5 entities" );
$collection = $client->get_collection("books");
ok ( $collection->count() == 0, "count must now be again zero" );
};
diag($@) if $@;
# Cleanup
$collection = $client->delete_collection("books", undef, 30);
$client->delete_entity($user);