- Created main application entry point in `main.go`. - Added configuration management in `config/config.go` and tests in `config/config_test.go`. - Implemented handlers for book and stationery uploads in `handlers/book.go` and `handlers/stationery.go`, including validation logic. - Established database connection and services in `services/database.go` and `services/book_service.go`. - Defined models for books and stationery in `models/book.go` and `models/stationery.go`. - Set up Firebase integration for image uploads in `services/firebase.go`. - Created migration scripts for database schema in `migrations/001_create_tables.sql` and subsequent updates. - Added CORS and error handling middleware. - Included comprehensive tests for handlers, services, and utilities. - Documented API endpoints and usage in `README.md` and migration instructions in `migrations/README.md`. - Introduced `.gitignore` to exclude unnecessary files and directories. - Added Go module support with `go.mod` and `go.sum` files. - Implemented utility functions for slug generation and validation in `utils/slug.go` and `utils/validation.go`.
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"bytes"
|
|
"mime/multipart"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func TestUploadBook(t *testing.T) {
|
|
// This test requires database and Firebase to be initialized
|
|
// Skip if not available
|
|
t.Skip("Skipping UploadBook test - requires database and Firebase")
|
|
|
|
app := fiber.New()
|
|
app.Post("/api/books", UploadBook)
|
|
|
|
// Create multipart form with test data
|
|
body := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(body)
|
|
|
|
// Add form fields
|
|
writer.WriteField("book_name", "Test Book")
|
|
writer.WriteField("cost", "10.50")
|
|
writer.WriteField("price", "15.99")
|
|
writer.WriteField("quantity", "100")
|
|
writer.WriteField("publisher_author", "Test Publisher")
|
|
writer.WriteField("category", "Fiction")
|
|
|
|
// Add image file
|
|
part, _ := writer.CreateFormFile("image", "test.png")
|
|
part.Write([]byte("fake image data"))
|
|
|
|
writer.Close()
|
|
|
|
req := httptest.NewRequest("POST", "/api/books", body)
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Test request failed: %v", err)
|
|
}
|
|
|
|
if resp.StatusCode != fiber.StatusCreated {
|
|
t.Errorf("Expected status %d, got %d", fiber.StatusCreated, resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestUploadBook_ValidationErrors(t *testing.T) {
|
|
app := fiber.New()
|
|
app.Post("/api/books", UploadBook)
|
|
|
|
// Test missing required field
|
|
body := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(body)
|
|
writer.WriteField("book_name", "") // Empty book name
|
|
writer.Close()
|
|
|
|
req := httptest.NewRequest("POST", "/api/books", body)
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Test request failed: %v", err)
|
|
}
|
|
|
|
if resp.StatusCode != fiber.StatusBadRequest {
|
|
t.Errorf("Expected status %d, got %d", fiber.StatusBadRequest, resp.StatusCode)
|
|
}
|
|
}
|