1 package io.github.magwas.inez.query;
2
3 import static org.junit.jupiter.api.Assertions.assertThrows;
4
5 import java.util.Set;
6 import java.util.stream.Stream;
7
8 import org.antlr.v4.runtime.misc.ParseCancellationException;
9 import org.junit.jupiter.api.DisplayName;
10 import org.junit.jupiter.api.Test;
11 import org.mockito.InjectMocks;
12
13 import io.github.magwas.inez.Bridi;
14 import io.github.magwas.inez.BridiTestData;
15 import io.github.magwas.inez.parse.ParserConstants;
16 import io.github.magwas.testing.TestBase;
17 import io.github.magwas.testing.TestUtil;
18
19 class QueryProcessorTest extends TestBase implements BridiTestData {
20
21 @InjectMocks
22 QueryProcessorService queryProcessor;
23
24 @Test
25 @DisplayName("for a nonexisting sumti returns the empty list")
26 void test() {
27 Stream<Bridi> actual = queryProcessor.apply(QUERY_NONEXISTING);
28 TestUtil.assertStreamEquals(Set.of(), actual);
29 }
30
31 @Test
32 @DisplayName("for an existing sumti returns the sumti")
33 void test_1() {
34 TestUtil.assertStreamEquals(Set.of(THING), queryProcessor.apply(THING_REPR));
35 }
36
37 @Test
38 @DisplayName("if more sumtis exist with the representation, return them all")
39 void test_2() {
40 TestUtil.assertStreamEquals(Set.of(GO1, GO2), queryProcessor.apply(GO_REPRESENTATION));
41 }
42
43 @Test
44 @DisplayName("for a reference, return the referenced bridi")
45 void test_3() {
46 TestUtil.assertStreamEquals(Set.of(GO1), queryProcessor.apply(GO1_REFERENCE));
47 }
48
49 @Test
50 @DisplayName("for a reference to a nonexisting bridi, return an empty list")
51 void test_5() {
52 TestUtil.assertStreamEquals(Set.of(), queryProcessor.apply(NONEXISTENT_REFERENCE));
53 }
54
55 @Test
56 @DisplayName("for a bridi with the matching representation returns it")
57 void test_4() {
58 TestUtil.assertStreamEquals(Set.of(SUMTI_IS_A_THING), queryProcessor.apply(SUMTI_IS_A_THING_REPR));
59 }
60
61 @Test
62 @DisplayName("for a bridi where the representation does not match, but the selbri and sumties do, finds it")
63 void test_6() {
64 TestUtil.assertStreamEquals(Set.of(TAUTOLOGY), queryProcessor.apply(TAUTOLOGY_GENERATED_REPR));
65 }
66
67 @Test
68 @DisplayName("'$?' returns a list of the any sumti")
69 void test_7() {
70 TestUtil.assertStreamEquals(Set.of(ANY), queryProcessor.apply(ParserConstants.QUERY_BRIDI_ID));
71 }
72
73 @Test
74 @DisplayName("for a bridi where one of the sumties is '$?', the matching bridies return")
75 void test1() {
76 TestUtil.assertStreamEquals(SIMPLE_QUERY_OUTPUT, queryProcessor.apply(QUERY_STRING_SIMPLE));
77 }
78
79 @Test
80 @DisplayName("for a bridi where all of the sumties is '$?', a ParseCancellationException is thrown")
81 void test1_1() {
82 assertThrows(
83 ParseCancellationException.class,
84 () -> queryProcessor.apply(QUERY_STRING_ALL_ANY).toArray());
85 }
86
87 @Test
88 @DisplayName("if no stored bridi matches the query with '$?', and empty list is returned")
89 void test2() {
90 TestUtil.assertStreamEquals(Set.of(), queryProcessor.apply(QUERY_STRING_NONMATCHING));
91 }
92
93 @Test
94 @DisplayName("query works with the '$?' being deep down in the query")
95 void test3() {
96 Stream<Bridi> actual = queryProcessor.apply(RECURSIVE_QUERY);
97 TestUtil.assertStreamEquals(Set.of(SUMTI_IS_A_THING_IS_A_THING, TAUTOLOGY_IS_A_THING), actual);
98 }
99 }