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 | |
| 10 | func TestConstants(t *testing.T) { |
| 11 | assert := assert.New(t) |
| 12 | assert.Equal(Hui(), "test") |
| 13 | } |
| 14 | |
| 15 | // KoralPipe-TermMapping |
| 16 | |
| 17 | func TestTokenBuilder(t *testing.T) { |
| 18 | |
| 19 | assert := assert.New(t) |
| 20 | |
| 21 | var strBuilder strings.Builder |
| 22 | term(&strBuilder, "myfoundry", "mylayer", "mykey1", true) |
| 23 | assert.Equal(strBuilder.String(), `{"@type":"koral:term","match":"match:eq","foundry":"myfoundry","layer":"mylayer","key":"mykey1"}`) |
| 24 | strBuilder.Reset() |
| 25 | |
| 26 | token(&strBuilder, "myfoundry", "mylayer", []string{"mykey1", "mykey2"}) |
| 27 | 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\"}]}}") |
| 28 | strBuilder.Reset() |
| 29 | |
| 30 | token(&strBuilder, "myfoundry", "mylayer", []string{"mykey2"}) |
| 31 | assert.Equal(strBuilder.String(), "{\"@type\":\"koral:token\",\"wrap\":{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey2\"}}") |
| 32 | strBuilder.Reset() |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | jsonStr := `{ |
| 37 | "query": { |
| 38 | "@type": "koral:operation", |
| 39 | "operators": [ |
| 40 | { |
| 41 | "@type": "koral:term", |
| 42 | "key": "example1" |
| 43 | }, |
| 44 | { |
| 45 | "@type": "koral:term", |
| 46 | "key": "example2" |
| 47 | }, |
| 48 | { |
| 49 | "@type": "koral:operation", |
| 50 | "operators": [ |
| 51 | { |
| 52 | "@type": "koral:term", |
| 53 | "key": "nested" |
| 54 | } |
| 55 | ] |
| 56 | } |
| 57 | ] |
| 58 | } |
| 59 | }` |
| 60 | */ |
| 61 | |
| 62 | func TestTermReplacement(t *testing.T) { |
| 63 | |
| 64 | assert := assert.New(t) |
| 65 | |
| 66 | // case1: 1 -> 1 the term is wrapped |
| 67 | testStr := replaceWrappedTerm("{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry\",\"layer\":\"mylayer\",\"key\":\"mykey\"}", "myfoundry2", "mylayer2", "mykey2") |
| 68 | assert.Equal(testStr, "{\"@type\":\"koral:term\",\"match\":\"match:eq\",\"foundry\":\"myfoundry2\",\"layer\":\"mylayer2\",\"key\":\"mykey2\"}") |
| 69 | |
| 70 | // case2: 1 -> 1 the term is an operand in a termGroup with the same relation/operation |
| 71 | // case3: 1 -> 1 the term is an operand in a termGroup with a different relation/operation |
| 72 | testStr = replaceGroupedTerm( |
| 73 | "{\"@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\"}]}", |
| 74 | []int{0}, |
| 75 | "myfoundryX", |
| 76 | "mylayerX", |
| 77 | "mykeyX", |
| 78 | ) |
| 79 | 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\"}]}") |
| 80 | |
| 81 | testStr = replaceGroupedTerm( |
| 82 | "{\"@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\"}]}", |
| 83 | []int{0}, |
| 84 | "myfoundryX", |
| 85 | "mylayerX", |
| 86 | "mykeyX", |
| 87 | ) |
| 88 | 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\"}]}") |
| 89 | |
| 90 | // case4: n -> 1 the term is an operand in a termGroup with the same relation/operation |
| 91 | testStr = replaceGroupedTerm( |
| 92 | "{\"@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\"}]}", |
| 93 | []int{0, 1}, |
| 94 | "myfoundryX", |
| 95 | "mylayerX", |
| 96 | "mykeyX", |
| 97 | ) |
| 98 | 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\"}]}") |
| 99 | |
| 100 | // case5: n -> 1 the term is an operand in a termGroup with a different relation/operation |
| 101 | // case6: 1 -> n the term is wrapped |
| 102 | // case7: 1 -> n the term is an operand in a termGroup with the same relation/operation |
| 103 | // case8: 1 -> n the term is an operand in a termGroup with a different relation/operation |
| 104 | // case9: n -> n the term is an operand in a termGroup with the same relation/operation |
| 105 | // case10: n -> n the term is an operand in a termGroup with a different relation/operation |
| 106 | } |