package bookstore_test import ( "bookstore" "testing" "sort" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" ) func TestBuy(t *testing.T) { t.Parallel() b := bookstore.Book{ Title: "Lol", Author: "KEK", Copies: 2, } want := 1 result,err := bookstore.Buy(b) if err != nil { t.Fatal(err) } got := result.Copies if want != got { t.Errorf("Want %d but got %d", want,got) } } func TestBuyOutOfStock(t *testing.T) { t.Parallel() b := bookstore.Book{ Title: "Lol", Author: "KEK", Copies: 0, } _,err := bookstore.Buy(b) if err == nil { t.Error("Book is out of stock") } } func TestGetAllBooks(t *testing.T) { t.Parallel() catalog := bookstore.Catalog{ 1: {ID: 1, Title: "For the Love of Go"}, 2: {ID: 2, Title: "The Power of Go: Tools"}, } want := []bookstore.Book{ {ID: 1, Title: "For the Love of Go"}, {ID: 2, Title: "The Power of Go: Tools"}, } got := catalog.GetAllBooks() sort.Slice(got, func(i, j int) bool { return got[i].ID < got[j].ID }) if !cmp.Equal(want, got,cmpopts.IgnoreUnexported(bookstore.Book{})) { t.Error(cmp.Diff(want, got,)) } } func TestGetBook(t *testing.T) { t.Parallel() catalog := bookstore.Catalog{ 1: {ID: 1, Title: "For the Love of Go"}, 2: {ID: 2, Title: "The Power of Go: Tools"}, } // want := bookstore.Book{ID: 2, Title: "The Power of Go: Tools"} _, err := catalog.GetBook(2) if err != nil { t.Fatal("want error for non-existent ID, got nil") } } func TestNetPriceCents(t *testing.T) { t.Parallel() b := bookstore.Book { Title: "For the love of Go", PriceCents: 4000, DiscountPercents: 25, } want := 3000 got := b.NetPriceCents() if want != got { t.Errorf("want %d, got %d",want,got) } } func TestSetPriceCents(t *testing.T) { t.Parallel() b := bookstore.Book{ Title: "For the Love of Go", PriceCents: 4000, } want := 3000 err := b.SetPriceCents(want) if err != nil { t.Fatal(err) } got := b.PriceCents if want != got { t.Errorf("want updated price %d, got %d", want, got) } } func TestSetCategory(t *testing.T) { t.Parallel() b := bookstore.Book{ Title: "For the Love of Go", } cats := []bookstore.Category { bookstore.CategoryAutobiography, bookstore.CategoryLargePrintRomance, bookstore.CategoryParticlePhysics, } for _, cat := range cats { err := b.SetCategory(cat) if err != nil { t.Fatal(err) } got := b.Category() if cat != got { t.Errorf("want category %q, got %q", cat, got) } } } func TestSetCategoryInvalid(t *testing.T) { t.Parallel() b := bookstore.Book{ Title: "For the Love of Go", } err := b.SetCategory(999) if err == nil { t.Fatal("want error for invalid category, got nil") } }