Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 54 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,50 +1,76 @@
.PHONY:
help
lint
test-all
test-verbose
test-coverage
test-coverage-html
test-clean
install-pre-push-hook
uninstall-pre-push-hook
TEST_DIR := ./test
COVER_PKG := ./...

.PHONY: help \
lint \
test \
test-verbose \
test-coverage \
test-coverage-html \
test-clean \
mock \
install-pre-push-hook \
uninstall-pre-push-hook

help:
@echo "Available commands:"
@echo " help - Show this help message"
@echo " lint - Run golangci-lint on the codebase"
@echo " test - Run all tests"
@echo " test-verbose - Run all tests with verbose output"
@echo " test-coverage - Run all tests with coverage report"
@echo " test-coverage-html - Run all tests and generate HTML coverage report"
@echo " test-clean - Clean test cache and run tests"
@echo " install-pre-push-hook - Install the pre-push git hook"
@echo " uninstall-pre-push-hook - Uninstall the pre-push git hook"
@echo "Makefile commands:"
@echo " make lint - Run golangci-lint on the codebase"
@echo " make test - Run all tests"
@echo " make test-verbose - Run all tests with verbose output"
@echo " make test-coverage - Run all tests with coverage report"
@echo " make test-coverage-html - Run all tests and generate HTML coverage report"
@echo " make test-clean - Clean test cache and run tests"
@echo " make mock - Generate mock implementations"
@echo " make install-pre-push-hook - Install the pre-push git hook"
@echo " make uninstall-pre-push-hook - Uninstall the pre-push git hook"

lint:
golangci-lint run ./...

test-all:
test:
@echo "Running all tests..."
go test ./test/...
@if [ -d $(TEST_DIR) ]; then \
go test $(TEST_DIR)/...; \
else \
echo "No tests found in $(TEST_DIR), skipping."; \
fi

test-verbose:
@echo "Running all tests with verbose output..."
go test -v ./test/...
@if [ -d $(TEST_DIR) ]; then \
go test -v $(TEST_DIR)/...; \
else \
echo "No tests found in $(TEST_DIR), skipping."; \
fi

test-coverage:
@echo "Running all tests with coverage report..."
go test -v -cover -coverprofile=coverage.out -coverpkg=./... ./test/...
@if [ -d $(TEST_DIR) ]; then \
go test -v -cover -coverprofile=coverage.out -coverpkg=$(COVER_PKG) $(TEST_DIR)/...; \
else \
echo "No tests found in $(TEST_DIR), skipping."; \
fi

test-coverage-html:
@echo "Running all tests and generating HTML coverage report..."
go test -v -cover -coverprofile=coverage.out -coverpkg=./... ./test/...
go tool cover -html=coverage.out -o coverage.html
@echo "Coverage report generated: coverage.html"
@if [ -d $(TEST_DIR) ]; then \
go test -v -cover -coverprofile=coverage.out -coverpkg=$(COVER_PKG) $(TEST_DIR)/... && \
go tool cover -html=coverage.out -o coverage.html && \
echo "Coverage report generated: coverage.html"; \
else \
echo "No tests found in $(TEST_DIR), skipping."; \
fi

test-clean:
@echo "Cleaning test cache and running tests..."
go clean -testcache && go test -v ./test/...
@if [ -d $(TEST_DIR) ]; then \
go clean -testcache && go test -v $(TEST_DIR)/...; \
else \
echo "No tests found in $(TEST_DIR), skipping."; \
fi
Comment on lines +30 to +70

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Conditional guards silently skip tests when TEST_DIR is missing.

All test targets now check for TEST_DIR existence and echo a skip message if it's missing. While this prevents errors, it could mask issues if the directory is accidentally deleted, moved, or misnamed.

Consider whether the targets should fail loudly (exit with error) when TEST_DIR is expected but missing, especially in CI/CD environments where silent skips could give false confidence.

