/
SqlStreamTerminatorTest.java
126 lines (114 loc) · 5.27 KB
/
SqlStreamTerminatorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
*
* Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.runtime.core.manager.sql;
import com.speedment.runtime.core.db.AsynchronousQueryResult;
import com.speedment.runtime.core.db.DbmsType;
import com.speedment.runtime.core.db.FieldPredicateView;
import com.speedment.runtime.core.internal.manager.sql.SqlPredicateFragmentImpl;
import com.speedment.runtime.core.internal.manager.sql.SqlStreamTerminator;
import com.speedment.runtime.core.internal.stream.builder.action.reference.FilterAction;
import com.speedment.runtime.core.internal.stream.builder.action.reference.MapAction;
import com.speedment.runtime.core.internal.stream.builder.pipeline.PipelineImpl;
import com.speedment.runtime.core.internal.stream.builder.pipeline.ReferencePipeline;
import com.speedment.runtime.core.stream.action.Action;
import com.speedment.runtime.field.Field;
import com.speedment.runtime.field.predicate.FieldPredicate;
import com.speedment.runtime.typemapper.TypeMapper;
import com.speedment.runtime.typemapper.internal.IdentityTypeMapper;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNull;
import org.junit.Test;
import static org.mockito.ArgumentMatchers.any;
import org.mockito.Mockito;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class SqlStreamTerminatorTest {
private static final long SQL_COUNT_RESULT = 100L;
private static final String SELECT_SQL = "SELECT * FROM table";
private static final String SELECT_COUNT_SQL = "SELECT COUNT(*) FROM table";
private static final String PREDICATE_COUNT_SQL_FRAGMENT = "ID is Cool";
private static final String COUNT_WHERE_SQL = String.join(" WHERE ", SELECT_COUNT_SQL, PREDICATE_COUNT_SQL_FRAGMENT);
private String lastCountingSql;
private static class MockEntity {
private final int id;
private MockEntity(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
@Test
public void testCountGeneralFilter() {
lastCountingSql = null;
final Action<Stream<MockEntity>, Stream<MockEntity>> filterAction = new FilterAction<>(e -> e.getId() % 10 == 3);
assertEquals(10, countStreamOf(filterAction));
assertNull(lastCountingSql);
}
@Test
public void testCountSizePreservingFilter() {
final Action<Stream<MockEntity>, Stream<Integer>> mapAction = new MapAction<>(MockEntity::getId);
assertEquals(SQL_COUNT_RESULT, countStreamOf(mapAction));
assertEquals(SELECT_COUNT_SQL, lastCountingSql);
}
@Test
public void testCountFieldPredicateFilter() {
final TypeMapper typeMapper = new IdentityTypeMapper();
@SuppressWarnings("unchecked")
final FieldPredicate<MockEntity> predicate = (FieldPredicate<MockEntity>) mock(FieldPredicate.class);
final Field field = mock(Field.class);
when(field.typeMapper()).thenReturn(typeMapper);
when(predicate.getField()).thenReturn(field);
Action<Stream<MockEntity>, Stream<MockEntity>> filterAction = new FilterAction<>(predicate);
assertEquals(SQL_COUNT_RESULT, countStreamOf(filterAction));
assertEquals(COUNT_WHERE_SQL, lastCountingSql);
}
private long countStreamOf(Action<?, ?> action) {
SqlStreamTerminator<MockEntity> terminator = new SqlStreamTerminator<>(
createDbmsType(),
SELECT_SQL,
SELECT_COUNT_SQL,
(sql, l) -> {
lastCountingSql = sql;
return SQL_COUNT_RESULT;
},
f -> "",
/*Mockito.<AsynchronousQueryResult<MockEntity>>*/mock(AsynchronousQueryResult.class)
);
return terminator.count(createPipeline(action));
}
private ReferencePipeline<MockEntity> createPipeline(Action action) {
Supplier supplier = mock(Supplier.class);
Stream<MockEntity> stream = IntStream.range(0, 100).boxed().map(MockEntity::new);
when(supplier.get()).thenReturn(stream);
ReferencePipeline<MockEntity> pipeline = new PipelineImpl<>(supplier);
pipeline.add(action);
return pipeline;
}
private DbmsType createDbmsType() {
final DbmsType dbmsType = mock(DbmsType.class);
final FieldPredicateView fpv = mock(FieldPredicateView.class);
final SqlPredicateFragmentImpl predicateFragment = new SqlPredicateFragmentImpl();
predicateFragment.setSql(PREDICATE_COUNT_SQL_FRAGMENT);
when(fpv.transform(any(), any())).thenReturn(predicateFragment);
when(dbmsType.getFieldPredicateView()).thenReturn(fpv);
return dbmsType;
}
}