Repository: codenotary/immudb
File: test/document_storage_tests/documents_tests/insert_documents_test.go (Go)

Code before:

package main

import (
	"testing"

	"github.com/codenotary/immudb/test/document_storage_tests/documents_tests/actions"
	"github.com/gavv/httpexpect/v2"
	"github.com/google/uuid"
	"github.com/stretchr/testify/suite"
)

type InsertDocumentsTestSuite struct {
	suite.Suite
	expect     *httpexpect.Expect
	sessionID  string
	collection *httpexpect.Object
}

func (s *InsertDocumentsTestSuite) SetupTest() {
	s.expect, s.sessionID = actions.OpenSession(s.T())
	s.collection = actions.CreateCollectionWithNameAndMultipleFields(s.expect, s.sessionID, uuid.New().String())
}

func (s *InsertDocumentsTestSuite) TestInsertOneDocumentWithMultipleFields() {
	document := map[string]interface{}{
		"birth_date": "1964-06-02",
		"first_name": "Bezalel",
		"last_name":  "Simmel",
		"gender":     "F",
		"hire_date":  "1985-11-21",
	}

	documentFound := actions.InsertOneDocumentWithMultipleFields(s.expect, s.sessionID, s.collection, document)

	documentFound.Keys().ContainsOnly("_id", "birth_date", "first_name", "last_name", "gender", "hire_date")
	documentFound.Value("birth_date").IsEqual(document["birth_date"])
	documentFound.Value("first_name").IsEqual(document["first_name"])
	documentFound.Value("last_name").IsEqual(document["last_name"])
	documentFound.Value("gender").IsEqual(document["gender"])
	documentFound.Value("hire_date").IsEqual(document["hire_date"])
}

func TestInsertDocumentsSuite(t *testing.T) {
	suite.Run(t, new(InsertDocumentsTestSuite))
}


Review comment (suggestion):
this is not a real document, as eventually what you have is map[string]string (all the values will be considered as string). Make it a proper test structure and then json.Marshal

Code after:

package main

import (
	"testing"

	"github.com/codenotary/immudb/test/document_storage_tests/documents_tests/actions"
	"github.com/codenotary/immudb/test/document_storage_tests/documents_tests/models"
	"github.com/gavv/httpexpect/v2"
	"github.com/google/uuid"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
)

type InsertDocumentsTestSuite struct {
	suite.Suite
	expect     *httpexpect.Expect
	sessionID  string
	collection *httpexpect.Object
}

func (s *InsertDocumentsTestSuite) SetupTest() {
	s.expect, s.sessionID = actions.OpenSession(s.T())
	s.collection = actions.CreateCollectionWithNameAndMultipleFields(s.expect, s.sessionID, uuid.New().String())
}

func (s *InsertDocumentsTestSuite) TestInsertOneDocumentWithMultipleFields() {
	documentModel := actions.InsertOneDocumentWithMultipleFields(s.expect, s.sessionID, s.collection)

	fieldComparison := models.FieldComparison{}
	fieldComparison.Field = "first_name"
	fieldComparison.Operator = "EQ"
	fieldComparison.Value = documentModel.FirstName
	documentFound := actions.SearchDocuments(s.expect, s.sessionID, s.collection, fieldComparison)

	documentFound.Keys().ContainsOnly("_id", "birth_date", "first_name", "last_name", "gender", "hire_date")

	var employee models.Employee

	documentFound.Decode(&employee)

	assert.Equal(s.T(), employee.BirthDate, "1964-06-02")
	assert.Equal(s.T(), employee.FirstName, "Bezalel")
	assert.Equal(s.T(), employee.LastName, "Simmel")
	assert.Equal(s.T(), employee.Gender, "F")
	assert.Equal(s.T(), employee.HireDate, "1985-11-21")
}

func TestInsertDocumentsSuite(t *testing.T) {
	suite.Run(t, new(InsertDocumentsTestSuite))
}