Test [Exporter] JSON Exporter (#10)
- [+] feat(export): add unit test for JSONExporter functionality in expoter_test.go Reviewed-on: #10 Co-authored-by: H0llyW00dzZ <h0llyw00dzz@pm.me> Co-committed-by: H0llyW00dzZ <h0llyw00dzz@pm.me>
This commit is contained in:
parent
1574f865c9
commit
f22bed1f12
49
export/expoter_test.go
Normal file
49
export/expoter_test.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// Copyright (c) 2025 H0llyW00dzZ All rights reserved.
|
||||||
|
//
|
||||||
|
// By accessing or using this software, you agree to be bound by the terms
|
||||||
|
// of the License Agreement, which you can find at LICENSE files.
|
||||||
|
|
||||||
|
package export_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.b0zal.io/H0llyW00dzZ/imap/export"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/valyala/bytebufferpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJSONExporter(t *testing.T) {
|
||||||
|
// Create a sample message
|
||||||
|
messages := []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"id": "1",
|
||||||
|
"from": []string{"contact@example.org"},
|
||||||
|
"subject": "A little message, just for you",
|
||||||
|
"body": "Hi there :)",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a buffer from the pool
|
||||||
|
buffer := bytebufferpool.Get()
|
||||||
|
defer func() {
|
||||||
|
buffer.Reset() // Reset the buffer to prevent data leaks
|
||||||
|
bytebufferpool.Put(buffer) // Return the buffer to the pool
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Create a JSONExporter with the default encoder
|
||||||
|
exporter := &export.JSONExporter{
|
||||||
|
Encoder: export.DefaultJSONEncoder,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export the messages to the buffer
|
||||||
|
err := exporter.Export(messages, buffer)
|
||||||
|
assert.NoError(t, err, "Failed to export messages")
|
||||||
|
|
||||||
|
// Define the expected JSON output
|
||||||
|
expectedJSON := `[{"body":"Hi there :)","from":["contact@example.org"],"id":"1","subject":"A little message, just for you"}]
|
||||||
|
`
|
||||||
|
|
||||||
|
// Assert that the buffer content matches the expected JSON
|
||||||
|
assert.Equal(t, expectedJSON, buffer.String(), "Unexpected JSON output")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user