Files
kurious/internal/kurious/adapters/sqlite_organization_repository_test.go
2024-04-19 00:13:03 +03:00

129 lines
3.4 KiB
Go

package adapters
import (
"slices"
"strconv"
"testing"
"git.loyso.art/frx/kurious/internal/common/nullable"
"git.loyso.art/frx/kurious/internal/kurious/domain"
"github.com/stretchr/testify/suite"
)
func TestSqilteOrganizations(t *testing.T) {
suite.Run(t, new(sqliteOrganzationRepositorySuite))
}
type sqliteOrganzationRepositorySuite struct {
sqliteBaseSuite
}
func (s *sqliteOrganzationRepositorySuite) TearDownTest() {
_ = s.connection.db.MustExecContext(s.ctx, "DELETE FROM organizations")
}
func (s *sqliteOrganzationRepositorySuite) TestList() {
const itemscount = 3
orgsdb := make([]domain.Organization, 0, itemscount)
baseOrg := domain.Organization{
Alias: "test-alias",
Name: "test-name",
Site: "test-site",
LogoLink: "test-logo",
}
for i := 0; i < itemscount; i++ {
nextitem := baseOrg
iStr := strconv.Itoa(i)
nextitem.ID = "test-id-" + iStr
nextitem.ExternalID.Set("test-ext-id-" + iStr)
gotOrg, err := s.connection.Organization().Create(s.ctx, domain.CreateOrganizationParams{
ID: nextitem.ID,
ExternalID: nextitem.ExternalID,
Alias: nextitem.Alias,
Name: nextitem.Name,
Site: nextitem.Site,
LogoLink: nextitem.LogoLink,
})
s.NoError(err)
orgsdb = append(orgsdb, gotOrg)
}
gotOrgs, err := s.connection.Organization().List(s.ctx, domain.ListOrganizationsParams{})
s.NoError(err)
compareF := func(lhs, rhs domain.Organization) int {
if lhs.ID < rhs.ID {
return -1
} else if lhs.ID > rhs.ID {
return 1
} else {
return 0
}
}
slices.SortFunc(gotOrgs, compareF)
for i := range gotOrgs {
s.NotEmpty(gotOrgs[i].CreatedAt)
s.NotEmpty(gotOrgs[i].UpdatedAt)
s.Empty(gotOrgs[i].DeletedAt)
orgsdb[i].CreatedAt = gotOrgs[i].CreatedAt
orgsdb[i].UpdatedAt = gotOrgs[i].UpdatedAt
orgsdb[i].DeletedAt = gotOrgs[i].DeletedAt
}
s.ElementsMatch(orgsdb, gotOrgs)
}
func (s *sqliteOrganzationRepositorySuite) TestGet() {
var orgdb organizationDB
err := s.connection.db.GetContext(
s.ctx, &orgdb,
`INSERT INTO organizations (`+organizationColumnsStr+`)`+
` VALUES ("test-id", "test-ext-id", "test-alias", "test-name", "test-site", "test-logo", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, NULL)`+
` RETURNING `+organizationColumnsStr,
)
s.NoError(err)
expectedOrganization := orgdb.AsDomain()
getParams := domain.GetOrganizationParams{
ID: nullable.NewValue(orgdb.ID),
}
gotOrganization, err := s.connection.Organization().Get(s.ctx, getParams)
s.NoError(err)
s.Equal(expectedOrganization, gotOrganization)
}
func (s *sqliteOrganzationRepositorySuite) TestCreate() {
expectedOrganization := domain.Organization{
ID: "test-id",
ExternalID: nullable.NewValue("test-ext-id"),
Alias: "test-alias",
Name: "test-name",
Site: "test-site",
LogoLink: "test-logo",
}
gotOrganization, err := s.connection.Organization().Create(s.ctx, domain.CreateOrganizationParams{
ID: expectedOrganization.ID,
ExternalID: expectedOrganization.ExternalID,
Alias: expectedOrganization.Alias,
Name: expectedOrganization.Name,
Site: expectedOrganization.Site,
LogoLink: expectedOrganization.LogoLink,
})
s.NoError(err)
s.NotEmpty(gotOrganization.CreatedAt)
s.NotEmpty(gotOrganization.UpdatedAt)
s.Empty(gotOrganization.DeletedAt)
expectedOrganization.CreatedAt = gotOrganization.CreatedAt
expectedOrganization.UpdatedAt = gotOrganization.UpdatedAt
s.Equal(expectedOrganization, gotOrganization)
}