1
2
3
4
5 package com.jcabi.dynamo.mock;
6
7 import com.jcabi.dynamo.Attributes;
8 import com.jcabi.dynamo.Credentials;
9 import com.jcabi.dynamo.Dosage;
10 import com.jcabi.dynamo.Frame;
11 import com.jcabi.dynamo.Region;
12 import com.jcabi.dynamo.Table;
13 import com.jcabi.dynamo.Valve;
14 import java.util.Collection;
15 import java.util.Map;
16 import org.hamcrest.MatcherAssert;
17 import org.hamcrest.Matchers;
18 import org.junit.jupiter.api.Test;
19 import software.amazon.awssdk.services.dynamodb.model.Condition;
20
21
22
23
24
25 final class MkFrameTest {
26
27 @Test
28 void filtersWithWhereClause() throws Exception {
29 final String name = "wh\u00e9re";
30 final String key = "k\u00e9y";
31 final String attr = "\u00e4ttr";
32 final Region region = new MkRegion(
33 new H2Data().with(name, new String[]{key}, attr)
34 );
35 final Table table = region.table(name);
36 table.put(
37 new Attributes().with(key, "1").with(attr, "f\u00f6o")
38 );
39 table.put(
40 new Attributes().with(key, "2").with(attr, "b\u00e4r")
41 );
42 MatcherAssert.assertThat(
43 "does not filter items by where clause",
44 table.frame().where(key, "1").size(),
45 Matchers.equalTo(1)
46 );
47 }
48
49 @Test
50 void returnsTableReference() throws Exception {
51 final String name = "t\u00e4ble";
52 MatcherAssert.assertThat(
53 "does not return table from frame",
54 new MkRegion(
55 new H2Data().with(name, new String[]{"cl\u00e9"})
56 ).table(name).frame().table(),
57 Matchers.notNullValue()
58 );
59 }
60
61 @Test
62 void countsSizeOfItems() throws Exception {
63 final String name = "s\u00edze";
64 final String key = "k\u00e9y";
65 final Region region = new MkRegion(
66 new H2Data().with(name, new String[]{key})
67 );
68 final Table table = region.table(name);
69 table.put(new Attributes().with(key, "73921"));
70 table.put(new Attributes().with(key, "48302"));
71 MatcherAssert.assertThat(
72 "does not count items correctly",
73 table.frame().size(),
74 Matchers.equalTo(2)
75 );
76 }
77
78 @Test
79 void ignoresValveInThrough() throws Exception {
80 final String name = "d\u00fcrch";
81 final String key = "k\u00e9y";
82 final Region region = new MkRegion(
83 new H2Data().with(name, new String[]{key})
84 );
85 final Table table = region.table(name);
86 table.put(new Attributes().with(key, "91234"));
87 final Frame frame = table.frame();
88 MatcherAssert.assertThat(
89 "does not return same frame from through",
90 frame.through(
91 new Valve() {
92 @Override
93 public Dosage fetch(
94 final Credentials credentials,
95 final String tbl,
96 final Map<String, Condition> conditions,
97 final Collection<String> keys) {
98 return new Dosage.Empty();
99 }
100
101 @Override
102 public int count(
103 final Credentials credentials,
104 final String tbl,
105 final Map<String, Condition> conditions) {
106 return 0;
107 }
108 }
109 ).size(),
110 Matchers.equalTo(1)
111 );
112 }
113
114 @Test
115 void iteratesOverEmptyFrame() throws Exception {
116 final String name = "\u00e9mpty";
117 MatcherAssert.assertThat(
118 "does not iterate over empty frame",
119 new MkRegion(
120 new H2Data().with(name, new String[]{"schl\u00fcssel"})
121 ).table(name).frame().iterator().hasNext(),
122 Matchers.is(false)
123 );
124 }
125 }