Skip to content
Snippets Groups Projects
emission_queue_test.go 1.49 KiB
Newer Older
package emissionqueue_test

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"orus.io/orus-io/go-orusapi/database"
	"orus.io/orus-io/go-orusapi/testutils"

	"orus.io/orus-io/go-orusapi/xbus/emissionqueue"
	"orus.io/orus-io/go-orusapi/xbus/emissionqueue/migration"
)

func TestEmissionQueue(t *testing.T) {
	log := testutils.GetLogger(t)
	db := database.GetTestDB(context.TODO(), t, migration.Source)
	defer db.Close()

	queue := emissionqueue.NewEmissionQueue()

	assert.NotNil(t, queue)

	sqlHelper := database.NewSQLHelper(context.Background(), db, log)

	require.NoError(t, queue.AddMessage(&sqlHelper, emissionqueue.NewMessage("AddMessage", "test", []byte("abcde"))))
	require.NoError(t, queue.Add(&sqlHelper, "Add", "test", "fghij"))

	var messages []emissionqueue.Message
	require.NoError(t,
		database.Select(db, &messages,
			database.SQ.
				Select("id", "related_to", "msgtype", "content").
				From(emissionqueue.TableName).
				OrderBy("id"),
			&log))

	var relatedTo []string
	require.Len(t, messages, 2)
	if assert.NoError(t, messages[0].RelatedTo.AssignTo(&relatedTo)) {
		assert.Equal(t, []string{"AddMessage"}, relatedTo)
	}
	assert.Equal(t, "test", messages[0].MsgType)
	assert.Equal(t, "abcde", string(messages[0].Content))
	if assert.NoError(t, messages[1].RelatedTo.AssignTo(&relatedTo)) {
		assert.Equal(t, []string{"Add"}, relatedTo)
	}
	assert.Equal(t, "test", messages[1].MsgType)
	assert.Equal(t, `"fghij"`, string(messages[1].Content))
}