Add initial project structure with core functionality for book and stationery uploads
- 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`.
This commit is contained in:
72
handlers/stationery_test.go
Normal file
72
handlers/stationery_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"mime/multipart"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func TestUploadStationery(t *testing.T) {
|
||||
// This test requires database and Firebase to be initialized
|
||||
// Skip if not available
|
||||
t.Skip("Skipping UploadStationery test - requires database and Firebase")
|
||||
|
||||
app := fiber.New()
|
||||
app.Post("/api/stationery", UploadStationery)
|
||||
|
||||
// Create multipart form with test data
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
// Add form fields
|
||||
writer.WriteField("stationery_name", "Test Pen")
|
||||
writer.WriteField("cost", "2.50")
|
||||
writer.WriteField("price", "5.99")
|
||||
writer.WriteField("quantity", "200")
|
||||
writer.WriteField("category", "Writing")
|
||||
writer.WriteField("color", "Blue")
|
||||
|
||||
// Add image file
|
||||
part, _ := writer.CreateFormFile("image", "test.png")
|
||||
part.Write([]byte("fake image data"))
|
||||
|
||||
writer.Close()
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/stationery", 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 TestUploadStationery_ValidationErrors(t *testing.T) {
|
||||
app := fiber.New()
|
||||
app.Post("/api/stationery", UploadStationery)
|
||||
|
||||
// Test missing required field
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
writer.WriteField("stationery_name", "") // Empty stationery name
|
||||
writer.Close()
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/stationery", 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user