From 8d8809d0c06c5b0be2ee602e24c471d30086a605 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Tue, 13 May 2025 20:32:48 -0700 Subject: [PATCH] fix(server): return only books with correct publisher the example server was returning books that had different publishers. --- example/service/service.go | 10 ++++++--- example/service/service_test.go | 38 +++++++++++++++++++++++++++++++++ scripts/serve.sh | 2 ++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 scripts/serve.sh diff --git a/example/service/service.go b/example/service/service.go index 7583678..71bad92 100644 --- a/example/service/service.go +++ b/example/service/service.go @@ -204,9 +204,14 @@ func (s BookstoreServer) GetBook(_ context.Context, r *bpb.GetBookRequest) (*bpb } func (s BookstoreServer) ListBooks(_ context.Context, r *bpb.ListBooksRequest) (*bpb.ListBooksResponse, error) { + if r.Parent == "" { + return nil, status.Errorf(codes.InvalidArgument, "parent must be specified") + } + rows, err := s.db.Query(` SELECT path, author, price, published, edition, isbn - FROM books`) + FROM books + WHERE path LIKE ?`, r.Parent+"/%") if err != nil { return nil, status.Errorf(codes.Internal, "failed to list books: %v", err) } @@ -216,7 +221,6 @@ func (s BookstoreServer) ListBooks(_ context.Context, r *bpb.ListBooksRequest) ( for rows.Next() { book := &bpb.Book{} - // Deserialize the 'author' field from JSON when listing books var authorsSerialized string var isbnSerialized string if err := rows.Scan(&book.Path, &authorsSerialized, &book.Price, &book.Published, &book.Edition, &isbnSerialized); err != nil { @@ -655,7 +659,7 @@ func StartServer(targetPort int) { CREATE TABLE IF NOT EXISTS books ( path TEXT PRIMARY KEY, author TEXT, - price REAL, + price INTEGER, published BOOLEAN, edition INTEGER, isbn TEXT diff --git a/example/service/service_test.go b/example/service/service_test.go index 4a4c748..22f69d3 100644 --- a/example/service/service_test.go +++ b/example/service/service_test.go @@ -199,3 +199,41 @@ func TestMoveItem(t *testing.T) { t.Fatalf("expected new path to be 'stores/2/items/1', got %q", newPath) } } + +func TestListBooksByPublisher(t *testing.T) { + db := setupTestDB(t) + defer db.Close() + + s := NewBookstoreServer(db) + + // Serialize authors and ISBNs for test data + authorOneSerialized := `[{"name":"Author One"}]` + authorTwoSerialized := `[{"name":"Author Two"}]` + isbnOneSerialized := `["1111111111"]` + isbnTwoSerialized := `["2222222222"]` + + // Insert test books + _, err := db.Exec(` + INSERT INTO books (path, author, price, published, edition, isbn) + VALUES (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)`, + "publishers/1/books/1", authorOneSerialized, 10, true, 1, isbnOneSerialized, + "publishers/2/books/2", authorTwoSerialized, 15, true, 1, isbnTwoSerialized, + ) + if err != nil { + t.Fatalf("failed to insert test books: %v", err) + } + + // Test filtering by publisher 1 + resp, err := s.ListBooks(context.Background(), &bpb.ListBooksRequest{Parent: "publishers/1"}) + if err != nil { + t.Fatalf("ListBooks failed: %v", err) + } + + if len(resp.Results) != 1 { + t.Fatalf("expected 1 book, got %d", len(resp.Results)) + } + + if resp.Results[0].Path != "publishers/1/books/1" { + t.Errorf("unexpected book path: %s", resp.Results[0].Path) + } +} diff --git a/scripts/serve.sh b/scripts/serve.sh new file mode 100644 index 0000000..57b4fd4 --- /dev/null +++ b/scripts/serve.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +go run ./... \ No newline at end of file