Skip to content

Commit

Permalink
adding course_phase tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niclasheun committed Dec 4, 2024
1 parent 0365db9 commit f603c1e
Show file tree
Hide file tree
Showing 5 changed files with 461 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/coursePhase/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func setupCoursePhaseRouter(router *gin.RouterGroup) {
coursePhase := router.Group("/course_phases")
coursePhase.GET("/:uuid", getCoursePhaseByID)
coursePhase.POST("/", createCoursePhase)
coursePhase.POST("", createCoursePhase)
coursePhase.PUT("/:uuid", updateCoursePhase)
}

Expand Down
150 changes: 150 additions & 0 deletions server/coursePhase/router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package coursePhase

import (
"bytes"
"context"
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/niclasheun/prompt2.0/coursePhase/coursePhaseDTO"
"github.com/niclasheun/prompt2.0/meta"
"github.com/niclasheun/prompt2.0/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type RouterTestSuite struct {
suite.Suite
router *gin.Engine
ctx context.Context
cleanup func()
coursePhaseService CoursePhaseService
}

func (suite *RouterTestSuite) SetupSuite() {
suite.ctx = context.Background()

// Set up PostgreSQL container
testDB, cleanup, err := testutils.SetupTestDB(suite.ctx, "../database_dumps/course_phase_test.sql")
if err != nil {
log.Fatalf("Failed to set up test database: %v", err)
}

suite.cleanup = cleanup
suite.coursePhaseService = CoursePhaseService{
queries: *testDB.Queries,
conn: testDB.Conn,
}
CoursePhaseServiceSingleton = &suite.coursePhaseService

suite.router = setupRouter()
}

func (suite *RouterTestSuite) TearDownSuite() {
suite.cleanup()
}

func setupRouter() *gin.Engine {
router := gin.Default()
api := router.Group("/api")
setupCoursePhaseRouter(api)
return router
}

func (suite *RouterTestSuite) TestGetCoursePhaseByID() {
req := httptest.NewRequest(http.MethodGet, "/api/course_phases/3d1f3b00-87f3-433b-a713-178c4050411b", nil)
w := httptest.NewRecorder()

suite.router.ServeHTTP(w, req)

assert.Equal(suite.T(), http.StatusOK, w.Code)

var coursePhase coursePhaseDTO.CoursePhase
err := json.Unmarshal(w.Body.Bytes(), &coursePhase)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "Test", coursePhase.Name, "Expected course phase name to match")
assert.False(suite.T(), coursePhase.IsInitialPhase, "Expected course phase to not be an initial phase")
assert.Equal(suite.T(), uuid.MustParse("3f42d322-e5bf-4faa-b576-51f2cab14c2e"), coursePhase.CourseID, "Expected CourseID to match")
assert.Equal(suite.T(), uuid.MustParse("7dc1c4e8-4255-4874-80a0-0c12b958744b"), coursePhase.CoursePhaseTypeID, "Expected CoursePhaseTypeID to match")
assert.Equal(suite.T(), "test-value", coursePhase.MetaData["test-key"], "Expected MetaData to match")
}

func (suite *RouterTestSuite) TestCreateCoursePhase() {
jsonData := `{"new_key": "new_value"}`
var metaData meta.MetaData
err := json.Unmarshal([]byte(jsonData), &metaData)
assert.NoError(suite.T(), err)

newCoursePhase := coursePhaseDTO.CreateCoursePhase{
CourseID: uuid.MustParse("3f42d322-e5bf-4faa-b576-51f2cab14c2e"),
Name: "New Phase",
IsInitialPhase: false,
MetaData: metaData,
CoursePhaseTypeID: uuid.MustParse("7dc1c4e8-4255-4874-80a0-0c12b958744c"),
}

body, _ := json.Marshal(newCoursePhase)
req := httptest.NewRequest(http.MethodPost, "/api/course_phases", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()

suite.router.ServeHTTP(w, req)

assert.Equal(suite.T(), http.StatusCreated, w.Code)

var createdCoursePhase coursePhaseDTO.CoursePhase
err = json.Unmarshal(w.Body.Bytes(), &createdCoursePhase)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "New Phase", createdCoursePhase.Name, "Expected course phase name to match")
assert.False(suite.T(), createdCoursePhase.IsInitialPhase, "Expected course phase to not be an initial phase")
assert.Equal(suite.T(), newCoursePhase.CourseID, createdCoursePhase.CourseID, "Expected CourseID to match")
assert.Equal(suite.T(), newCoursePhase.MetaData, createdCoursePhase.MetaData, "Expected MetaData to match")
assert.Equal(suite.T(), newCoursePhase.CoursePhaseTypeID, createdCoursePhase.CoursePhaseTypeID, "Expected CoursePhaseTypeID to match")
}

