From 03ae5b9081010c90782d461dae8d5dc57465e24b Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Sat, 19 Nov 2016 21:12:04 +0200 Subject: [PATCH] #85 MkRegion more fixes --- .../java/com/jcabi/dynamo/mock/H2Data.java | 2 +- .../java/com/jcabi/dynamo/mock/MkItem.java | 12 +++--- .../com/jcabi/dynamo/mock/MkRegionTest.java | 38 ++++++++++++++++++- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/jcabi/dynamo/mock/H2Data.java b/src/main/java/com/jcabi/dynamo/mock/H2Data.java index 33f6d6c..599dfc9 100644 --- a/src/main/java/com/jcabi/dynamo/mock/H2Data.java +++ b/src/main/java/com/jcabi/dynamo/mock/H2Data.java @@ -213,7 +213,7 @@ public String apply(final String input) { ) ); } - return matcher.group(1); + return matcher.group(1).toLowerCase(Locale.ENGLISH); } } ); diff --git a/src/main/java/com/jcabi/dynamo/mock/MkItem.java b/src/main/java/com/jcabi/dynamo/mock/MkItem.java index 20b039a..18b26b1 100644 --- a/src/main/java/com/jcabi/dynamo/mock/MkItem.java +++ b/src/main/java/com/jcabi/dynamo/mock/MkItem.java @@ -110,7 +110,8 @@ public boolean has(final String name) throws IOException { @Override public Map put( - final String name, final AttributeValueUpdate value) { + final String name, final AttributeValueUpdate value) + throws IOException { return this.put( new ImmutableMap.Builder() .put(name, value).build() @@ -119,12 +120,11 @@ public Map put( @Override public Map put( - final Map attrs) { + final Map attrs) throws IOException { final Map keys = - new HashMap(); - keys.putAll(this.attributes); - for (final String attr : attrs.keySet()) { - keys.remove(attr); + new HashMap(0); + for (final String attr : this.data.keys(this.table)) { + keys.put(attr, this.attributes.get(attr)); } try { this.data.update( diff --git a/src/test/java/com/jcabi/dynamo/mock/MkRegionTest.java b/src/test/java/com/jcabi/dynamo/mock/MkRegionTest.java index 643ed54..3213116 100644 --- a/src/test/java/com/jcabi/dynamo/mock/MkRegionTest.java +++ b/src/test/java/com/jcabi/dynamo/mock/MkRegionTest.java @@ -29,6 +29,7 @@ */ package com.jcabi.dynamo.mock; +import com.amazonaws.services.dynamodbv2.model.AttributeAction; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate; import com.jcabi.dynamo.Attributes; @@ -44,6 +45,7 @@ * @author Yegor Bugayenko (yegor@tpc2.com) * @version $Id$ * @since 0.10 + * @checkstyle MultipleStringLiteralsCheck (500 lines) */ public final class MkRegionTest { @@ -56,14 +58,16 @@ public void storesAndReadsAttributes() throws Exception { final String name = "users"; final String key = "id"; final String attr = "description"; + final String nattr = "thenumber"; final Region region = new MkRegion( - new H2Data().with(name, new String[] {key}, attr) + new H2Data().with(name, new String[] {key}, attr, nattr) ); final Table table = region.table(name); table.put( new Attributes() .with(key, "32443") .with(attr, "first value to \n\t€ save") + .with(nattr, "150") ); final Item item = table.frame().iterator().next(); MatcherAssert.assertThat(item.has(attr), Matchers.is(true)); @@ -81,6 +85,38 @@ public void storesAndReadsAttributes() throws Exception { item.get(attr).getS(), Matchers.containsString("another value") ); + MatcherAssert.assertThat( + item.get(nattr).getN(), + Matchers.endsWith("50") + ); + } + + /** + * MkRegion can store and read items. + * @throws Exception If some problem inside + */ + @Test + public void storesAndReadsSingleAttribute() throws Exception { + final String table = "ideas"; + final String key = "number"; + final String attr = "total"; + final Region region = new MkRegion( + new H2Data().with(table, new String[] {key}, attr) + ); + final Table tbl = region.table(table); + tbl.put( + new Attributes() + .with(key, "32443") + .with(attr, "0") + ); + final Item item = tbl.frame().iterator().next(); + item.put( + attr, + new AttributeValueUpdate().withValue( + new AttributeValue().withN("2") + ).withAction(AttributeAction.PUT) + ); + MatcherAssert.assertThat(item.get(attr).getN(), Matchers.equalTo("2")); } }