blob: b5a8950676c5b573544c0baa09895ca7edcfbeab [file] [log] [blame]
Akron32d53de2025-05-22 13:45:32 +02001package mapper
2
3import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11)
12
13func TestMapper(t *testing.T) {
14 // Create a temporary config file
15 tmpDir := t.TempDir()
16 configFile := filepath.Join(tmpDir, "test-config.yaml")
17
18 configContent := `- id: test-mapper
19 foundryA: opennlp
20 layerA: p
21 foundryB: upos
22 layerB: p
23 mappings:
24 - "[PIDAT] <> [opennlp/p=PIDAT & opennlp/p=AdjType:Pdt]"
25 - "[DET] <> [opennlp/p=DET]"`
26
27 err := os.WriteFile(configFile, []byte(configContent), 0644)
28 require.NoError(t, err)
29
30 // Create a new mapper
31 m, err := NewMapper(configFile)
32 require.NoError(t, err)
33
34 tests := []struct {
35 name string
36 mappingID string
37 opts MappingOptions
38 input string
39 expected string
40 expectError bool
41 }{
42 {
43 name: "Simple A to B mapping",
44 mappingID: "test-mapper",
45 opts: MappingOptions{
46 Direction: AtoB,
47 },
48 input: `{
49 "@type": "koral:token",
50 "wrap": {
51 "@type": "koral:term",
52 "foundry": "opennlp",
53 "key": "PIDAT",
54 "layer": "p",
55 "match": "match:eq"
56 }
57 }`,
58 expected: `{
59 "@type": "koral:token",
60 "wrap": {
61 "@type": "koral:termGroup",
62 "operands": [
63 {
64 "@type": "koral:term",
65 "foundry": "opennlp",
66 "key": "PIDAT",
67 "layer": "p",
68 "match": "match:eq"
69 },
70 {
71 "@type": "koral:term",
72 "foundry": "opennlp",
73 "key": "AdjType",
74 "layer": "p",
75 "match": "match:eq",
76 "value": "Pdt"
77 }
78 ],
79 "relation": "relation:and"
80 }
81 }`,
82 },
83 {
84 name: "B to A mapping",
85 mappingID: "test-mapper",
86 opts: MappingOptions{
87 Direction: BtoA,
88 },
89 input: `{
90 "@type": "koral:token",
91 "wrap": {
92 "@type": "koral:termGroup",
93 "operands": [
94 {
95 "@type": "koral:term",
96 "foundry": "opennlp",
97 "key": "PIDAT",
98 "layer": "p",
99 "match": "match:eq"
100 },
101 {
102 "@type": "koral:term",
103 "foundry": "opennlp",
104 "key": "AdjType",
105 "layer": "p",
106 "match": "match:eq",
107 "value": "Pdt"
108 }
109 ],
110 "relation": "relation:and"
111 }
112 }`,
113 expected: `{
114 "@type": "koral:token",
115 "wrap": {
116 "@type": "koral:term",
117 "foundry": "opennlp",
118 "key": "PIDAT",
119 "layer": "p",
120 "match": "match:eq"
121 }
122 }`,
123 },
124 {
125 name: "Mapping with foundry override",
126 mappingID: "test-mapper",
127 opts: MappingOptions{
128 Direction: AtoB,
129 FoundryB: "custom",
130 },
131 input: `{
132 "@type": "koral:token",
133 "wrap": {
134 "@type": "koral:term",
135 "foundry": "opennlp",
136 "key": "PIDAT",
137 "layer": "p",
138 "match": "match:eq"
139 }
140 }`,
141 expected: `{
142 "@type": "koral:token",
143 "wrap": {
144 "@type": "koral:termGroup",
145 "operands": [
146 {
147 "@type": "koral:term",
148 "foundry": "custom",
149 "key": "PIDAT",
150 "layer": "p",
151 "match": "match:eq"
152 },
153 {
154 "@type": "koral:term",
155 "foundry": "custom",
156 "key": "AdjType",
157 "layer": "p",
158 "match": "match:eq",
159 "value": "Pdt"
160 }
161 ],
162 "relation": "relation:and"
163 }
164 }`,
165 },
166 {
167 name: "Invalid mapping ID",
168 mappingID: "nonexistent",
169 opts: MappingOptions{
170 Direction: AtoB,
171 },
172 input: `{
173 "@type": "koral:token",
174 "wrap": {
175 "@type": "koral:term",
176 "foundry": "opennlp",
177 "key": "PIDAT",
178 "layer": "p",
179 "match": "match:eq"
180 }
181 }`,
182 expectError: true,
183 },
184 {
185 name: "Invalid direction",
186 mappingID: "test-mapper",
187 opts: MappingOptions{
188 Direction: "invalid",
189 },
190 input: `{
191 "@type": "koral:token",
192 "wrap": {
193 "@type": "koral:term",
194 "foundry": "opennlp",
195 "key": "PIDAT",
196 "layer": "p",
197 "match": "match:eq"
198 }
199 }`,
200 expectError: true,
201 },
202 }
203
204 for _, tt := range tests {
205 t.Run(tt.name, func(t *testing.T) {
206 // Parse input JSON
207 var inputData interface{}
208 err := json.Unmarshal([]byte(tt.input), &inputData)
209 require.NoError(t, err)
210
211 // Apply mappings
212 result, err := m.ApplyMappings(tt.mappingID, tt.opts, inputData)
213 if tt.expectError {
214 assert.Error(t, err)
215 return
216 }
217 require.NoError(t, err)
218
219 // Parse expected JSON
220 var expectedData interface{}
221 err = json.Unmarshal([]byte(tt.expected), &expectedData)
222 require.NoError(t, err)
223
224 // Compare results
225 assert.Equal(t, expectedData, result)
226 })
227 }
228}