Amazon-DynamoDB
view release on metacpan or search on metacpan
t/03-simple-get-put.t view on Meta::CPAN
#!perl
use strict;
use warnings;
use lib ('lib', './t');
use Test::Most;
use Test::Differences;
use TestSettings;
use Data::Dumper;
unless ( $ENV{'AMAZON_DYNAMODB_EXPENSIVE_TESTS'} ) {
plan skip_all => 'Testing this module for real costs money.';
} else {
plan tests => 84;
}
bail_on_fail;
my $ddb = TestSettings::get_ddb();
my $table_name = TestSettings::random_table_name();
my $create = $ddb->create_table(TableName => $table_name,
ReadCapacityUnits => 2,
WriteCapacityUnits => 2,
AttributeDefinitions => {
user_id => 'N',
},
KeySchema => ['user_id'],
);
ok($create->is_done, "Create request was completed");
my $wait = $ddb->wait_for_table_status(TableName => $table_name);
ok($wait->is_done, "Created table is ready");
my $source_data = {
email => 'test@example.com',
user_id => 1,
name => 'Rusty Conover',
names => ['R. Conover', 'Rusty Conover'],
numbers => [1, 3, 5, 5.002],
numbers_and_strings => [1, 3, 5, "5.002.3.2.3"],
test_binary => \'Rusty Conover',
test_binary_array => [\'rusty', \'conover'],
};
{
my $put = $ddb->put_item(TableName => $table_name,
Item => $source_data);
ok($put->is_done, "put_item completed successfully");
is_deeply($put->get(), {}, "Results of put_item with no attributes returned didn't return any.");
}
{
my $put = $ddb->put_item(TableName => $table_name,
ReturnItemCollectionMetrics => 'SIZE',
Item => $source_data);
ok($put->is_done, "put_item completed successfully");
is_deeply($put->get(), {}, "Results of put_item with no attributes returned didn't return any.");
}
{
my $found_item;
my $get = $ddb->get_item(
sub {
$found_item = shift;
},
TableName => $table_name,
Key => {
user_id => $source_data->{user_id}
});
ok($get->is_done, "get_item completed ok");
ok(defined($found_item), "an item was retrieved");
is($found_item->{email}, $source_data->{email},"Email matches");
is($found_item->{name}, $source_data->{name}, "Name matches");
is(${$found_item->{test_binary}}, ${$source_data->{test_binary}}, "Binary string matches");
is($found_item->{user_id}, $source_data->{user_id}, "User id matches");
eq_or_diff([sort @{$source_data->{numbers}}],
[sort @{$found_item->{numbers}}], "Number array matches");
eq_or_diff([sort map { $$_ } @{$source_data->{test_binary_array}}],
[sort map { $$_ } @{$found_item->{test_binary_array}}], "Binary array matches");
eq_or_diff([sort @{$source_data->{numbers_and_strings}}],
[sort @{$found_item->{numbers_and_strings}}], "Numbers and strings in array match");
}
{
my $delete = $ddb->delete_item(
TableName => $table_name,
Key => {
user_id => $source_data->{user_id}
});
ok($delete->is_done, "delete_item completed ok");
is_deeply($delete->get, {}, "Delete result is empty with no requested attributes");
}
{
my $found_item;
my $get = $ddb->get_item(
sub {
$found_item = shift;
},
TableName => $table_name,
Key => {
user_id => $source_data->{user_id}
});
ok($get->is_done, "get_item completed ok:" . Data::Dumper->Dump([$get]));
ok(!defined($found_item), "an item was not retrieved");
}
# test delete with retrieving the attributes
{
my $put = $ddb->put_item(TableName => $table_name,
Item => $source_data);
ok($put->is_done, "put_item completed successfully");
is_deeply($put->get(), {}, "Results of put_item with no attributes returned didn't return any.");
}
{
my $found_item;
my $get = $ddb->get_item(
sub {
$found_item = shift;
},
TableName => $table_name,
Key => {
user_id => $source_data->{user_id}
});
ok($get->is_done, "get_item completed ok");
ok(defined($found_item), "an item was retrieved");
is($found_item->{email}, $source_data->{email},"Email matches");
is($found_item->{name}, $source_data->{name}, "Name matches");
is(${$found_item->{test_binary}}, ${$source_data->{test_binary}}, "Binary string matches");
is($found_item->{user_id}, $source_data->{user_id}, "User id matches");
eq_or_diff([sort @{$source_data->{numbers}}],
[sort @{$found_item->{numbers}}], "Number array matches");
eq_or_diff([sort map { $$_ } @{$source_data->{test_binary_array}}],
[sort map { $$_ } @{$found_item->{test_binary_array}}], "Binary array matches");
eq_or_diff([sort @{$source_data->{numbers_and_strings}}],
[sort @{$found_item->{numbers_and_strings}}], "Numbers and strings in array match");
}
{
my $delete = $ddb->delete_item(
TableName => $table_name,
Key => {
user_id => $source_data->{user_id}
},
ReturnValues => 'ALL_OLD'
);
ok($delete->is_done, "delete_item completed ok");
my $found_item = $delete->get()->{Attributes};
ok(defined($found_item), "Got attributes form delete_item");
is($found_item->{email}, $source_data->{email},"Email matches");
is($found_item->{name}, $source_data->{name}, "Name matches");
is(${$found_item->{test_binary}}, ${$source_data->{test_binary}}, "Binary string matches");
is($found_item->{user_id}, $source_data->{user_id}, "User id matches");
( run in 1.648 second using v1.01-cache-2.11-cpan-98e64b0badf )