Package: SoqlRunner
SoqlRunner
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SoqlRunner(Logger) |
|
|
|
|
|
||||||||||||||||||||
lambda$0(OWLClassA, OWLClassA) |
|
|
|
|
|
||||||||||||||||||||
lambda$2(OWLClassT, OWLClassT) |
|
|
|
|
|
||||||||||||||||||||
testFindByConjunctionOfAttributes() |
|
|
|
|
|
||||||||||||||||||||
testFindByDataNotPropertyAttribute() |
|
|
|
|
|
||||||||||||||||||||
testFindByDataNotPropertyAttributeAndPropertyAttribute() |
|
|
|
|
|
||||||||||||||||||||
testFindByDataPropertyAttribute() |
|
|
|
|
|
||||||||||||||||||||
testFindByDisjunctionOfAttributes() |
|
|
|
|
|
||||||||||||||||||||
testFindByObjectPropertyAttribute() |
|
|
|
|
|
||||||||||||||||||||
testFindByTransitiveAttributeValue() |
|
|
|
|
|
||||||||||||||||||||
testOrderBy() |
|
|
|
|
|
||||||||||||||||||||
testSimpleCount() |
|
|
|
|
|
||||||||||||||||||||
testSimpleFindAll() |
|
|
|
|
|
Coverage
1: package cz.cvut.kbss.jopa.test.query.runner;
2:
3: import cz.cvut.kbss.jopa.test.OWLClassA;
4: import cz.cvut.kbss.jopa.test.OWLClassD;
5: import cz.cvut.kbss.jopa.test.OWLClassT;
6: import cz.cvut.kbss.jopa.test.environment.Generators;
7: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
8: import org.junit.jupiter.api.Test;
9: import org.slf4j.Logger;
10:
11: import java.util.Comparator;
12: import java.util.List;
13:
14: import static org.hamcrest.MatcherAssert.assertThat;
15: import static org.hamcrest.Matchers.lessThanOrEqualTo;
16: import static org.junit.jupiter.api.Assertions.*;
17:
18: public abstract class SoqlRunner extends BaseQueryRunner {
19:
20: protected SoqlRunner(Logger logger) {
21: super(logger);
22: }
23:
24: @Test
25: public void testSimpleFindAll() {
26: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
27: final List<OWLClassA> result = getEntityManager().createQuery("SELECT a FROM OWLClassA a", OWLClassA.class)
28: .getResultList();
29: assertEquals(expected.size(), result.size());
30:• for (OWLClassA a : result) {
31: assertNotNull(a.getStringAttribute());
32: assertTrue(expected.stream().anyMatch(aa -> aa.getUri().equals(a.getUri())));
33: }
34: }
35:
36: @Test
37: public void testSimpleCount() {
38: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
39: final Object result = getEntityManager().createQuery("SELECT DISTINCT COUNT(a) FROM OWLClassA a")
40: .getSingleResult();
41: assertEquals(expected.size(), result);
42: }
43:
44: @Test
45: public void testFindByDataPropertyAttribute() {
46: final OWLClassA expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
47: final OWLClassA result = getEntityManager()
48: .createQuery("SELECT a FROM OWLClassA a WHERE a.stringAttribute = :str", OWLClassA.class)
49: .setParameter("str", expected.getStringAttribute(), "en").getSingleResult();
50: assertEquals(expected.getUri(), result.getUri());
51: assertEquals(expected.getStringAttribute(), result.getStringAttribute());
52: assertEquals(expected.getTypes(), result.getTypes());
53: }
54:
55: @Test
56: public void testFindByDataNotPropertyAttribute() {
57: final OWLClassA unexpected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
58: final List<OWLClassA> result = getEntityManager()
59: .createQuery("SELECT a FROM OWLClassA a WHERE NOT a.stringAttribute = :str", OWLClassA.class)
60: .setParameter("str", unexpected.getStringAttribute(), "en").getResultList();
61:• for (OWLClassA item : result) {
62: assertNotEquals(unexpected.getUri(), item.getUri());
63: assertNotEquals(unexpected.getStringAttribute(), item.getStringAttribute());
64: }
65: }
66:
67: @Test
68: public void testFindByDataNotPropertyAttributeAndPropertyAttribute() {
69: final OWLClassT unexpected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
70: final int intThreshold = QueryTestEnvironment.getData(OWLClassT.class).size() / 2;
71: final List<OWLClassT> result = getEntityManager()
72: .createQuery("SELECT t FROM OWLClassT t WHERE NOT t.owlClassA = :a AND t.intAttribute < :intAtt",
73: OWLClassT.class)
74: .setParameter("a", unexpected.getOwlClassA().getUri())
75: .setParameter("intAtt", intThreshold).getResultList();
76: assertFalse(result.isEmpty());
77:• for (OWLClassT item : result) {
78: assertNotEquals(unexpected.getUri(), item.getUri());
79:• assertTrue(intThreshold > item.getIntAttribute());
80: }
81: }
82:
83: @Test
84: public void testFindByObjectPropertyAttribute() {
85: final OWLClassD expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassD.class));
86: final OWLClassD result = getEntityManager()
87: .createQuery("SELECT d FROM OWLClassD d WHERE d.owlClassA = :a", OWLClassD.class)
88: .setParameter("a", expected.getOwlClassA().getUri()).getSingleResult();
89: assertEquals(expected.getUri(), result.getUri());
90: assertEquals(expected.getOwlClassA().getUri(), result.getOwlClassA().getUri());
91: }
92:
93: @Test
94: public void testFindByConjunctionOfAttributes() {
95: final OWLClassT sample = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
96: final List<OWLClassT> result = getEntityManager()
97: .createQuery("SELECT t FROM OWLClassT t WHERE t.owlClassA = :a AND t.intAttribute <= :intAtt",
98: OWLClassT.class)
99: .setParameter("a", sample.getOwlClassA().getUri())
100: .setParameter("intAtt", sample.getIntAttribute()).getResultList();
101: assertFalse(result.isEmpty());
102:• for (OWLClassT item : result) {
103: assertEquals(sample.getOwlClassA().getUri(), item.getOwlClassA().getUri());
104: assertThat(item.getIntAttribute(), lessThanOrEqualTo(sample.getIntAttribute()));
105: }
106: }
107:
108: @Test
109: public void testOrderBy() {
110: final List<OWLClassT> expected = QueryTestEnvironment.getData(OWLClassT.class);
111: expected.sort(Comparator.comparing(OWLClassT::getIntAttribute));
112: final List<OWLClassT> result = getEntityManager()
113: .createQuery("SELECT t FROM OWLClassT t ORDER BY t.intAttribute", OWLClassT.class).getResultList();
114: assertEquals(expected.size(), result.size());
115:• for (OWLClassT t : result) {
116: assertTrue(expected.stream().anyMatch(tt -> tt.getUri().equals(t.getUri())));
117: }
118: }
119:
120: @Test
121: public void testFindByDisjunctionOfAttributes() {
122: final OWLClassT sample = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
123: final List<OWLClassT> result = getEntityManager()
124: .createQuery("SELECT t FROM OWLClassT t WHERE t.owlClassA = :a OR t.intAttribute <= :intAtt",
125: OWLClassT.class)
126: .setParameter("a", sample.getOwlClassA().getUri())
127: .setParameter("intAtt", sample.getIntAttribute()).getResultList();
128: assertFalse(result.isEmpty());
129:• for (OWLClassT item : result) {
130: boolean matches = item.getOwlClassA().getUri().equals(sample.getOwlClassA().getUri());
131:• matches |= item.getIntAttribute() <= sample.getIntAttribute();
132: assertTrue(matches);
133: }
134: }
135:
136: @Test
137: public void testFindByTransitiveAttributeValue() {
138: final OWLClassD expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassD.class));
139: final OWLClassD result = getEntityManager()
140: .createQuery("SELECT d FROM OWLClassD d WHERE d.owlClassA.stringAttribute = :stringAtt",
141: OWLClassD.class)
142: .setParameter("stringAtt", expected.getOwlClassA().getStringAttribute(), "en").getSingleResult();
143: assertEquals(expected.getUri(), result.getUri());
144: assertEquals(expected.getOwlClassA().getUri(), result.getOwlClassA().getUri());
145: }
146: }