@@ -199,3 +199,41 @@ func TestMoveItem(t *testing.T) {
199199 t .Fatalf ("expected new path to be 'stores/2/items/1', got %q" , newPath )
200200 }
201201}
202+
203+ func TestListBooksByPublisher (t * testing.T ) {
204+ db := setupTestDB (t )
205+ defer db .Close ()
206+
207+ s := NewBookstoreServer (db )
208+
209+ // Serialize authors and ISBNs for test data
210+ authorOneSerialized := `[{"name":"Author One"}]`
211+ authorTwoSerialized := `[{"name":"Author Two"}]`
212+ isbnOneSerialized := `["1111111111"]`
213+ isbnTwoSerialized := `["2222222222"]`
214+
215+ // Insert test books
216+ _ , err := db .Exec (`
217+ INSERT INTO books (path, author, price, published, edition, isbn)
218+ VALUES (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)` ,
219+ "publishers/1/books/1" , authorOneSerialized , 10 , true , 1 , isbnOneSerialized ,
220+ "publishers/2/books/2" , authorTwoSerialized , 15 , true , 1 , isbnTwoSerialized ,
221+ )
222+ if err != nil {
223+ t .Fatalf ("failed to insert test books: %v" , err )
224+ }
225+
226+ // Test filtering by publisher 1
227+ resp , err := s .ListBooks (context .Background (), & bpb.ListBooksRequest {Parent : "publishers/1" })
228+ if err != nil {
229+ t .Fatalf ("ListBooks failed: %v" , err )
230+ }
231+
232+ if len (resp .Results ) != 1 {
233+ t .Fatalf ("expected 1 book, got %d" , len (resp .Results ))
234+ }
235+
236+ if resp .Results [0 ].Path != "publishers/1/books/1" {
237+ t .Errorf ("unexpected book path: %s" , resp .Results [0 ].Path )
238+ }
239+ }
0 commit comments