func (suite *RouterTestSuite) TestUpdateCoursePhase() {
jsonData := `{"updated_key": "updated_value"}`
var metaData meta.MetaData
err := json.Unmarshal([]byte(jsonData), &metaData)
assert.NoError(suite.T(), err)

updatedCoursePhase := coursePhaseDTO.UpdateCoursePhase{
ID: uuid.MustParse("3d1f3b00-87f3-433b-a713-178c4050411b"),
Name: "Updated Phase",
IsInitialPhase: false,
MetaData: metaData,
}

body, _ := json.Marshal(updatedCoursePhase)
req := httptest.NewRequest(http.MethodPut, "/api/course_phases/3d1f3b00-87f3-433b-a713-178c4050411b", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()

suite.router.ServeHTTP(w, req)

assert.Equal(suite.T(), http.StatusOK, w.Code)

// Verify the update by fetching the updated course phase
fetchReq := httptest.NewRequest(http.MethodGet, "/api/course_phases/3d1f3b00-87f3-433b-a713-178c4050411b", nil)
fetchRes := httptest.NewRecorder()
suite.router.ServeHTTP(fetchRes, fetchReq)

assert.Equal(suite.T(), http.StatusOK, fetchRes.Code)

var fetchedCoursePhase coursePhaseDTO.CoursePhase
err = json.Unmarshal(fetchRes.Body.Bytes(), &fetchedCoursePhase)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "Updated Phase", fetchedCoursePhase.Name, "Expected updated course phase name to match")
assert.False(suite.T(), fetchedCoursePhase.IsInitialPhase, "Expected course phase not to be an initial phase")
assert.Equal(suite.T(), updatedCoursePhase.MetaData["updated_key"], fetchedCoursePhase.MetaData["updated_key"], "Expected updated metadata to match")
assert.Equal(suite.T(), "test-value", fetchedCoursePhase.MetaData["test-key"], "Expected existing metadata to match")
}

func TestRouterTestSuite(t *testing.T) {
suite.Run(t, new(RouterTestSuite))
}
107 changes: 107 additions & 0 deletions server/coursePhase/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package coursePhase

import (
"context"
"encoding/json"
"testing"

"github.com/google/uuid"
"github.com/niclasheun/prompt2.0/coursePhase/coursePhaseDTO"
"github.com/niclasheun/prompt2.0/meta"
"github.com/niclasheun/prompt2.0/testutils"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type CoursePhaseTestSuite struct {
suite.Suite
ctx context.Context
cleanup func()
coursePhaseService CoursePhaseService
}

func (suite *CoursePhaseTestSuite) SetupSuite() {
suite.ctx = context.Background()

// Set up PostgreSQL container
testDB, cleanup, err := testutils.SetupTestDB(suite.ctx, "../database_dumps/course_phase_test.sql")
if err != nil {
log.Fatalf("Failed to set up test database: %v", err)
}

suite.cleanup = cleanup
suite.coursePhaseService = CoursePhaseService{
queries: *testDB.Queries,
conn: testDB.Conn,
}
CoursePhaseServiceSingleton = &suite.coursePhaseService
}

func (suite *CoursePhaseTestSuite) TearDownSuite() {
suite.cleanup()
}

func (suite *CoursePhaseTestSuite) TestGetCoursePhaseByID() {
id := uuid.MustParse("3d1f3b00-87f3-433b-a713-178c4050411b")
coursePhase, err := GetCoursePhaseByID(suite.ctx, id)

assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "Test", coursePhase.Name, "Expected course phase name to match")
assert.False(suite.T(), coursePhase.IsInitialPhase, "Expected course phase to not be an initial phase")
assert.Equal(suite.T(), id, coursePhase.ID, "Expected course phase ID to match")
}

func (suite *CoursePhaseTestSuite) TestUpdateCoursePhase() {
id := uuid.MustParse("3d1f3b00-87f3-433b-a713-178c4050411b")
jsonData := `{"updated_key": "updated_value"}`
var metaData meta.MetaData
err := json.Unmarshal([]byte(jsonData), &metaData)
assert.NoError(suite.T(), err)

update := coursePhaseDTO.UpdateCoursePhase{
ID: id,
Name: "Updated Phase",
IsInitialPhase: false,
MetaData: metaData,
}

err = UpdateCoursePhase(suite.ctx, update)
assert.NoError(suite.T(), err)

// Verify update
updatedCoursePhase, err := GetCoursePhaseByID(suite.ctx, id)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "Updated Phase", updatedCoursePhase.Name, "Expected updated course phase name to match")
assert.False(suite.T(), updatedCoursePhase.IsInitialPhase, "Expected updated course phase to be an initial phase")
assert.Equal(suite.T(), metaData, updatedCoursePhase.MetaData, "Expected metadata to match updated data")
}

func (suite *CoursePhaseTestSuite) TestCreateCoursePhase() {
jsonData := `{"new_key": "new_value"}`
var metaData meta.MetaData
err := json.Unmarshal([]byte(jsonData), &metaData)
assert.NoError(suite.T(), err)

newCoursePhase := coursePhaseDTO.CreateCoursePhase{
CourseID: uuid.MustParse("3f42d322-e5bf-4faa-b576-51f2cab14c2e"),
Name: "New Phase",
IsInitialPhase: false,
MetaData: metaData,
CoursePhaseTypeID: uuid.MustParse("7dc1c4e8-4255-4874-80a0-0c12b958744c"),
}

createdCoursePhase, err := CreateCoursePhase(suite.ctx, newCoursePhase)
assert.NoError(suite.T(), err)

// Verify creation
fetchedCoursePhase, err := GetCoursePhaseByID(suite.ctx, createdCoursePhase.ID)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "New Phase", fetchedCoursePhase.Name, "Expected course phase name to match")
assert.False(suite.T(), fetchedCoursePhase.IsInitialPhase, "Expected course phase to not be an initial phase")
assert.Equal(suite.T(), metaData, fetchedCoursePhase.MetaData, "Expected metadata to match")
}

func TestCoursePhaseTestSuite(t *testing.T) {
suite.Run(t, new(CoursePhaseTestSuite))
}
Loading

0 comments on commit f603c1e

Please sign in to comment.