Skip to content

Commit

Permalink
#102 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 5, 2019
1 parent d8a52c5 commit 4720094
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
46 changes: 36 additions & 10 deletions src/main/java/com/jcabi/dynamo/mock/H2Data.java
Expand Up @@ -56,6 +56,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -130,6 +131,40 @@ public String apply(final String key) {
}
};

/**
* Select WHERE.
* @checkstyle AnonInnerLengthCheck (100 lines)
* @checkstyle LineLength (3 lines)
*/
private static final Function<Map.Entry<String, Condition>, String> SELECT_WHERE =
new Function<Map.Entry<String, Condition>, String>() {
@Override
public String apply(final Map.Entry<String, Condition> cnd) {
final String opr;
if (cnd.getValue().getComparisonOperator()
.equals(ComparisonOperator.GT.toString())) {
opr = ">";
} else if (cnd.getValue().getComparisonOperator()
.equals(ComparisonOperator.LT.toString())) {
opr = "<";
} else if (cnd.getValue().getComparisonOperator()
.equals(ComparisonOperator.EQ.toString())) {
opr = "=";
} else {
throw new UnsupportedOperationException(
String.format(
// @checkstyle LineLength (1 line)
"At the moment only EQ/GT/LT operators are supported: %s",
cnd.getValue().getComparisonOperator()
)
);
}
return String.format(
"%s %s ?", cnd.getKey(), opr
);
}
};

/**
* Create primary key.
*/
Expand Down Expand Up @@ -232,7 +267,7 @@ public Iterable<Attributes> iterate(final String table,
sql.append(" WHERE ");
Joiner.on(H2Data.AND).appendTo(
sql,
Iterables.transform(conds.keySet(), H2Data.WHERE)
Iterables.transform(conds.entrySet(), H2Data.SELECT_WHERE)
);
}
JdbcSession session = new JdbcSession(this.connection())
Expand All @@ -243,15 +278,6 @@ public Iterable<Attributes> iterate(final String table,
"at the moment only one value of condition is supported"
);
}
if (!cond.getComparisonOperator()
.equals(ComparisonOperator.EQ.toString())) {
throw new UnsupportedOperationException(
String.format(
"at the moment only EQ operator is supported: %s",
cond.getComparisonOperator()
)
);
}
final AttributeValue val = cond.getAttributeValueList().get(0);
session = session.set(H2Data.value(val));
}
Expand Down
39 changes: 37 additions & 2 deletions src/test/java/com/jcabi/dynamo/mock/H2DataTest.java
Expand Up @@ -30,6 +30,8 @@
package com.jcabi.dynamo.mock;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.jcabi.dynamo.AttributeUpdates;
Expand Down Expand Up @@ -155,7 +157,7 @@ public void supportsColumnNamesWithIllegalCharacters() throws Exception {
final String key = "0-.col.-0";
final String table = "test";
new H2Data().with(
table, new String[] {key}, new String[0]
table, new String[] {key}
).put(table, new Attributes().with(key, "value"));
}

Expand All @@ -170,7 +172,7 @@ public void deletesRecords() throws Exception {
final String man = "Kevin";
final String woman = "Helen";
final H2Data data = new H2Data()
.with(table, new String[]{field}, new String[0]);
.with(table, new String[]{field});
data.put(
table,
new Attributes().with(field, man)
Expand Down Expand Up @@ -234,4 +236,37 @@ table, new Conditions().with(key, Conditions.equalTo(number))
Matchers.<Attributes>iterableWithSize(1)
);
}

/**
* H2Data can fetch with comparison.
* @throws Exception If some problem inside
*/
@Test
public void fetchesWithComparison() throws Exception {
final String table = "x12";
final String key = "foo1";
final String value = "bar2";
final MkData data = new H2Data().with(table, new String[] {key}, value);
data.put(table, new Attributes().with(key, "101").with(value, 0));
data.put(table, new Attributes().with(key, "102").with(value, 1));
MatcherAssert.assertThat(
data.iterate(table, new Conditions()),
Matchers.<Attributes>iterableWithSize(2)
);
MatcherAssert.assertThat(
data.iterate(
table,
new Conditions().with(
value,
new Condition()
.withAttributeValueList(
new AttributeValue().withN("0")
)
.withComparisonOperator(ComparisonOperator.GT)
)
).iterator().next().get(value).getN(),
Matchers.equalTo("1")
);
}

}

0 comments on commit 4720094

Please sign in to comment.