mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
Some checks failed
CI/CD Pipeline / backend-lint (push) Failing after 1m1s
CI/CD Pipeline / backend-build (push) Has been skipped
CI/CD Pipeline / frontend-lint (push) Successful in 1m37s
CI/CD Pipeline / frontend-build (push) Failing after 35s
CI/CD Pipeline / e2e-test (push) Has been skipped
- Replace pgtestdb with Testcontainers for improved test isolation and reliability - Update test setup functions to spin up dedicated PostgreSQL containers for each test - Ensure automatic cleanup of containers after tests to prevent resource leaks - Modify documentation to reflect changes in testing methodology and benefits of using Testcontainers
179 lines
5.3 KiB
Go
179 lines
5.3 KiB
Go
|
|
package repository_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"bugulma/backend/internal/testutils"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
"gorm.io/gorm"
|
|
|
|
"bugulma/backend/internal/domain"
|
|
"bugulma/backend/internal/repository"
|
|
)
|
|
|
|
type SiteRepositoryTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
repo domain.SiteRepository
|
|
orgRepo domain.OrganizationRepository
|
|
}
|
|
|
|
func (suite *SiteRepositoryTestSuite) SetupTest() {
|
|
// Setup PostgreSQL test database using pgtestdb
|
|
suite.db = testutils.SetupTestDBWithTestcontainers(suite.T())
|
|
suite.repo = repository.NewSiteRepository(suite.db)
|
|
suite.orgRepo = repository.NewOrganizationRepository(suite.db)
|
|
}
|
|
|
|
func (suite *SiteRepositoryTestSuite) TearDownTest() {
|
|
// pgtestdb automatically cleans up the database after each test
|
|
}
|
|
|
|
func (suite *SiteRepositoryTestSuite) TestCreate() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Test Organization"}
|
|
suite.Require().NoError(suite.orgRepo.Create(context.Background(), org))
|
|
|
|
site := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Test Site",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
|
|
err := suite.repo.Create(context.Background(), site)
|
|
assert.NoError(suite.T(), err)
|
|
|
|
// Verify
|
|
found, err := suite.repo.GetByID(context.Background(), "site-1")
|
|
assert.NoError(suite.T(), err)
|
|
assert.Equal(suite.T(), "Test Site", found.Name)
|
|
}
|
|
|
|
func (suite *SiteRepositoryTestSuite) TestGetByID() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Test Organization"}
|
|
suite.Require().NoError(suite.orgRepo.Create(context.Background(), org))
|
|
|
|
site := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Test Site",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
err := suite.repo.Create(context.Background(), site)
|
|
suite.Require().NoError(err)
|
|
|
|
found, err := suite.repo.GetByID(context.Background(), "site-1")
|
|
assert.NoError(suite.T(), err)
|
|
assert.Equal(suite.T(), "Test Site", found.Name)
|
|
}
|
|
|
|
func (suite *SiteRepositoryTestSuite) TestGetByOrganizationID() {
|
|
// Create organizations first
|
|
org1 := &domain.Organization{ID: "org-1", Name: "Test Organization 1"}
|
|
org2 := &domain.Organization{ID: "org-2", Name: "Test Organization 2"}
|
|
suite.Require().NoError(suite.orgRepo.Create(context.Background(), org1))
|
|
suite.Require().NoError(suite.orgRepo.Create(context.Background(), org2))
|
|
|
|
site1 := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Site 1",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
site2 := &domain.Site{
|
|
ID: "site-2",
|
|
Name: "Site 2",
|
|
Latitude: 52.53,
|
|
Longitude: 13.406,
|
|
OwnerOrganizationID: "org-2",
|
|
}
|
|
err := suite.repo.Create(context.Background(), site1)
|
|
suite.Require().NoError(err)
|
|
err = suite.repo.Create(context.Background(), site2)
|
|
suite.Require().NoError(err)
|
|
|
|
sites, err := suite.repo.GetByOrganizationID(context.Background(), "org-1")
|
|
assert.NoError(suite.T(), err)
|
|
assert.Len(suite.T(), sites, 1)
|
|
assert.Equal(suite.T(), "Site 1", sites[0].Name)
|
|
}
|
|
|
|
func (suite *SiteRepositoryTestSuite) TestGetWithinRadius() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Test Organization"}
|
|
suite.Require().NoError(suite.orgRepo.Create(context.Background(), org))
|
|
|
|
site1 := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Site 1",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
err := suite.repo.Create(context.Background(), site1)
|
|
suite.Require().NoError(err)
|
|
|
|
sites, err := suite.repo.GetWithinRadius(context.Background(), 52.52, 13.405, 10)
|
|
assert.NoError(suite.T(), err)
|
|
assert.Len(suite.T(), sites, 1)
|
|
}
|
|
|
|
func (suite *SiteRepositoryTestSuite) TestUpdate() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Test Organization"}
|
|
suite.Require().NoError(suite.orgRepo.Create(context.Background(), org))
|
|
|
|
site := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Test Site",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
err := suite.repo.Create(context.Background(), site)
|
|
suite.Require().NoError(err)
|
|
|
|
site.Name = "Updated Site"
|
|
err = suite.repo.Update(context.Background(), site)
|
|
assert.NoError(suite.T(), err)
|
|
|
|
found, err := suite.repo.GetByID(context.Background(), "site-1")
|
|
assert.NoError(suite.T(), err)
|
|
assert.Equal(suite.T(), "Updated Site", found.Name)
|
|
}
|
|
|
|
func (suite *SiteRepositoryTestSuite) TestDelete() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Test Organization"}
|
|
suite.Require().NoError(suite.orgRepo.Create(context.Background(), org))
|
|
|
|
site := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Test Site",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
err := suite.repo.Create(context.Background(), site)
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.repo.Delete(context.Background(), "site-1")
|
|
assert.NoError(suite.T(), err)
|
|
|
|
_, err = suite.repo.GetByID(context.Background(), "site-1")
|
|
assert.Error(suite.T(), err)
|
|
}
|
|
|
|
func TestSiteRepositoryTestSuite(t *testing.T) {
|
|
suite.Run(t, new(SiteRepositoryTestSuite))
|
|
}
|