-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TECH Extract xml2json test data into files for clarity
- Loading branch information
1 parent
d086312
commit e796262
Showing
9 changed files
with
70 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,47 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"os" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/antchfx/xmlquery" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNodeToJSON(t *testing.T) { | ||
func TestXmlToJSON(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
depth int | ||
expected string | ||
unformattedFile string | ||
expectedFile string | ||
depth int | ||
}{ | ||
{ | ||
name: "Simple XML", | ||
input: "<root><child>value</child></root>", | ||
depth: -1, | ||
expected: `{"root":{"child":"value"}}`, | ||
}, | ||
{ | ||
name: "XML with attributes", | ||
input: "<root attr=\"value\"><child>text</child></root>", | ||
depth: -1, | ||
expected: `{"root":{"@attr":"value","child":"text"}}`, | ||
}, | ||
{ | ||
name: "XML with mixed content", | ||
input: "<root>\n text <child>value</child>\n more text\n</root>", | ||
depth: -1, | ||
expected: `{"root":{"#text":"text\nmore text","child":"value"}}`, | ||
}, | ||
{ | ||
name: "Depth limited XML", | ||
input: "<root><child1><grandchild>value</grandchild></child1><child2>text</child2></root>", | ||
depth: 2, | ||
expected: `{"root":{"child1":{"grandchild":"value"},"child2":"text"}}`, | ||
}, | ||
{ | ||
name: "Depth 1 XML", | ||
input: "<root><child1><grandchild>value</grandchild></child1><child2>text</child2></root>", | ||
depth: 1, | ||
expected: `{"root":{"child1":"value","child2":"text"}}`, | ||
}, | ||
{ | ||
name: "Depth 0 XML", | ||
input: "<root><child1><grandchild>value</grandchild></child1><child2>text</child2></root>", | ||
depth: 0, | ||
expected: `{"root":"value\ntext"}`, | ||
}, | ||
{ | ||
name: "mixed text and xml", | ||
input: `Thank you | ||
<thinking> | ||
1. woop | ||
</thinking> | ||
Bye`, | ||
expected: `{"#text":"Thank you\nBye","thinking":"1. woop"}`, | ||
}, | ||
{"unformatted.xml", "formatted.json", -1}, | ||
{"unformatted2.xml", "formatted2.json", -1}, | ||
{"unformatted3.xml", "formatted3.json", -1}, | ||
{"unformatted4.xml", "formatted4.json", 1}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
doc, err := xmlquery.Parse(strings.NewReader(tt.input)) | ||
if err != nil { | ||
t.Fatalf("Failed to parse XML: %v", err) | ||
} | ||
|
||
result := NodeToJSON(doc, tt.depth) | ||
resultJSON, err := json.Marshal(result) | ||
if err != nil { | ||
t.Fatalf("Failed to marshal result to JSON: %v", err) | ||
} | ||
|
||
var resultMap, expectedMap map[string]interface{} | ||
err = json.Unmarshal(resultJSON, &resultMap) | ||
if err != nil { | ||
t.Fatalf("Failed to unmarshal result JSON: %v", err) | ||
} | ||
err = json.Unmarshal([]byte(tt.expected), &expectedMap) | ||
if err != nil { | ||
t.Fatalf("Failed to unmarshal expected JSON: %v", err) | ||
} | ||
|
||
t.Log(string(resultJSON)) | ||
if diff := cmp.Diff(expectedMap, resultMap); diff != "" { | ||
t.Errorf("NodeToJSON mismatch (-want +got):\n%s", diff) | ||
} | ||
}) | ||
for _, testCase := range tests { | ||
inputFileName := path.Join("..", "..", "test", "data", "xml2json", testCase.unformattedFile) | ||
unformattedXmlReader := getFileReader(inputFileName) | ||
|
||
outputFileName := path.Join("..", "..", "test", "data", "xml2json", testCase.expectedFile) | ||
data, jsonReadErr := os.ReadFile(outputFileName) | ||
assert.Nil(t, jsonReadErr) | ||
expectedJson := string(data) | ||
|
||
node, parseErr := xmlquery.Parse(unformattedXmlReader) | ||
assert.Nil(t, parseErr) | ||
result := NodeToJSON(node, testCase.depth) | ||
jsonData, jsonMarshalErr := json.Marshal(result) | ||
assert.Nil(t, jsonMarshalErr) | ||
|
||
output := new(strings.Builder) | ||
formatErr := FormatJson(bytes.NewReader(jsonData), output, " ", ColorsDisabled) | ||
assert.Nil(t, formatErr) | ||
assert.Equal(t, expectedJson, output.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"root": { | ||
"child": "value" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"root": { | ||
"@attr": "value", | ||
"child": "text" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"root": { | ||
"#text": "text\nmore text", | ||
"child": "value" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"root": { | ||
"child1": "value", | ||
"child2": "text" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<root> | ||
<child>value</child> | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<root attr="value"> | ||
<child>text</child> | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<root> | ||
text <child>value</child> | ||
more text | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<root> | ||
<child1> | ||
<grandchild>value</grandchild> | ||
</child1> | ||
<child2>text</child2> | ||
</root> |