-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessor_test.go
More file actions
462 lines (433 loc) · 11.9 KB
/
accessor_test.go
File metadata and controls
462 lines (433 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2026 Joshua Jones <joshua.jones.software@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hl7
import "testing"
func TestLocationString(t *testing.T) {
tests := []struct {
name string
loc Location
want string
}{
{
name: "segment-field",
loc: Location{Segment: "MSH", Field: 9},
want: "MSH-9",
},
{
name: "segment-field.component",
loc: Location{Segment: "MSH", Field: 9, Component: 1},
want: "MSH-9.1",
},
{
name: "segment-field.component.subcomponent",
loc: Location{Segment: "PID", Field: 3, Component: 1, SubComponent: 2},
want: "PID-3.1.2",
},
{
name: "segment with index",
loc: Location{Segment: "OBX", SegmentIndex: 1, Field: 5},
want: "OBX(1)-5",
},
{
name: "field with repetition",
loc: Location{Segment: "PID", Field: 3, Repetition: 1},
want: "PID-3[1]",
},
{
name: "full specification",
loc: Location{Segment: "PID", Field: 3, Repetition: 1, Component: 4, SubComponent: 2},
want: "PID-3[1].4.2",
},
{
name: "two-char segment",
loc: Location{Segment: "NK", Field: 1},
want: "NK-1",
},
{
name: "component zero omitted",
loc: Location{Segment: "PID", Field: 3, Component: 0},
want: "PID-3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.loc.String()
if got != tt.want {
t.Errorf("String() = %q, want %q", got, tt.want)
}
// Round-trip: parsing the string should recover the original Location.
parsed, err := ParseLocation(got)
if err != nil {
t.Fatalf("ParseLocation(%q) failed: %v", got, err)
}
if parsed != tt.loc {
t.Errorf("round-trip: got %+v, want %+v", parsed, tt.loc)
}
})
}
}
func TestParseLocation(t *testing.T) {
tests := []struct {
name string
input string
want Location
wantErr bool
}{
{
name: "segment-field",
input: "MSH-9",
want: Location{Segment: "MSH", Field: 9},
},
{
name: "segment-field.component",
input: "MSH-9.1",
want: Location{Segment: "MSH", Field: 9, Component: 1},
},
{
name: "segment-field.component.subcomponent",
input: "PID-3.1.2",
want: Location{Segment: "PID", Field: 3, Component: 1, SubComponent: 2},
},
{
name: "segment with index",
input: "OBX(1)-5",
want: Location{Segment: "OBX", SegmentIndex: 1, Field: 5},
},
{
name: "field with repetition",
input: "PID-3[1]",
want: Location{Segment: "PID", Field: 3, Repetition: 1},
},
{
name: "full specification",
input: "PID-3[1].4.2",
want: Location{Segment: "PID", Field: 3, Repetition: 1, Component: 4, SubComponent: 2},
},
{
name: "two-char segment",
input: "NK-1",
want: Location{Segment: "NK", Field: 1},
},
{
name: "empty string",
input: "",
wantErr: true,
},
{
name: "no field",
input: "MSH",
wantErr: true,
},
{
name: "no segment",
input: "-9",
wantErr: true,
},
{
name: "bad field number",
input: "MSH-abc",
wantErr: true,
},
{
name: "bad segment index",
input: "OBX(abc)-5",
wantErr: true,
},
{
name: "unclosed paren",
input: "OBX(1-5",
wantErr: true,
},
{
name: "too many dots",
input: "PID-3.1.2.3",
wantErr: true,
},
{
name: "unclosed bracket",
input: "PID-3[1.4",
wantErr: true,
},
{
name: "segment too long",
input: "ABCD-1",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseLocation(tt.input)
if tt.wantErr {
if err == nil {
t.Errorf("expected error, got %+v", got)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tt.want {
t.Errorf("got %+v, want %+v", got, tt.want)
}
})
}
}
func TestMessageGet(t *testing.T) {
raw := []byte("MSH|^~\\&|SEND|FAC|REC|RFAC|20240101||ADT^A01^ADT_A01|MSG001|P|2.5.1\rPID|1||123^^&OID&ISO^MRN^MR~456^^^ACCT^AN||Doe^John^M\rOBX|1|ST|code1||val1\rOBX|2|ST|code2||val2")
msg, err := ParseMessage(raw)
if err != nil {
t.Fatalf("ParseMessage failed: %v", err)
}
tests := []struct {
location string
want string
}{
{"MSH-9", "ADT^A01^ADT_A01"},
{"MSH-9.1", "ADT"},
{"MSH-9.2", "A01"},
{"MSH-9.3", "ADT_A01"},
{"MSH-10", "MSG001"},
{"MSH-12", "2.5.1"},
{"PID-3.1", "123"},
{"PID-3.4", "MRN"},
{"PID-5.1", "Doe"},
{"PID-5.2", "John"},
{"PID-5.3", "M"},
{"OBX(0)-5", "val1"},
{"OBX(1)-5", "val2"},
{"OBX(0)-3.1", "code1"},
{"OBX(1)-3.1", "code2"},
// Repetitions.
{"PID-3[0].1", "123"},
{"PID-3[1].1", "456"},
{"PID-3[1].4", "ACCT"},
{"PID-3[1].5", "AN"},
{"PID-3[0]", "123^^&OID&ISO^MRN^MR"},
{"PID-3[1]", "456^^^ACCT^AN"},
// Subcomponents.
{"PID-3.3.1", ""},
{"PID-3.3.2", "OID"},
{"PID-3.3.3", "ISO"},
{"PID-3[0].3.2", "OID"},
{"PID-3[1].3.1", ""},
// Non-existent locations return empty string via Value.String().
{"ZZZ-1", ""},
{"PID-99", ""},
{"OBX(5)-1", ""},
{"PID-3[5]", ""},
{"PID-5.99", ""},
{"PID-3.1.99", ""},
}
for _, tt := range tests {
t.Run(tt.location, func(t *testing.T) {
got := msg.Get(tt.location).String()
if got != tt.want {
t.Errorf("Get(%q).String() = %q, want %q", tt.location, got, tt.want)
}
})
}
}
func TestMessageGetInvalidLocation(t *testing.T) {
raw := []byte("MSH|^~\\&|S|F|R|RF|20240101||ADT^A01|1|P|2.5.1")
msg, _ := ParseMessage(raw)
// Invalid location string should return zero Value (empty string), not panic.
if got := msg.Get("").String(); got != "" {
t.Errorf("Get(\"\").String() = %q, want empty", got)
}
if got := msg.Get("garbage").String(); got != "" {
t.Errorf("Get(\"garbage\").String() = %q, want empty", got)
}
}
func TestValueZeroValue(t *testing.T) {
var v Value
if !v.IsEmpty() {
t.Error("zero Value should be empty")
}
if v.IsNull() {
t.Error("zero Value should not be null")
}
if v.HasValue() {
t.Error("zero Value should not have value")
}
if v.String() != "" {
t.Errorf("zero Value.String() = %q, want empty", v.String())
}
if v.Bytes() != nil {
t.Errorf("zero Value.Bytes() = %v, want nil", v.Bytes())
}
}
func TestValueFromGet(t *testing.T) {
raw := []byte("MSH|^~\\&|SEND|F|R|RF|20240101||ADT^A01|1|P|2.5.1\rPID|||12345")
msg, _ := ParseMessage(raw)
// Present field: Bytes() returns raw bytes without unescaping.
v := msg.Get("MSH-3")
if string(v.Bytes()) != "SEND" {
t.Errorf("Get(MSH-3).Bytes() = %q, want SEND", v.Bytes())
}
if v.String() != "SEND" {
t.Errorf("Get(MSH-3).String() = %q, want SEND", v.String())
}
if v.IsEmpty() {
t.Error("Get(MSH-3).IsEmpty() should be false")
}
if v.IsNull() {
t.Error("Get(MSH-3).IsNull() should be false")
}
if !v.HasValue() {
t.Error("Get(MSH-3).HasValue() should be true")
}
// Not-found location: Bytes() returns nil.
vMissing := msg.Get("ZZZ-1")
if vMissing.Bytes() != nil {
t.Errorf("Get(ZZZ-1).Bytes() = %v, want nil", vMissing.Bytes())
}
if !vMissing.IsEmpty() {
t.Error("Get(ZZZ-1).IsEmpty() should be true")
}
// Invalid location: also returns zero Value.
vInvalid := msg.Get("invalid")
if vInvalid.Bytes() != nil {
t.Errorf("Get(invalid).Bytes() = %v, want nil", vInvalid.Bytes())
}
}
func TestParseLocationEdgeCases(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
wantLoc Location // asserted when non-zero (Segment != "")
}{
{"EmptySegmentIndex", "OBX()-5", true, Location{}},
{"SingleCharSegment", "X-1", true, Location{}},
{"BadRepIndex", "PID-3[abc]", true, Location{}},
{"BadComponentNum", "PID-3.abc", true, Location{}},
{"BadSubcomponentNum", "PID-3.1.abc", true, Location{}},
{"EmptyBrackets", "PID-3[]", true, Location{}},
{"EmptyFieldPart", "PID-", true, Location{}},
{"TrailingGarbageAfterRep", "PID-3[1]x", true, Location{}},
{"TrailingGarbageAfterSegIndex", "OBX(1)x-5", true, Location{}},
// ZeroComponent documents that ".0" is accepted and treated as
// "component not specified" (equivalent to omitting the component).
{"ZeroComponent", "PID-3.0", false, Location{Segment: "PID", Field: 3, Component: 0}},
// Trailing dot after field: accepted, Component stays 0.
{"TrailingDotAfterField", "PID-3.", false, Location{Segment: "PID", Field: 3, Component: 0}},
// Trailing dot after repetition: accepted, Component stays 0.
{"TrailingDotAfterRepetition", "PID-3[1].", false, Location{Segment: "PID", Field: 3, Repetition: 1, Component: 0}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseLocation(tt.input)
if tt.wantErr {
if err == nil {
t.Errorf("expected error for %q, got %+v", tt.input, got)
}
return
}
if err != nil {
t.Fatalf("unexpected error for %q: %v", tt.input, err)
}
if tt.wantLoc.Segment != "" && got != tt.wantLoc {
t.Errorf("ParseLocation(%q) = %+v, want %+v", tt.input, got, tt.wantLoc)
}
})
}
}
func TestGetVeryHighSegmentIndex(t *testing.T) {
raw := []byte("MSH|^~\\&|S|F|R|RF|20240101||ADT^A01|1|P|2.5.1\rOBX|1|NM|code||42")
msg, err := ParseMessage(raw)
if err != nil {
t.Fatalf("ParseMessage: %v", err)
}
// OBX(999) does not exist — should return zero Value, no panic.
if got := msg.Get("OBX(999)-5").String(); got != "" {
t.Errorf("Get(OBX(999)-5).String() = %q, want empty", got)
}
}
func TestGetSegmentWithNoFields(t *testing.T) {
// A segment consisting of only the type name with no field separator.
raw := []byte("MSH|^~\\&|S|F|R|RF|20240101||ADT^A01|1|P|2.5.1\rZZZ")
msg, err := ParseMessage(raw)
if err != nil {
t.Fatalf("ParseMessage: %v", err)
}
// ZZZ has no fields at all — accessing ZZZ-1 should return zero Value, no panic.
if got := msg.Get("ZZZ-1").String(); got != "" {
t.Errorf("Get(ZZZ-1).String() = %q, want empty", got)
}
}
func TestGetSubcomponentEmpty(t *testing.T) {
raw := []byte("MSH|^~\\&|S|F|R|RF|20240101||ADT^A01|1|P|2.5.1\rPID|1||123")
msg, _ := ParseMessage(raw)
// Accessing a subcomponent that doesn't exist returns zero Value.
if got := msg.Get("PID-3.1.99").String(); got != "" {
t.Errorf("Get(PID-3.1.99).String() = %q, want empty", got)
}
}
func TestMessageGetPresentButEmptyField(t *testing.T) {
// PID-2 is between two consecutive field separators (||) — present but empty.
raw := []byte("MSH|^~\\&|S|F|R|RF|20240101||ADT^A01|1|P|2.5.1\rPID|1||123")
msg, err := ParseMessage(raw)
if err != nil {
t.Fatalf("ParseMessage: %v", err)
}
v := msg.Get("PID-2")
if !v.IsEmpty() {
t.Errorf("Get(PID-2).IsEmpty() = false, want true")
}
if v.String() != "" {
t.Errorf("Get(PID-2).String() = %q, want empty", v.String())
}
// A present-but-empty field returns a zero Value (nil raw): the early-return
// in getByLocation when field.IsEmpty() is true yields a zero Value.
if v.Raw() != nil {
t.Errorf("Get(PID-2).Raw() = %v, want nil", v.Raw())
}
// Contrast: PID-3 has a value.
if got := msg.Get("PID-3").String(); got != "123" {
t.Errorf("Get(PID-3).String() = %q, want 123", got)
}
}
func BenchmarkLocationString(b *testing.B) {
l := Location{
Segment: "PID",
SegmentIndex: 123,
Field: 123,
Repetition: 123,
Component: 123,
SubComponent: 123,
}
for b.Loop() {
_ = l.String()
}
}
func BenchmarkParseLocation(b *testing.B) {
inputs := []struct {
name string
input string
}{
{"Simple", "MSH-9"},
{"Component", "PID-3.1"},
{"Subcomponent", "PID-3.1.2"},
{"Full", "PID-3[1].4.2"},
{"SegmentIndex", "OBX(1)-5"},
}
for _, tt := range inputs {
b.Run(tt.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = ParseLocation(tt.input)
}
})
}
}