Skip to content

Instantly share code, notes, and snippets.

View BK1031's full-sized avatar

Bharat Kathi BK1031

View GitHub Profile
@BK1031
BK1031 / book_test.go
Created July 15, 2024 20:56
singlestore-go-bookstore TestCreateBook
package service
import (
"bookstore/model"
"testing"
)
func TestCreateBook(t *testing.T) {
// Arrange
book := model.Book{
@BK1031
BK1031 / book.go
Created July 15, 2024 22:31
singlestore-go-bookstore updated CreateBook function
package service
import (
"bookstore/database"
"bookstore/model"
)
func CreateBook(book model.Book) (model.Book, error) {
result := database.DB.Create(&book)
if result.Error != nil {
@BK1031
BK1031 / book.go
Created July 15, 2024 23:06
singlestore-go-bookstore book skeleton functions
package service
import (
"bookstore/database"
"bookstore/model"
)
func CreateBook(book model.Book) (model.Book, error) {
result := database.DB.Create(&book)
if result.Error != nil {
@BK1031
BK1031 / book_test.go
Last active July 15, 2024 23:13
singlestore-go-bookstore all book tests
package service
import (
"bookstore/database"
"bookstore/model"
"testing"
)
// ===== add this function =====
func ResetBookTable() {
@BK1031
BK1031 / book.go
Created July 15, 2024 23:28
singlestore-go-bookstore rest of book service
package service
import (
"bookstore/database"
"bookstore/model"
)
func CreateBook(book model.Book) (model.Book, error) {
result := database.DB.Create(&book)
if result.Error != nil {
@BK1031
BK1031 / book_test.go
Created July 16, 2024 00:14
singlestore-go-bookstore update TestCreateBook
func TestCreateBook(t *testing.T) {
ResetBookTable()
createdID := 0
t.Run("Success", func(t *testing.T) {
// Arrange
book := model.Book{
Title: "The Great Gatsby",
Author: "F. Scott Fitzgerald",
Price: 29.99,
Genre: "Fiction",
@BK1031
BK1031 / book_test.go
Created July 16, 2024 00:36
singlestore-go-bookstore full book coverage
func TestUpdateBook(t *testing.T) {
ResetBookTable()
t.Run("Success", func(t *testing.T) {
// Arrange
book := model.Book{
Title: "The Great Gatsby",
Author: "F. Scott Fitzgerald",
Price: 29.99,
Genre: "Fiction",
}
@BK1031
BK1031 / order.go
Last active July 16, 2024 01:18
singlestore-go-bookstore order skeleton service
package service
import "bookstore/model"
func CreateOrder(order model.Order) (model.Order, error) {
return model.Order{}, nil
}
func UpdateOrder(order model.Order) (model.Order, error) {
return model.Order{}, nil
@BK1031
BK1031 / main_test.go
Created July 16, 2024 01:20
singlestore-go-bookstore add reset table function to main service test
func ResetBookTable() {
database.DB.Where("1 = 1").Delete(&model.Book{})
}
func ResetOrderTable() {
database.DB.Where("1 = 1").Delete(&model.Order{})
}
@BK1031
BK1031 / order.go
Created July 16, 2024 08:38
singlestore-go-bookstore model/order.go CalculateTotal helper function
func (o *Order) CalculateTotal() {
o.Total = 0
for _, item := range o.Items {
o.Total += item.Book.Price * float64(item.Quantity)
}
}