125 lines
3.0 KiB
Go
125 lines
3.0 KiB
Go
package systems
|
|
|
|
import (
|
|
"clortho/lib/db"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
// Global setup
|
|
fmt.Println("Setting up resources...")
|
|
|
|
db.InitDb()
|
|
|
|
exitCode := m.Run() // Run all tests
|
|
|
|
// Global teardown
|
|
fmt.Println("Cleaning up resources...")
|
|
|
|
db.ResetDb()
|
|
|
|
os.Exit(exitCode)
|
|
}
|
|
|
|
func TestCreateSystem(t *testing.T) {
|
|
// Test creating a new system
|
|
system, err := CreateSystem("Test System")
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, system)
|
|
assert.Equal(t, "Test System", system.Name)
|
|
assert.NotZero(t, system.ID)
|
|
|
|
// Test creating a system with duplicate name (should fail)
|
|
duplicateSystem, err := CreateSystem("Test System")
|
|
assert.Error(t, err)
|
|
assert.Nil(t, duplicateSystem)
|
|
}
|
|
|
|
func TestGetSystems(t *testing.T) {
|
|
// Clear existing systems
|
|
db.Connection.Exec("DELETE FROM systems")
|
|
|
|
// Create test systems
|
|
system1, _ := CreateSystem("System 1")
|
|
system2, _ := CreateSystem("System 2")
|
|
|
|
// Get all systems
|
|
systems := GetSystems()
|
|
assert.Len(t, systems, 2)
|
|
|
|
// Verify systems are returned correctly
|
|
foundSystem1 := false
|
|
foundSystem2 := false
|
|
for _, s := range systems {
|
|
if s.ID == system1.ID {
|
|
assert.Equal(t, "System 1", s.Name)
|
|
foundSystem1 = true
|
|
}
|
|
if s.ID == system2.ID {
|
|
assert.Equal(t, "System 2", s.Name)
|
|
foundSystem2 = true
|
|
}
|
|
}
|
|
assert.True(t, foundSystem1)
|
|
assert.True(t, foundSystem2)
|
|
}
|
|
|
|
func TestGetSystem(t *testing.T) {
|
|
// Create a test system
|
|
createdSystem, _ := CreateSystem("Get System Test")
|
|
|
|
// Get the system by ID
|
|
system, err := GetSystem(createdSystem.ID)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, system)
|
|
assert.Equal(t, createdSystem.ID, system.ID)
|
|
assert.Equal(t, "Get System Test", system.Name)
|
|
|
|
// Test getting a non-existent system
|
|
nonExistentSystem, err := GetSystem(9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, nonExistentSystem)
|
|
}
|
|
|
|
func TestUpdateSystem(t *testing.T) {
|
|
// Create a test system
|
|
createdSystem, _ := CreateSystem("Update System Test")
|
|
|
|
// Update the system
|
|
updatedSystem, err := UpdateSystem(createdSystem.ID, "Updated System")
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, updatedSystem)
|
|
assert.Equal(t, createdSystem.ID, updatedSystem.ID)
|
|
assert.Equal(t, "Updated System", updatedSystem.Name)
|
|
|
|
// Verify the update in the database
|
|
system, _ := GetSystem(createdSystem.ID)
|
|
assert.Equal(t, "Updated System", system.Name)
|
|
|
|
// Test updating a non-existent system
|
|
nonExistentSystem, err := UpdateSystem(9999, "Non-existent")
|
|
assert.Error(t, err)
|
|
assert.Nil(t, nonExistentSystem)
|
|
}
|
|
|
|
func TestDeleteSystem(t *testing.T) {
|
|
// Create a test system
|
|
createdSystem, _ := CreateSystem("Delete System Test")
|
|
|
|
// Delete the system
|
|
err := DeleteSystem(createdSystem.ID)
|
|
assert.NoError(t, err)
|
|
|
|
// Verify the system is deleted
|
|
system, err := GetSystem(createdSystem.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, system)
|
|
|
|
// Test deleting a non-existent system
|
|
err = DeleteSystem(9999)
|
|
assert.NoError(t, err) // GORM doesn't return an error when deleting non-existent records
|
|
}
|