Skip to content

Commit 1476c1e

Browse files
iDneprovArtDu
authored andcommitted
Add tarantoolTupleUsageExample test
Add test with examples of TarantoolTuple usage and accounts space Needed for #354
1 parent cfba3de commit 1476c1e

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

src/test/java/io/tarantool/driver/integration/ProxyTarantoolClientExampleIT.java

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010
import io.tarantool.driver.api.tuple.TarantoolTupleFactory;
1111
import io.tarantool.driver.api.tuple.operations.TupleOperations;
1212
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.BeforeEach;
1314
import org.junit.jupiter.api.Test;
1415

1516
import java.util.ArrayList;
1617
import java.util.Collections;
1718
import java.util.List;
19+
import java.util.Optional;
1820
import java.util.concurrent.ExecutionException;
1921

2022
import static org.junit.jupiter.api.Assertions.*;
2123

2224
/**
2325
* @author Ivan Dneprov
2426
* <p>
25-
* WARNING: If you updated the code in this file, don't forget to update the docs/ProxyTarantoolClient.md permalinks!
27+
* WARNING: If you updated the code in this file, don't forget to update the docs/ProxyTarantoolClient.md
28+
* and docs/TarantoolTupleUsage.md permalinks!
2629
*/
2730
public class ProxyTarantoolClientExampleIT extends SharedCartridgeContainer {
2831

@@ -38,7 +41,6 @@ public class ProxyTarantoolClientExampleIT extends SharedCartridgeContainer {
3841
public static void setUp() throws Exception {
3942
startCluster();
4043
initClient();
41-
truncateSpace(SPACE_NAME);
4244
}
4345

4446
public static void initClient() {
@@ -55,8 +57,9 @@ public static void initClient() {
5557
.build();
5658
}
5759

58-
private static void truncateSpace(String spaceName) {
59-
client.space(spaceName).truncate().join();
60+
@BeforeEach
61+
public void truncateSpace() {
62+
client.space(SPACE_NAME).truncate().join();
6063
}
6164

6265
@Test
@@ -106,4 +109,56 @@ public void clusterInsertSelectDeleteTest() throws ExecutionException, Interrupt
106109
TarantoolResult<TarantoolTuple> updateResult = space.update(conditions, TupleOperations.set(4, 10)).get();
107110
assertEquals(10, updateResult.get(0).getInteger(4));
108111
}
112+
113+
@Test
114+
public void tarantoolTupleUsageExample() throws ExecutionException, InterruptedException, NullPointerException {
115+
TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> accounts =
116+
client.space("accounts");
117+
118+
// Use TarantoolTupleFactory for instantiating new tuples
119+
TarantoolTupleFactory tupleFactory = new DefaultTarantoolTupleFactory(
120+
client.getConfig().getMessagePackMapper());
121+
122+
// Create a tuple from listed values: [1, null, "credit card", 99.99]
123+
// This tuple contains java values
124+
TarantoolTuple inputTuple = tupleFactory.create(1, null, "credit card", 99.99);
125+
126+
// Insert it in the database
127+
accounts.insert(inputTuple).join();
128+
129+
// This tuple form the database
130+
Conditions conditions = Conditions.equals("id", 1);
131+
TarantoolResult<TarantoolTuple> selectResult = accounts.select(conditions).get();
132+
assertEquals(selectResult.size(), 1);
133+
// This tuple contains messagePack values
134+
TarantoolTuple selectTuple = selectResult.get(0);
135+
assertEquals(selectTuple.size(), 4);
136+
137+
// You can get value from TarantoolTuple by its filedPosition
138+
// If you do not set objectClass default converter will be used for this value
139+
Optional<?> object = selectTuple.getObject(0);
140+
assertEquals(1, object.orElseThrow(NullPointerException::new));
141+
142+
// For example any non-integer number will be converted to Double by default
143+
Optional<?> doubleValue = selectTuple.getObject(3);
144+
assertEquals(99.99, doubleValue.orElseThrow(NullPointerException::new));
145+
assertEquals(Double.class, doubleValue.orElseThrow(NullPointerException::new).getClass());
146+
147+
// But if you need to get Float, you can set objectClass
148+
Optional<?> floatValue = selectTuple.getObject(3, Float.class);
149+
assertEquals(99.99f, floatValue.orElseThrow(NullPointerException::new));
150+
assertEquals(Float.class, floatValue.orElseThrow(NullPointerException::new).getClass());
151+
152+
// You do not have to work with Optional
153+
// Getters for all basic types are available
154+
float floatNumber = selectTuple.getFloat(3);
155+
assertEquals(99.99f, floatNumber);
156+
157+
// Also you can get values by field name
158+
Optional<?> balance = selectTuple.getObject("balance");
159+
assertEquals(99.99, balance.orElseThrow(NullPointerException::new));
160+
161+
String stringValue = selectTuple.getString("name");
162+
assertEquals("credit card", stringValue);
163+
}
109164
}

src/test/resources/cartridge/app/roles/api_storage.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,21 @@ local function init_space()
202202
type = 'TREE'
203203
})
204204

205+
local accounts = box.schema.space.create(
206+
'accounts',
207+
{
208+
format = {
209+
{ 'id', 'unsigned' },
210+
{ 'bucket_id', 'unsigned' },
211+
{ 'name', 'string' },
212+
{ 'balance', 'number' },
213+
},
214+
if_not_exists = true,
215+
}
216+
)
217+
218+
accounts:create_index('id', { parts = { 'id' }, if_not_exists = true, })
219+
accounts:create_index('bucket_id', { parts = { 'bucket_id' }, unique = false, if_not_exists = true, })
205220
end
206221

207222
local function get_composite_data(id)

0 commit comments

Comments
 (0)