- 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`.
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"jd-book-uploader/models"
|
|
)
|
|
|
|
func TestCreateStationery(t *testing.T) {
|
|
// This test requires a running database
|
|
// Skip if not available
|
|
t.Skip("Skipping CreateStationery test - requires running database")
|
|
|
|
ctx := context.Background()
|
|
|
|
// This test would require:
|
|
// 1. Database connection initialized
|
|
// 2. Valid test data
|
|
// 3. Cleanup after test
|
|
|
|
req := &models.StationeryCreateRequest{
|
|
StationeryName: "Test Pen",
|
|
Cost: 2.50,
|
|
Price: 5.99,
|
|
Discount: 0,
|
|
Quantity: 200,
|
|
Color: "Blue",
|
|
Material: "Plastic",
|
|
Dimensions: "15cm",
|
|
Category: "Writing",
|
|
Description: "A test pen",
|
|
ImageURL: "https://example.com/image.png",
|
|
}
|
|
|
|
stationery, err := CreateStationery(ctx, req)
|
|
if err != nil {
|
|
t.Fatalf("CreateStationery() error = %v", err)
|
|
}
|
|
|
|
if stationery == nil {
|
|
t.Fatal("CreateStationery() returned nil stationery")
|
|
}
|
|
|
|
if stationery.StationeryCode == "" {
|
|
t.Error("CreateStationery() did not generate stationery_code")
|
|
}
|
|
|
|
if stationery.Slug == "" {
|
|
t.Error("CreateStationery() did not generate slug")
|
|
}
|
|
|
|
if stationery.Slug != "test-pen" {
|
|
t.Errorf("CreateStationery() slug = %q, want %q", stationery.Slug, "test-pen")
|
|
}
|
|
}
|
|
|
|
func TestGetStationeryByCode(t *testing.T) {
|
|
// This test requires a running database
|
|
// Skip if not available
|
|
t.Skip("Skipping GetStationeryByCode test - requires running database")
|
|
|
|
ctx := context.Background()
|
|
|
|
// This test would require:
|
|
// 1. Database connection initialized
|
|
// 2. A stationery item already in the database
|
|
// 3. Valid stationery_code
|
|
|
|
stationeryCode := "test-stationery-code"
|
|
stationery, err := GetStationeryByCode(ctx, stationeryCode)
|
|
if err != nil {
|
|
t.Logf("GetStationeryByCode() error = %v (expected if stationery not found)", err)
|
|
return
|
|
}
|
|
|
|
if stationery == nil {
|
|
t.Error("GetStationeryByCode() returned nil stationery")
|
|
}
|
|
|
|
if stationery.StationeryCode != stationeryCode {
|
|
t.Errorf("GetStationeryByCode() stationery_code = %q, want %q", stationery.StationeryCode, stationeryCode)
|
|
}
|
|
}
|