Skip to content

Commit

Permalink
add folder dedicated to helpers for e.g. mocks, DB setup
Browse files Browse the repository at this point in the history
  • Loading branch information
MGTheTrain committed Nov 21, 2024
1 parent 40449ed commit 7da7a1c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 156 deletions.
2 changes: 1 addition & 1 deletion test/unit/app/services/mocks.go → test/helpers/mocks.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services
package helpers

import (
"crypto_vault_service/internal/domain/blobs"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package repository
package helpers

import (
"crypto_vault_service/internal/domain/blobs"
Expand All @@ -13,14 +13,14 @@ import (
"gorm.io/gorm"
)

type TestRepositoryContext struct {
type TestDBContext struct {
DB *gorm.DB
BlobRepo *repository.GormBlobRepository
CryptoKeyRepo *repository.GormCryptoKeyRepository
}

// SetupTestDB initializes the test database and repositories based on the DB_TYPE environment variable
func SetupTestDB(t *testing.T) *TestRepositoryContext {
func SetupTestDB(t *testing.T) *TestDBContext {
var err error
var db *gorm.DB

Expand Down Expand Up @@ -90,15 +90,15 @@ func SetupTestDB(t *testing.T) *TestRepositoryContext {
blobRepo := &repository.GormBlobRepository{DB: db}
cryptoKeyRepo := &repository.GormCryptoKeyRepository{DB: db}

return &TestRepositoryContext{
return &TestDBContext{
DB: db,
BlobRepo: blobRepo,
CryptoKeyRepo: cryptoKeyRepo,
}
}

// TeardownTestDB closes the DB connection after the test
func TeardownTestDB(t *testing.T, ctx *TestRepositoryContext) {
func TeardownTestDB(t *testing.T, ctx *TestDBContext) {
sqlDB, err := ctx.DB.DB()
if err != nil {
t.Fatalf("Failed to get DB connection: %v", err)
Expand Down
17 changes: 9 additions & 8 deletions test/integration/app/services/key_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto_vault_service/internal/app/services"
"crypto_vault_service/internal/domain/keys"
"crypto_vault_service/internal/infrastructure/connector"
"crypto_vault_service/test/helpers"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -39,8 +40,8 @@ func TestCryptoKeyUploadService_Upload_Success(t *testing.T) {
require.NoError(t, err, "Error setting environment variable")

// Set up test context and ensure proper teardown
ctx := setupTestDB(t)
defer teardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Initialize Vault Connector
connectionString := "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
Expand Down Expand Up @@ -86,8 +87,8 @@ func TestCryptoKeyMetadataService_GetByID_Success(t *testing.T) {
require.NoError(t, err, "Error setting environment variable")

// Set up test context and ensure proper teardown
ctx := setupTestDB(t)
defer teardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a key metadata instance in the database for testing
cryptoKeyMeta := &keys.CryptoKeyMeta{
Expand Down Expand Up @@ -127,8 +128,8 @@ func TestCryptoKeyMetadataService_DeleteByID_Success(t *testing.T) {
require.NoError(t, err, "Error setting environment variable")

// Set up test context and ensure proper teardown
ctx := setupTestDB(t)
defer teardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a key metadata instance in the database for testing
cryptoKeyMeta := &keys.CryptoKeyMeta{
Expand Down Expand Up @@ -168,8 +169,8 @@ func TestCryptoKeyDownloadService_Download_Success(t *testing.T) {
require.NoError(t, err, "Error setting environment variable")

// Set up test context and ensure proper teardown
ctx := setupTestDB(t)
defer teardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Initialize Vault Connector
connectionString := "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
Expand Down
121 changes: 0 additions & 121 deletions test/integration/app/services/test_repository_context.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package repository
import (
"crypto_vault_service/internal/domain/blobs"
"crypto_vault_service/internal/domain/keys"
"crypto_vault_service/test/helpers"

"os"
"testing"
"time"
Expand All @@ -19,8 +21,8 @@ func TestBlobPsqlRepository_Create(t *testing.T) {
}

// Set up test context
ctx := SetupTestDB(t)
defer TeardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a valid CryptoKey object
cryptographicKey := keys.CryptoKeyMeta{
Expand Down Expand Up @@ -62,8 +64,8 @@ func TestBlobPsqlRepository_GetById(t *testing.T) {
}

// Set up test context
ctx := SetupTestDB(t)
defer TeardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a valid CryptoKey object
cryptographicKey := keys.CryptoKeyMeta{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package repository
import (
"crypto_vault_service/internal/domain/blobs"
"crypto_vault_service/internal/domain/keys"
"crypto_vault_service/test/helpers"

"os"
"testing"
"time"
Expand All @@ -19,8 +21,8 @@ func TestBlobSqliteRepository_Create(t *testing.T) {
}

// Set up test context
ctx := SetupTestDB(t)
defer TeardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a valid CryptoKey object
cryptographicKey := keys.CryptoKeyMeta{
Expand Down Expand Up @@ -62,8 +64,8 @@ func TestBlobSqliteRepository_GetById(t *testing.T) {
}

// Set up test context
ctx := SetupTestDB(t)
defer TeardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a valid CryptoKey object
cryptographicKey := keys.CryptoKeyMeta{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package repository

import (
"crypto_vault_service/internal/domain/keys"
"crypto_vault_service/test/helpers"

"os"
"testing"
"time"
Expand All @@ -19,8 +21,8 @@ func TestCryptoKeySqliteRepository_Create(t *testing.T) {
}

// Set up test context
ctx := SetupTestDB(t)
defer TeardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a valid CryptoKey object
cryptoKeyMeta := &keys.CryptoKeyMeta{
Expand Down Expand Up @@ -51,8 +53,8 @@ func TestCryptoKeySqliteRepository_GetByID(t *testing.T) {
}

// Set up test context
ctx := SetupTestDB(t)
defer TeardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a valid CryptoKey object
cryptoKeyMeta := &keys.CryptoKeyMeta{
Expand Down Expand Up @@ -82,8 +84,8 @@ func TestCryptoKeySqliteRepository_UpdateByID(t *testing.T) {
}

// Set up test context
ctx := SetupTestDB(t)
defer TeardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a valid CryptoKey object
cryptoKeyMeta := &keys.CryptoKeyMeta{
Expand Down Expand Up @@ -113,8 +115,8 @@ func TestCryptoKeySqliteRepository_UpdateByID(t *testing.T) {
// TestCryptoKeySqliteRepository_DeleteByID tests the DeleteByID method of GormCryptoKeyRepository
func TestCryptoKeySqliteRepository_DeleteByID(t *testing.T) {
// Set up test context
ctx := SetupTestDB(t)
defer TeardownTestDB(t, ctx)
ctx := helpers.SetupTestDB(t)
defer helpers.TeardownTestDB(t, ctx)

// Create a valid CryptoKey object
cryptoKeyMeta := &keys.CryptoKeyMeta{
Expand Down
12 changes: 7 additions & 5 deletions test/unit/app/services/blob_services_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package services
import (
"crypto_vault_service/internal/app/services"
"crypto_vault_service/internal/domain/blobs"
"crypto_vault_service/test/helpers"

"fmt"
"testing"

Expand All @@ -12,8 +14,8 @@ import (

func TestBlobUploadService_Upload(t *testing.T) {
// Prepare mock dependencies
mockBlobConnector := new(MockBlobConnector)
mockBlobRepository := new(MockBlobRepository)
mockBlobConnector := new(helpers.MockBlobConnector)
mockBlobRepository := new(helpers.MockBlobRepository)

// Initialize the service with mock dependencies
service := services.NewBlobUploadService(mockBlobConnector, mockBlobRepository)
Expand Down Expand Up @@ -73,8 +75,8 @@ func TestBlobUploadService_Upload(t *testing.T) {

func TestBlobMetadataService(t *testing.T) {
// Prepare mock dependencies
mockBlobConnector := new(MockBlobConnector)
mockBlobRepository := new(MockBlobRepository)
mockBlobConnector := new(helpers.MockBlobConnector)
mockBlobRepository := new(helpers.MockBlobRepository)

// Initialize the service with mock dependencies
metadataService := services.NewBlobMetadataService(mockBlobRepository, mockBlobConnector)
Expand Down Expand Up @@ -104,7 +106,7 @@ func TestBlobMetadataService(t *testing.T) {

func TestBlobDownloadService(t *testing.T) {
// Prepare mock dependencies
mockBlobConnector := new(MockBlobConnector)
mockBlobConnector := new(helpers.MockBlobConnector)

// Initialize the service with mock dependencies
downloadService := services.NewBlobDownloadService(mockBlobConnector)
Expand Down

0 comments on commit 7da7a1c

Please sign in to comment.