Example alternative approach:

 test:
 	@echo "Running all tests..."
 	@if [ -d $(TEST_DIR) ]; then \
 		go test $(TEST_DIR)/...; \
 	else \
-		echo "No tests found in $(TEST_DIR), skipping."; \
+		echo "ERROR: Test directory $(TEST_DIR) not found!"; \
+		exit 1; \
 	fi
🧰 Tools
🪛 checkmake (0.2.2)

[warning] 30-30: Target body for "test" exceeds allowed length of 5 (6).

(maxbodylength)

🤖 Prompt for AI Agents
Makefile lines 30-70: the test targets currently silently skip when $(TEST_DIR)
is missing; change the guard so CI/automated runs fail loudly by returning a
non-zero exit status if $(TEST_DIR) is expected but absent (or introduce an
environment flag like FAIL_ON_MISSING_TEST_DIR to control behavior), update each
target to print a clear error message and exit 1 when the directory is missing,
and keep the current soft-skip behavior only when the flag explicitly allows it.


mock:
@scripts/gen-mocks.sh

install-pre-push-hook:
@echo "Installing pre-push git hook..."
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/itsLeonB/ezutil/v2 v2.0.0
github.com/rotisserie/eris v0.5.4
github.com/stretchr/testify v1.11.0
go.uber.org/mock v0.6.0
gorm.io/driver/sqlite v1.6.0
gorm.io/gorm v1.30.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/rotisserie/eris v0.5.4 h1:Il6IvLdAapsMhvuOahHWiBnl1G++Q0/L5UIkI5mARSk
github.com/rotisserie/eris v0.5.4/go.mod h1:Z/kgYTJiJtocxCbFfvRmO+QejApzG6zpyky9G1A4g9s=
github.com/stretchr/testify v1.11.0 h1:ib4sjIrwZKxE5u/Japgo/7SJV3PvgjGiRNAvTVGqQl8=
github.com/stretchr/testify v1.11.0/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
25 changes: 22 additions & 3 deletions gorm_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ type Repository[T any] interface {
Update(ctx context.Context, model T) (T, error)
// Delete removes a record from the database (hard delete).
Delete(ctx context.Context, model T) error
// BatchInsert creates multiple records in a single database operation.
BatchInsert(ctx context.Context, models []T) ([]T, error)
// InsertMany creates multiple records in a single database operation.
InsertMany(ctx context.Context, models []T) ([]T, error)
// DeleteMany removes multiple records in a single database operation (hard delete).
DeleteMany(ctx context.Context, models []T) error
// GetGormInstance returns the appropriate GORM DB instance (transaction-aware).
GetGormInstance(ctx context.Context) (*gorm.DB, error)
}
Expand Down Expand Up @@ -159,7 +161,7 @@ func (gr *gormRepository[T]) Delete(ctx context.Context, model T) error {
return nil
}

func (gr *gormRepository[T]) BatchInsert(ctx context.Context, models []T) ([]T, error) {
func (gr *gormRepository[T]) InsertMany(ctx context.Context, models []T) ([]T, error) {
if len(models) < 1 {
return nil, eris.Errorf("inserted models cannot be empty")
}
Expand All @@ -176,6 +178,23 @@ func (gr *gormRepository[T]) BatchInsert(ctx context.Context, models []T) ([]T,
return models, nil
}

func (gr *gormRepository[T]) DeleteMany(ctx context.Context, models []T) error {
if len(models) < 1 {
return eris.Errorf("deleted models cannot be empty")
}

db, err := gr.GetGormInstance(ctx)
if err != nil {
return err
}

if err = db.Unscoped().Delete(&models).Error; err != nil {
return eris.Wrap(err, "error batch deleting data")
}

return nil
}

func (gr *gormRepository[T]) checkZeroValue(model T) error {
if reflect.DeepEqual(model, *new(T)) {
return eris.New("model cannot be zero value")
Expand Down
160 changes: 160 additions & 0 deletions gorm_repository_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading