core/tests/db_test.go

666 lines
21 KiB
Go
Raw Normal View History

2024-03-13 17:23:27 +00:00
package tests
//import (
// "context"
// "github.com/pioz/faker"
// "math"
// "math/rand"
2025-02-27 13:10:51 +00:00
// "gitea.pena/SQuiz/common/dal"
// "gitea.pena/SQuiz/common/model"
// "gitea.pena/SQuiz/common/repository/quiz"
// "gitea.pena/SQuiz/common/repository/result"
// "gitea.pena/SQuiz/core/clients/auth"
//
// //"database/sql"
// "fmt"
// "testing"
// "time"
//
// "github.com/stretchr/testify/assert"
// bdd "github.com/themakers/bdd"
//)
//
//const baseURL = "http://localhost:"
//const authClientUrl = "test.com"
//const postgresCred string = "host=localhost port=35432 user=squiz password=Redalert2 dbname=squiz sslmode=disable"
//const appPort = "1488"
//const storerPort = "1489"
//const answererPort = "1490"
//const healthChecksPort = "5489"
//
//var types4testing = []string{
// model.TypeVariant,
// model.TypeImages,
// model.TypeVarImages,
// model.TypeFile,
// model.TypeText,
// model.TypeEmoji,
// model.TypeSelect,
// model.TypeDate,
// model.TypeNumber,
// model.TypePage,
// model.TypeRating,
//}
//
//func TestDAL(t *testing.T) {
// //login := "D" + strconv.FormatInt(time.Now().Unix(), 10)
// //token := registerUser(login)
// //
// //m, ok := token.Claims.(jwt.MapClaims)
// //if !ok {
// // panic("malformed JWT")
// //}
// //
// //id, ok := m["id"].(string)
// //if !ok || id == "" {
// // panic("missing id claim in JWT")
// //}
//
// id := "123"
//
// ctx := context.Background()
// authClient := auth.NewAuthClient(authClientUrl)
// fmt.Println(id)
//
// bdd.Scenario(t, "DAL testing", func(t *testing.T, runID string) {
// var (
// err error
// d *dal.DAL
// records []model.Quiz
// questions []model.Question
// answersSession string
// )
// bdd.Act(t, "Postgres Tables Creating", func() {
//
// bdd.Test(t, "test dbconnect", func() {
// d, err = dal.New(ctx, postgresCred, authClient)
// assert.NoError(t, err, nil)
// assert.NotNil(t, d, nil)
// })
//
// //bdd.Test(t, "positive test of table creating", func() {
// // err = d.Init()
// // assert.NoError(t, err, nil)
// //})
// //
// //bdd.Test(t, "negative test of table creating", func() {
// // err = d.Init()
// // assert.NoError(t, err, nil)
// //})
// })
//
// bdd.Act(t, "create quiz testing", func() {
// record := model.Quiz{
// AccountId: id,
// Fingerprinting: true,
// Repeatable: true,
// NotePrevented: true,
// MailNotifications: true,
// UniqueAnswers: true,
// Name: "test",
// Description: "test",
// Config: `{"Mailing": {"When": "test","Theme": "Quiz test","Reply": "test","ReplName": "test"}}`,
// Status: model.StatusDraft,
// Limit: 100,
// DueTo: uint64(time.Now().Add(time.Hour).Unix()),
// TimeOfPassing: 60,
// Pausable: true,
// GroupId: 2,
// }
// statuses4creating := []string{
// model.StatusDraft,
// model.StatusStart,
// model.StatusStop,
// model.StatusOffLimit,
// model.StatusTimeout,
// model.StatusTemplate,
// }
//
// for i := 0; i < 100; i++ {
//
// record2 := model.Quiz{
// AccountId: id,
// Fingerprinting: true,
// Repeatable: true,
// NotePrevented: true,
// MailNotifications: true,
// UniqueAnswers: true,
// Name: record.Name + fmt.Sprint(i),
// Description: "test",
// Config: `{"Mailing": {"When": "test","Theme": "Quiz test","Reply": "test","ReplName": "test"}}`,
// Status: statuses4creating[rand.Intn(len(statuses4creating))],
// Limit: 100,
// DueTo: uint64(time.Now().Add(time.Hour).Unix()),
// ParentIds: []int32{},
// TimeOfPassing: 60,
// Pausable: true,
// GroupId: 1,
// }
//
// bdd.Test(t, "positive quiz storing", func() {
// err := d.QuizRepo.CreateQuiz(ctx, &record2)
// assert.NoError(t, err, nil)
// assert.NotZero(t, record2.Id, nil)
// assert.NotEqual(t, record2.CreatedAt, nil)
// assert.NotEqual(t, record2.UpdatedAt, nil)
// records = append(records, record2)
// })
// }
//
// for i := 0; i < len(records)-1; i++ {
// if records[i].Status == model.StatusStart {
//
// bdd.Test(t, "get quiz by qid", func() {
// qu, err := d.QuizRepo.GetQuizByQid(ctx, records[i].Qid)
// assert.Equal(t, records[i].Id, qu.Id)
// assert.NoError(t, err, nil)
// })
// }
//
// bdd.Test(t, "get quiz by id", func() {
// qu, err := d.QuizRepo.GetQuizById(ctx, id, records[i].Id)
// assert.Equal(t, records[i].Id, qu.Id)
// assert.Equal(t, records[i].Qid, qu.Qid)
// assert.NoError(t, err, nil)
// })
//
// bdd.Test(t, "get quiz config", func() {
// _, _, err := d.QuizRepo.GetQuizConfig(ctx, records[i].Id)
// assert.NoError(t, err, nil)
// })
// }
//
// for i := 0; i < len(records)%80; i++ {
// bdd.Test(t, "update quiz", func() {
// updquiz := model.Quiz{
// Id: records[i].Id,
// Qid: records[i].Qid,
// AccountId: id,
// Deleted: records[i].Deleted,
// Archived: records[i].Archived,
// Fingerprinting: records[i].Fingerprinting,
// Repeatable: records[i].Repeatable,
// NotePrevented: records[i].NotePrevented,
// MailNotifications: records[i].MailNotifications,
// UniqueAnswers: records[i].UniqueAnswers,
// Name: faker.String() + records[i].Name,
// Description: faker.String() + records[i].Description,
// Config: faker.String() + records[i].Config,
// Status: records[i].Status,
// Limit: records[i].Limit,
// DueTo: records[i].DueTo,
// TimeOfPassing: records[i].TimeOfPassing,
// Pausable: records[i].Pausable,
// Version: records[i].Version,
// VersionComment: records[i].VersionComment,
// ParentIds: records[i].ParentIds,
// CreatedAt: records[i].CreatedAt,
// UpdatedAt: records[i].UpdatedAt,
// QuestionsCount: records[i].QuestionsCount,
// SessionCount: records[i].SessionCount,
// PassedCount: records[i].PassedCount,
// AverageTime: records[i].AverageTime,
// Super: records[i].Super,
// GroupId: records[i].GroupId,
// }
// err := d.QuizRepo.UpdateQuiz(ctx, id, updquiz)
// assert.NoError(t, err, nil)
// })
// bdd.Test(t, "move to history quiz", func() {
// qu, err := d.QuizRepo.MoveToHistoryQuiz(ctx, records[i].Id, id)
// assert.Equal(t, records[i].Id+100, qu.Id)
// assert.NoError(t, err, nil)
// })
// }
//
// bdd.Test(t, "negative test quiz storing1", func() {
// record.Name = faker.StringWithSize(300)
// err := d.QuizRepo.CreateQuiz(ctx, &record)
// assert.Error(t, err, nil)
// })
//
// bdd.Test(t, "negative test quiz storing2", func() {
// record.Status = model.StatusTemplate
// err := d.QuizRepo.CreateQuiz(ctx, &record)
// assert.Error(t, err, nil)
// })
// })
//
// bdd.Act(t, "get list of quizes", func() {
// bdd.Test(t, "get list of quizes", func() {
// res, _, err := d.QuizRepo.GetQuizList(ctx, quiz.GetQuizListDeps{
// Limit: 10,
// Offset: 0,
// From: 0,
// To: 0,
// Group: 0,
// Deleted: false,
// Archived: false,
// Super: true,
// Search: "",
// Status: "",
// AccountId: id,
// })
// assert.NoError(t, err, nil)
// assert.Equal(t, 0, len(res), nil)
// //assert.Greater(t, cnt, uint64(0), nil)
// })
// })
//
// bdd.Act(t, "create question testing", func() {
// quix4filling := records[rand.Intn(len(records))]
// for _, questionType := range types4testing {
// bdd.Test(t, fmt.Sprintf("positive question creating: %s", questionType), func() {
// question := model.Question{
// Id: 0,
// QuizId: quix4filling.Id,
// Title: "testNone",
// Description: "sample text",
// Type: questionType,
// Required: true,
// Deleted: false,
// Page: 0,
// Content: "test",
// Version: 0,
// ParentIds: nil,
// CreatedAt: time.Now(),
// UpdatedAt: time.Now(),
// }
// err := d.QuestionRepo.CreateQuestion(ctx, &question)
// assert.NoError(t, err, nil)
// assert.NotZero(t, question.Id, nil)
// questions = append(questions, question)
//
// bdd.Test(t, "get question list", func() {
// ques, count, err := d.QuestionRepo.GetQuestionList(ctx, 0, 10, 1226358000, 1289430000, quix4filling.Id, false, true, "", questionType)
// fmt.Println(ques)
// fmt.Println(count)
// assert.NoError(t, err, nil)
// })
// })
// }
//
// bdd.Test(t, "test create answers", func() {
// testanswers := make([]model.Answer, 0)
// for i := 0; i < 15; i++ {
// recAns := model.Answer{
// Content: "test",
// QuestionId: questions[0].Id,
// Result: false,
// Email: faker.String(),
// QuizId: faker.Uint64(),
// }
// testanswers = append(testanswers, recAns)
// }
// answersSession = faker.String()
//
// anss, err := d.AnswerRepo.CreateAnswers(ctx, testanswers, answersSession, "seeeeeeeesssion", questions[0].QuizId)
// if len(err) != 0 {
// assert.NoError(t, err[0], nil)
// }
//
// if len(anss) != len(testanswers) {
// panic("test create answers crushed")
// }
// })
//
// bdd.Test(t, "Get All Answers By Quiz ID(session)", func() {
// ansss, err := d.AnswerRepo.GetAllAnswersByQuizID(ctx, answersSession)
// assert.NoError(t, err, nil)
// assert.Equal(t, 1, len(ansss))
// })
//
// bdd.Test(t, "update question", func() {
// for i := 0; i < len(questions)%80; i++ {
//
// bdd.Test(t, "get question title", func() {
// title, tipe, err := d.QuestionRepo.GetQuestionTitleByID(ctx, questions[i].Id)
// assert.NoError(t, err, nil)
// assert.Equal(t, questions[i].Title, title)
// assert.Equal(t, questions[i].Type, tipe)
// })
//
// updateques := model.Question{
// Id: questions[i].Id,
// QuizId: questions[i].QuizId,
// Title: faker.String() + questions[i].Title,
// Description: faker.String() + questions[i].Description,
// Type: questions[i].Type,
// Required: questions[i].Required,
// Deleted: questions[i].Deleted,
// Page: questions[i].Page,
// Content: faker.String() + questions[i].Content,
// Version: questions[i].Version,
// ParentIds: questions[i].ParentIds,
// CreatedAt: questions[i].CreatedAt,
// UpdatedAt: questions[i].UpdatedAt,
// }
//
// err := d.QuestionRepo.UpdateQuestion(ctx, updateques)
// assert.NoError(t, err, nil)
//
// bdd.Test(t, "move to history question", func() {
// que, err := d.QuestionRepo.MoveToHistoryQuestion(ctx, questions[i].Id)
// assert.NoError(t, err, nil)
// assert.Equal(t, questions[i].Id+11, que.Id)
// })
// }
// })
//
// bdd.Test(t, "negative question creating: overflow title", func() {
// question := model.Question{
// Id: 0,
// QuizId: quix4filling.Id,
// Title: faker.StringWithSize(1024),
// Description: "sample text",
// Type: model.TypeText,
// Required: true,
// Deleted: false,
// Page: 0,
// Content: "test",
// Version: 0,
// ParentIds: nil,
// CreatedAt: time.Time{},
// UpdatedAt: time.Time{},
// }
// err := d.QuestionRepo.CreateQuestion(ctx, &question)
// assert.Error(t, err, nil)
// })
// bdd.Test(t, "negative question creating: invalid status", func() {
// question := model.Question{
// Id: 0,
// QuizId: quix4filling.Id,
// Title: `testNonete`,
// Description: "sample text",
// Type: "test",
// Required: true,
// Deleted: true,
// Page: 0,
// Content: "test",
// Version: 0,
// ParentIds: nil,
// CreatedAt: time.Time{},
// UpdatedAt: time.Time{},
// }
// err := d.QuestionRepo.CreateQuestion(ctx, &question)
// assert.Error(t, err, nil)
// })
// })
//
// bdd.Act(t, "edit some questions", func() {
// bdd.Test(t, "positive delete question", func() {
// question, err := d.QuestionRepo.DeleteQuestion(ctx, questions[0].Id)
// assert.NoError(t, err)
// assert.True(t, question.Deleted)
// questions[0] = question
// })
// bdd.Test(t, "negative delete question", func() {
// question, err := d.QuestionRepo.DeleteQuestion(ctx, math.MaxUint64)
// assert.Error(t, err)
// assert.Zero(t, question.Id)
// })
// bdd.Test(t, "positive create new version of question", func() {
// questions[0].Deleted = true
// questions[0].Version += 1
// questions[0].ParentIds = append(questions[0].ParentIds, int32(questions[0].Id))
// questions[0].Title = faker.StringWithSize(100)
//
// err := d.QuestionRepo.CreateQuestion(ctx, &questions[0])
// assert.NoError(t, err)
// })
// })
//
// bdd.Act(t, "edit some quizes", func() {
// bdd.Test(t, "positive delete quiz", func() {
// quiz, err := d.QuizRepo.DeleteQuiz(ctx, id, records[0].Id)
// fmt.Println(quiz)
// assert.NoError(t, err)
// assert.True(t, quiz.Deleted)
// records[0].Id = quiz.Id
// })
// bdd.Test(t, "negative delete question", func() {
// quiz, err := d.QuizRepo.DeleteQuiz(ctx, id, math.MaxUint64)
// assert.Error(t, err)
// assert.Zero(t, quiz.Id)
// })
// bdd.Test(t, "positive create new version of question", func() {
// records[0].Deleted = true
// records[0].Version += 1
// records[0].ParentIds = append(records[0].ParentIds, int32(records[0].Id))
// records[0].Name = faker.StringWithSize(100)
//
// err := d.QuizRepo.CreateQuiz(ctx, &records[0])
// assert.NoError(t, err)
// })
// })
//
// bdd.Act(t, "update expired quizes", func() {
// bdd.Test(t, "check that we have quizes for expiration", func() {
// record2 := model.Quiz{
// AccountId: id,
// Fingerprinting: true,
// Repeatable: true,
// NotePrevented: true,
// MailNotifications: true,
// UniqueAnswers: true,
// Name: faker.StringWithSize(100),
// Description: "test",
// Config: "test",
// Status: model.StatusStart,
// Limit: 0,
// DueTo: uint64(time.Now().Add(-time.Hour).Unix()),
// TimeOfPassing: 0,
// Pausable: false,
// }
// err := d.QuizRepo.CreateQuiz(ctx, &record2)
// assert.NoError(t, err, nil)
// assert.NotZero(t, record2.Id, nil)
// assert.NotEqual(t, record2.CreatedAt, nil)
// assert.NotEqual(t, record2.UpdatedAt, nil)
// })
// })
//
// bdd.Act(t, "copy question flow", func() {
// bdd.Test(t, "duplicate question", func() {
// copiedQuestion, err := d.QuestionRepo.CopyQuestion(ctx, questions[4].Id, 0)
// assert.NoError(t, err)
// assert.Equal(t, questions[4].QuizId, copiedQuestion.QuizId)
// assert.NotEqual(t, questions[4].Id, copiedQuestion.Id)
// })
// bdd.Test(t, "copy question to another quiz", func() {
// copiedQuestion, err := d.QuestionRepo.CopyQuestion(ctx, questions[4].Id, records[10].Id)
// assert.NoError(t, err)
// assert.Equal(t, records[10].Id, copiedQuestion.QuizId)
// assert.NotEqual(t, questions[4].Id, copiedQuestion.Id)
// })
// })
//
// bdd.Act(t, "copy quiz flow", func() {
// bdd.Test(t, "quiz duplication", func() {
// copiesQuiz, err := d.QuizRepo.CopyQuiz(ctx, records[10].AccountId, records[10].Id)
// assert.NoError(t, err)
// assert.NotEqual(t, records[10].Id, copiesQuiz.Id)
// })
// })
//
// bdd.Act(t, "get history for quiz", func() {
// bdd.Test(t, "positive quiz history", func() {
// hist, err := d.QuizRepo.QuizHistory(ctx, quiz.QuizHistoryDeps{
// Id: records[0].Id,
// Limit: 100,
// Offset: 0,
// AccountId: id,
// })
// assert.NoError(t, err)
// assert.Equal(t, len(hist), 2)
// })
// bdd.Test(t, "negative quiz history", func() {
// _, err := d.QuizRepo.QuizHistory(ctx, quiz.QuizHistoryDeps{
// Id: math.MaxUint64,
// Limit: 100,
// Offset: 0,
// AccountId: id,
// })
// assert.NoError(t, err)
// assert.Equal(t, nil, nil)
// })
// })
//
// bdd.Act(t, "get history for question", func() {
// bdd.Test(t, "positive question history", func() {
// hist, err := d.QuestionRepo.QuestionHistory(ctx, questions[0].Id, 100, 0)
// assert.NoError(t, err)
// assert.Equal(t, len(hist), 2)
// })
// bdd.Test(t, "negative question history", func() {
// _, err := d.QuestionRepo.QuestionHistory(ctx, math.MaxUint64, 100, 0)
// assert.NoError(t, err)
// })
// })
//
// bdd.Act(t, "Worker Stat Process", func() {
// err := d.WorkerRepo.WorkerStatProcess(ctx)
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Worker Timeout Process", func() {
// err := d.WorkerRepo.WorkerTimeoutProcess(ctx)
// assert.NoError(t, err, nil)
// })
// // todo нужно будет обновить эти тесты связать с выше стоящими
// bdd.Act(t, "Get Account By ID", func() {
// account, err := d.AccountRepo.GetAccountByID(ctx, "64eb6ce57047f28fdabf69ec")
// assert.NoError(t, err, nil)
// fmt.Println(account)
// })
//
// bdd.Act(t, "Get Privileges By Account ID", func() {
// privileges, err := d.AccountRepo.GetPrivilegesByAccountID(ctx, "64eb6ce57047f28fdabf69ec")
// assert.NoError(t, err, nil)
// fmt.Println(privileges)
// })
//
// bdd.Act(t, "Get Accounts", func() {
// accounts, count, err := d.AccountRepo.GetAccounts(ctx, 10, 0)
// assert.NoError(t, err, nil)
// fmt.Println(accounts)
// fmt.Println(len(accounts))
// fmt.Println(count)
// })
//
// bdd.Act(t, "Update Privilege", func() {
// priv := model.ShortPrivilege{
// PrivilegeID: "quizCnt",
// PrivilegeName: "Количество Заявок",
// Amount: 0,
// CreatedAt: time.Now(),
// }
// err := d.AccountRepo.UpdatePrivilege(ctx, &priv, "047c1109-1fba-47d8-b4ec-6ded153a2ea9")
// assert.NoError(t, err, nil)
//
// })
//
// bdd.Act(t, "Insert Privilege", func() {
// priv := model.ShortPrivilege{
// PrivilegeID: "quizCnt",
// PrivilegeName: "123",
// Amount: 123,
// CreatedAt: time.Now(),
// }
//
// err := d.AccountRepo.InsertPrivilege(ctx, &priv, "047c1109-1fba-47d8-b4ec-6ded153a2ea9")
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Get Expired", func() {
//
// priv, err := d.AccountRepo.GetExpired(ctx, "quizCnt")
// fmt.Println(priv)
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Delete Privilege By ID", func() {
// err := d.AccountRepo.DeletePrivilegeByID(ctx, "10")
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Update Privilege Amount", func() {
// err := d.AccountRepo.UpdatePrivilegeAmount(ctx, "2", 100000)
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Get Acc And Privilege By Email(userid)", func() {
// acc, priv, err := d.AccountRepo.GetAccAndPrivilegeByEmail(ctx, "650061935ad320604ff24cc2")
// fmt.Println(acc, priv)
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Get Quiz Results", func() {
// now := time.Now()
//
// yearAgo := now.AddDate(-1, 0, 0)
//
// yearAhead := now.AddDate(1, 0, 0)
// ansexp, count, err := d.ResultRepo.GetQuizResults(ctx, 9422, result.GetQuizResDeps{
// To: yearAhead,
// From: yearAgo,
// New: false,
// Page: 1,
// Limit: 10,
// }, true)
// assert.NoError(t, err, nil)
// fmt.Println(ansexp)
// fmt.Println(count)
// })
//
// bdd.Act(t, "Update Answers Status", func() {
// err := d.ResultRepo.UpdateAnswersStatus(ctx, "64eb6ce57047f28fdabf69ec", []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Get Quiz Results CSV", func() {
// now := time.Now()
//
// yearAgo := now.AddDate(-1, 0, 0)
//
// yearAhead := now.AddDate(1, 0, 0)
// ans, err := d.ResultRepo.GetQuizResultsCSV(ctx, 14734, result.GetQuizResDeps{
// To: yearAhead,
// From: yearAgo,
// New: false,
// })
// fmt.Println(ans)
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Get Questions", func() {
// ques, err := d.ResultRepo.GetQuestions(ctx, 14734)
// fmt.Println(ques)
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Check Results Owner", func() {
// anss, err := d.ResultRepo.CheckResultsOwner(ctx, []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "64eb6ce57047f28fdabf69ec")
// fmt.Println(anss)
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Get Result Answers", func() {
// anss, err := d.ResultRepo.GetResultAnswers(ctx, 1)
// fmt.Println(anss)
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Check Result Owner", func() {
// anss, err := d.ResultRepo.CheckResultOwner(ctx, 1, "64eb6ce57047f28fdabf69ec")
// assert.Equal(t, anss, true)
// assert.NoError(t, err, nil)
// })
//
// bdd.Act(t, "Soft Delete Result By ID", func() {
// err := d.ResultRepo.SoftDeleteResultByID(ctx, 10)
// assert.NoError(t, err, nil)
// })
// })
//}