Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 1 | package termmapper |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | ) |
| 9 | |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 10 | // KoralPipe-TermMapping |
| 11 | |
| 12 | func TestTokenBuilder(t *testing.T) { |
| 13 | |
| 14 | assert := assert.New(t) |
| 15 | |
| 16 | var strBuilder strings.Builder |
Akron | f461423 | 2025-04-08 11:17:53 +0200 | [diff] [blame] | 17 | term(&strBuilder, Term{"myfoundry", "mylayer", "mykey1"}, true) |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 18 | assert.Equal(strBuilder.String(), `{"@type":"koral:term","match":"match:eq","foundry":"myfoundry","layer":"mylayer","key":"mykey1"}`) |
| 19 | strBuilder.Reset() |
| 20 | |
Akron | f461423 | 2025-04-08 11:17:53 +0200 | [diff] [blame] | 21 | token(&strBuilder, []Term{{"myfoundry", "mylayer", "mykey1"}, {"myfoundry", "mylayer", "mykey2"}}, true) |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 22 | assert.Equal(strBuilder.String(), "{\"@type\":\"koral:token\",\"wrap\":{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey1\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}}") |
| 23 | strBuilder.Reset() |
| 24 | |
Akron | f461423 | 2025-04-08 11:17:53 +0200 | [diff] [blame] | 25 | token(&strBuilder, []Term{{"myfoundry", "mylayer", "mykey2"}}, true) |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 26 | assert.Equal(strBuilder.String(), "{\"@type\":\"koral:token\",\"wrap\":{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}}") |
| 27 | strBuilder.Reset() |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | jsonStr := `{ |
| 32 | "query": { |
| 33 | "@type": "koral:operation", |
| 34 | "operators": [ |
| 35 | { |
| 36 | "@type": "koral:term", |
| 37 | "key": "example1" |
| 38 | }, |
| 39 | { |
| 40 | "@type": "koral:term", |
| 41 | "key": "example2" |
| 42 | }, |
| 43 | { |
| 44 | "@type": "koral:operation", |
| 45 | "operators": [ |
| 46 | { |
| 47 | "@type": "koral:term", |
| 48 | "key": "nested" |
| 49 | } |
| 50 | ] |
| 51 | } |
| 52 | ] |
| 53 | } |
| 54 | }` |
| 55 | */ |
| 56 | |
| 57 | func TestTermReplacement(t *testing.T) { |
| 58 | |
| 59 | assert := assert.New(t) |
| 60 | |
Akron | 08aa263 | 2025-04-07 17:38:28 +0200 | [diff] [blame] | 61 | // case1: 1 -> 1 the term is wrapped with eq |
| 62 | // case1: 1 -> 1 the term is wrapped with ne |
| 63 | // [ADV] -> [ADV] |
| 64 | testStr := replaceWrappedTerms( |
| 65 | "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}", |
| 66 | []Term{{"myfoundry2", |
| 67 | "mylayer2", |
| 68 | "mykey2", |
| 69 | }}, |
| 70 | ) |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 71 | assert.Equal(testStr, "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}") |
| 72 | |
Akron | 08aa263 | 2025-04-07 17:38:28 +0200 | [diff] [blame] | 73 | testStr = replaceWrappedTerms( |
| 74 | "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}", |
| 75 | []Term{{"myfoundry2", |
| 76 | "mylayer2", |
| 77 | "mykey2", |
| 78 | }}, |
| 79 | ) |
| 80 | assert.Equal(testStr, "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}") |
| 81 | |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 82 | // case2: 1 -> 1 the term is an operand in a termGroup with the same relation/operation |
Akron | 08aa263 | 2025-04-07 17:38:28 +0200 | [diff] [blame] | 83 | // [ADV & ...] -> [ADV] |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 84 | // case3: 1 -> 1 the term is an operand in a termGroup with a different relation/operation |
Akron | 08aa263 | 2025-04-07 17:38:28 +0200 | [diff] [blame] | 85 | // [ADV | ...] -> [ADV] |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 86 | testStr = replaceGroupedTerm( |
| 87 | "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}", |
| 88 | []int{0}, |
| 89 | "myfoundryX", |
| 90 | "mylayerX", |
| 91 | "mykeyX", |
| 92 | ) |
| 93 | assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundryX\",\"layer\":\"mylayerX\",\"key\":\"mykeyX\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}") |
| 94 | |
| 95 | testStr = replaceGroupedTerm( |
| 96 | "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}", |
| 97 | []int{0}, |
| 98 | "myfoundryX", |
| 99 | "mylayerX", |
| 100 | "mykeyX", |
| 101 | ) |
| 102 | assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundryX\",\"layer\":\"mylayerX\",\"key\":\"mykeyX\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}") |
| 103 | |
| 104 | // case4: n -> 1 the term is an operand in a termGroup with the same relation/operation |
Akron | 08aa263 | 2025-04-07 17:38:28 +0200 | [diff] [blame] | 105 | // [PRON & Poss=yes & PronType=Prs] -> [PPOSAT] |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 106 | testStr = replaceGroupedTerm( |
| 107 | "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}", |
| 108 | []int{0, 1}, |
| 109 | "myfoundryX", |
| 110 | "mylayerX", |
| 111 | "mykeyX", |
| 112 | ) |
| 113 | assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundryX\",\"layer\":\"mylayerX\",\"key\":\"mykeyX\"}]}") |
| 114 | |
Akron | 08aa263 | 2025-04-07 17:38:28 +0200 | [diff] [blame] | 115 | // case5: 1 -> n the term is wrapped |
| 116 | // [PPOSAT] -> [PRON & Poss=yes & PronType=Prs] |
| 117 | testStr = replaceWrappedTerms( |
| 118 | "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}", |
| 119 | []Term{{ |
| 120 | "myfoundry1", |
| 121 | "mylayer1", |
| 122 | "mykey1", |
| 123 | }, { |
| 124 | "myfoundry2", |
| 125 | "mylayer2", |
| 126 | "mykey2", |
| 127 | }}, |
| 128 | ) |
| 129 | assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry1\",\"layer\":\"mylayer1\",\"key\":\"mykey1\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}]}") |
| 130 | |
| 131 | // [!PPOSAT] -> [!PRON | !Poss=yes | !PronType=Prs] |
| 132 | testStr = replaceWrappedTerms( |
| 133 | "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}", |
| 134 | []Term{{ |
| 135 | "myfoundry1", |
| 136 | "mylayer1", |
| 137 | "mykey1", |
| 138 | }, { |
| 139 | "myfoundry2", |
| 140 | "mylayer2", |
| 141 | "mykey2", |
| 142 | }}, |
| 143 | ) |
| 144 | assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:or\",\"operation\":\"operation:or\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry1\",\"layer\":\"mylayer1\",\"key\":\"mykey1\"},{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}]}") |
| 145 | |
| 146 | // case6: 1 -> n the term is an operand in a termGroup with the same relation/operation |
Akron | 8251bae | 2025-05-16 14:13:50 +0200 | [diff] [blame^] | 147 | // [PPOSAT & ...] -> [PRON & Poss=yes & PronType=Prs] |
| 148 | testStr = replaceGroupedTerms( |
| 149 | "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey1\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}]}", |
| 150 | []int{0}, |
| 151 | []Term{{ |
| 152 | "myfoundry3", |
| 153 | "mylayer3", |
| 154 | "mykey3", |
| 155 | }, { |
| 156 | "myfoundry4", |
| 157 | "mylayer4", |
| 158 | "mykey4", |
| 159 | }}, |
| 160 | ) |
| 161 | assert.Equal(testStr, "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":[{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry3\",\"layer\":\"mylayer3\",\"key\":\"mykey3\"},{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry4\",\"layer\":\"mylayer4\",\"key\":\"mykey4\"}]}") |
Akron | 08aa263 | 2025-04-07 17:38:28 +0200 | [diff] [blame] | 162 | |
| 163 | // case7: 1 -> n the term is an operand in a termGroup with a different relation/operation |
Akron | 8251bae | 2025-05-16 14:13:50 +0200 | [diff] [blame^] | 164 | testStr = replaceGroupedTerms( |
| 165 | |
| 166 | "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":["+ |
| 167 | "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey1\"},"+ |
| 168 | "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}"+ |
| 169 | "]}", |
| 170 | []int{0}, |
| 171 | []Term{{ |
| 172 | "myfoundry3", |
| 173 | "mylayer3", |
| 174 | "mykey3", |
| 175 | }, { |
| 176 | "myfoundry4", |
| 177 | "mylayer4", |
| 178 | "mykey4", |
| 179 | }}, |
| 180 | ) |
| 181 | |
| 182 | // TODO: Add a termGroup with reversed signs |
| 183 | assert.Equal(testStr, |
| 184 | |
| 185 | "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:and\",\"operation\":\"operation:and\",\"operands\":["+ |
| 186 | |
| 187 | "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"},"+ |
| 188 | |
| 189 | "{\"@type\":\"koral:termGroup\",\"relation\":\"relation:or\",\"operation\":\"operation:or\",\"operands\":["+ |
| 190 | |
| 191 | "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry3\",\"layer\":\"mylayer3\",\"key\":\"mykey3\"},"+ |
| 192 | "{\"@type\":\"koral:term\",\"match\":\"match:ne\",\"foundry\":\"myfoundry4\",\"layer\":\"mylayer4\",\"key\":\"mykey4\"}"+ |
| 193 | "]}"+ |
| 194 | |
| 195 | "]"+ |
| 196 | "}") |
Akron | 08aa263 | 2025-04-07 17:38:28 +0200 | [diff] [blame] | 197 | // case8: n -> n the term is an operand in a termGroup with the same relation/operation |
| 198 | // case9: n -> n the term is an operand in a termGroup with a different relation/operation |
| 199 | |
Akron | fc3bd27 | 2025-04-04 16:15:44 +0200 | [diff] [blame] | 200 | } |