cslms-api/test/quiz_test.go

176 lines
4.6 KiB
Go
Raw Normal View History

2023-12-27 17:31:49 +09:00
package learsteam_quiz_test
import (
"learnsteam/cslms-api/internal/controllers"
2023-12-29 00:27:12 +09:00
"learnsteam/cslms-api/internal/models"
2023-12-27 17:31:49 +09:00
"learnsteam/cslms-api/internal/repositories"
"learnsteam/cslms-api/internal/services"
2023-12-29 00:27:12 +09:00
"os"
"testing"
2023-12-27 17:31:49 +09:00
"github.com/stretchr/testify/suite"
"gorm.io/gorm"
)
type QuizTestSuite struct {
suite.Suite
db *gorm.DB
repository repositories.QuizRepository
service services.QuizService
controller controllers.QuizController
}
// func (suite *QuizTestSuite) SetupSuite() {
// // err := os.Remove("test.db")
// // if err != nil {
// // suite.Fail("Failed to remove the test database file")
// // }
// gorm_config := gorm.Config{NamingStrategy: schema.NamingStrategy{SingularTable: true}}
// db, _ := gorm.Open(sqlite.Open("test.db"), &gorm_config)
// db.AutoMigrate(&models.Quiz{})
// repository := repositories.NewQuizRepository(db)
// service := services.NewQuizService(repository)
// controller := controllers.NewQuizController(service)
// suite.db = db
// suite.service = service
// suite.repository = repository
// suite.controller = controller
2023-12-29 00:27:12 +09:00
// // suite.CreateSampleData()
2023-12-27 17:31:49 +09:00
// }
// func (suite *QuizTestSuite) CreateSampleData() {
// var quizzes []models.Quiz
// quiz_paper_id := "1_id"
// c := []string{"1", "2", "3", "4"}
// var j datatypes.JSON
// j, _ = json.Marshal(c)
// for i := 1; i < 6; i++ {
// q := models.Quiz{
// ID: strconv.Itoa(i),
// QuizPaperID: quiz_paper_id,
// No: i,
// QuestionType: "multiple",
// Question: strconv.Itoa(i) + " question",
// Content: j,
// Answer: j,
// Hint: "힌트입니다.",
// Comment: "설명입니다.",
// UpdatedAt: time.Now(),
// CreatedAt: time.Now(),
// }
// quizzes = append(quizzes, q)
// }
// for _, quiz := range quizzes {
// suite.db.Create(&quiz)
// }
// }
2023-12-29 00:27:12 +09:00
func (suite *QuizTestSuite) TearDownSuite() {
// DB 삭제
suite.db.Migrator().DropTable(&models.Quiz{})
err := os.Remove("test.db")
if err != nil {
suite.Fail("Failed to remove the test database file")
}
}
2023-12-27 17:31:49 +09:00
2023-12-29 00:27:12 +09:00
func (suite *QuizTestSuite) SetupTest() {
// suite.CreateSampleData()
}
2023-12-27 17:31:49 +09:00
2023-12-29 00:27:12 +09:00
func (suite *QuizTestSuite) TearDownTest() {
2023-12-27 17:31:49 +09:00
2023-12-29 00:27:12 +09:00
}
2023-12-27 17:31:49 +09:00
2023-12-29 00:27:12 +09:00
func TestQuizSuite(t *testing.T) {
suite.Run(t, new(QuizTestSuite))
}
2023-12-27 17:31:49 +09:00
// // 목록 테스트
// func (suite *QuizTestSuite) TestListQuizSuccess() {
// quizzes, err := suite.repository.List("id_1", 1, 10)
// assert.NoError(suite.T(), err)
// assert.NotNil(suite.T(), quizzes)
// assert.Equal(suite.T(), 5, len(*quizzes))
// }
// // Find 테스트
// func (suite *QuizTestSuite) TestFindQuizSuccess() {
// quiz, err := suite.repository.Find("1")
// assert.NoError(suite.T(), err)
// assert.Equal(suite.T(), "1", quiz.ID)
// }
// // Find 테스트
// func (suite *QuizTestSuite) TestFindQuizFail() {
// _, err := suite.repository.Find("x")
// assert.Error(suite.T(), err)
// }
// // 수정 테스트
// func (suite *QuizTestSuite) TestUpdateQuizFail() {
// c := []string{"1", "2", "3", "4"}
// var j datatypes.JSON
// j, _ = json.Marshal(c)
// q := &models.Quiz{
// ID: uuid.NewString(),
// QuizPaperID: uuid.NewString(),
// No: 1,
// QuestionType: "multiple",
// Question: "question",
// Content: j,
// Answer: j,
// Hint: "힌트입니다.",
// Comment: "설명입니다.",
// UpdatedAt: time.Now(),
// CreatedAt: time.Now(),
// }
// quiz, err := suite.repository.Create(q)
// assert.NoError(suite.T(), err)
// quiz.QuestionType = "check"
// quiz.Content = j
// quiz.Answer = j
// quiz.Hint = "힌트입니다!"
// updatedQuiz, err := suite.repository.Update(quiz)
// assert.NoError(suite.T(), err)
// assert.Equal(suite.T(), updatedQuiz.QuestionType, quiz.QuestionType)
// assert.Equal(suite.T(), updatedQuiz.Content, quiz.Content)
// assert.Equal(suite.T(), updatedQuiz.Answer, quiz.Answer)
// }
// // 삭제 테스트
// func (suite *QuizTestSuite) TestDeleteQuizSuccess() {
// c := []string{"1", "2", "3", "4"}
// var j datatypes.JSON
// j, _ = json.Marshal(c)
// q := &models.Quiz{
// ID: uuid.NewString(),
// QuizPaperID: uuid.NewString(),
// No: 1,
// QuestionType: "multiple",
// Question: "question",
// Content: j,
// Answer: j,
// Hint: "힌트입니다.",
// Comment: "설명입니다.",
// UpdatedAt: time.Now(),
// CreatedAt: time.Now(),
// }
// _, err := suite.repository.Create(q)
// assert.NoError(suite.T(), err)
// err = suite.repository.Delete(q.ID)
// assert.NoError(suite.T(), err)
// _, err = suite.repository.Find(q.ID)
// assert.Error(suite.T(), err)
// }