Test [Exporter] JSON Exporter

- [+] feat(export): add unit test for JSONExporter functionality in expoter_test.go
This commit is contained in:
H0llyW00dzZ 2025-01-24 16:57:55 +07:00
parent 1574f865c9
commit db35b93a30
Signed by: H0llyW00dzZ
GPG Key ID: A0F9424A7002343A

49
export/expoter_test.go Normal file
View 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")
}