Files
kurious/internal/kurious/adapters/sqlite_course_repository_test.go

140 lines
3.7 KiB
Go

package adapters
import (
"errors"
"strconv"
"testing"
"time"
cerrors "git.loyso.art/frx/kurious/internal/common/errors"
"git.loyso.art/frx/kurious/internal/common/nullable"
"git.loyso.art/frx/kurious/internal/kurious/domain"
"github.com/stretchr/testify/suite"
)
func TestSqliteCourseRepository(t *testing.T) {
suite.Run(t, new(sqliteCourseRepositorySuite))
}
type sqliteCourseRepositorySuite struct {
sqliteBaseSuite
}
func (s *sqliteCourseRepositorySuite) TestCreateCourse() {
expcourse := domain.Course{
ID: "test-id",
ExternalID: nullable.NewValue("ext-id"),
Name: "test-name",
SourceType: domain.SourceTypeParsed,
SourceName: nullable.NewValue("test-source"),
ThematicID: "test-thematic",
LearningTypeID: "test-learning",
OrganizationID: "test-org-id",
OriginLink: "test-link",
ImageLink: "test-image-link",
Description: "description",
FullPrice: 123,
Discount: 321,
Duration: time.Second * 360,
StartsAt: time.Date(2020, 10, 01, 11, 22, 33, 0, time.UTC),
Thematic: "",
LearningType: "",
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nullable.Value[time.Time]{},
}
cr := s.connection.CourseRepository()
_, err := cr.Create(s.ctx, domain.CreateCourseParams{
ID: "test-id",
ExternalID: nullable.NewValue("ext-id"),
Name: "test-name",
SourceType: domain.SourceTypeParsed,
SourceName: nullable.NewValue("test-source"),
CourseThematic: "test-thematic",
LearningType: "test-learning",
OrganizationID: "test-org-id",
OriginLink: "test-link",
ImageLink: "test-image-link",
Description: "description",
FullPrice: 123,
Discount: 321,
Duration: time.Second * 360,
StartsAt: time.Date(2020, 10, 01, 11, 22, 33, 0, time.UTC),
})
s.Require().NoError(err)
gotCourse, err := cr.Get(s.ctx, expcourse.ID)
s.Require().NoError(err)
s.Require().NotEmpty(gotCourse.CreatedAt)
s.Require().NotEmpty(gotCourse.UpdatedAt)
s.Require().Empty(gotCourse.DeletedAt)
expcourse.CreatedAt = gotCourse.CreatedAt
expcourse.UpdatedAt = gotCourse.UpdatedAt
s.Require().Equal(expcourse, gotCourse)
}
func (s *sqliteCourseRepositorySuite) TestListLimitOffset() {
const coursecount = 9
const listparts = 3
basecourse := domain.CreateCourseParams{
SourceType: domain.SourceTypeManual,
}
cr := s.connection.CourseRepository()
for i := 0; i < coursecount; i++ {
basecourse.ID = strconv.Itoa(i)
_, err := cr.Create(s.ctx, basecourse)
s.NoError(err)
}
params := domain.ListCoursesParams{
Limit: coursecount / listparts,
}
for i := 0; i < listparts; i++ {
result, err := cr.List(s.ctx, params)
s.NoError(err)
s.Len(result.Courses, listparts)
params.Offset += listparts
}
result, err := cr.List(s.ctx, params)
s.NoError(err)
s.Empty(result.Courses)
}
func (s *sqliteCourseRepositorySuite) TestCreateBatchPartialFailure() {
cr := s.connection.CourseRepository()
baseParams := domain.CreateCourseParams{
SourceType: domain.SourceTypeManual,
}
first := baseParams
first.ID = "dup-id"
duplicate := baseParams
duplicate.ID = "dup-id"
err := cr.CreateBatch(s.ctx, first, duplicate)
s.Require().Error(err)
result, listErr := cr.List(s.ctx, domain.ListCoursesParams{})
s.Require().NoError(listErr)
s.Empty(result.Courses, "partial batch should be rolled back, not committed")
s.Zero(result.Count, "partial batch should be rolled back, not committed")
}
func (s *sqliteCourseRepositorySuite) TestGetNotFound() {
cr := s.connection.CourseRepository()
_, err := cr.Get(s.ctx, "does-not-exist")
s.Require().Error(err)
s.True(errors.Is(err, cerrors.ErrNotFound), "expected ErrNotFound, got %v", err)
}