go17/book/controller/book_test.go

54 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-11-23 12:11:44 +08:00
package controller_test
import (
"context"
"testing"
"gitlab.com/go-course-project/go17/book/controller"
2024-11-23 16:08:17 +08:00
"gitlab.com/go-course-project/go17/book/exception"
2024-11-23 12:11:44 +08:00
"gitlab.com/go-course-project/go17/book/model"
)
func TestGetBook(t *testing.T) {
book := controller.NewBookController()
ins, err := book.GetBook(context.Background(), &controller.GetBookRequest{
2024-11-23 16:08:17 +08:00
Isbn: 100,
2024-11-23 12:11:44 +08:00
})
2024-11-23 16:08:17 +08:00
if err != nil && exception.IsNotFound(err) {
t.Log("is not found error")
}
// if errors.Is(err, gorm.ErrRecordNotFound) {
// t.Log("is not found error")
// }
// if err.Error() == "record not found" {
// t.Log(" string equal is not found error")
// }
2024-11-23 12:11:44 +08:00
if err != nil {
t.Fatal(err)
}
t.Log(ins)
}
// INSERT INTO `books` (`title`,`author`,`price`,`is_sale`) VALUES ('Go语言','will',10,NULL)
//
// {
// "isbn": 5,
// "title": "Go语言",
// "author": "will",
// "price": 10,
// "is_sale": null
// }
func TestCreateBook(t *testing.T) {
book := controller.NewBookController()
ins, err := book.CreateBook(context.Background(), &model.BookSpec{
Author: "will",
Price: 10,
Title: "Go语言",
})
if err != nil {
t.Fatal(err)
}
t.Log(ins)
}