From f22bed1f127ecdf9ebaf898b4b3170e3893342b4 Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Fri, 24 Jan 2025 09:58:03 +0000 Subject: [PATCH] Test [Exporter] JSON Exporter (#10) - [+] feat(export): add unit test for JSONExporter functionality in expoter_test.go Reviewed-on: https://git.b0zal.io/H0llyW00dzZ/imap/pulls/10 Co-authored-by: H0llyW00dzZ Co-committed-by: H0llyW00dzZ --- export/expoter_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 export/expoter_test.go diff --git a/export/expoter_test.go b/export/expoter_test.go new file mode 100644 index 0000000..2dad30a --- /dev/null +++ b/export/expoter_test.go @@ -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") +}