DeviceRepository单测实现
This commit is contained in:
		
							
								
								
									
										107
									
								
								internal/infra/repository/device_repository_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										107
									
								
								internal/infra/repository/device_repository_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,107 @@ | |||||||
|  | package repository_test | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"strconv" | ||||||
|  | 	"testing" | ||||||
|  |  | ||||||
|  | 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" | ||||||
|  | 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
|  | 	"gorm.io/gorm" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func TestGormDeviceRepository(t *testing.T) { | ||||||
|  | 	db := setupTestDB(t) | ||||||
|  | 	repo := repository.NewGormDeviceRepository(db) | ||||||
|  |  | ||||||
|  | 	// --- 准备测试数据 --- | ||||||
|  | 	loraProps, _ := json.Marshal(models.LoraProperties{LoraAddress: "0xABCD"}) | ||||||
|  | 	busProps, _ := json.Marshal(models.BusProperties{BusID: 1, BusAddress: 10}) | ||||||
|  |  | ||||||
|  | 	areaController := &models.Device{ | ||||||
|  | 		Name:       "1号猪舍主控", | ||||||
|  | 		Type:       models.DeviceTypeAreaController, | ||||||
|  | 		Location:   "1号猪舍", | ||||||
|  | 		Properties: loraProps, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	t.Run("创建 - 成功创建区域主控", func(t *testing.T) { | ||||||
|  | 		err := repo.Create(areaController) | ||||||
|  | 		assert.NoError(t, err) | ||||||
|  | 		assert.NotZero(t, areaController.ID, "创建后应获得一个非零ID") | ||||||
|  | 		assert.Nil(t, areaController.ParentID, "区域主控的 ParentID 应为 nil") | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	var createdDevice *models.Device | ||||||
|  | 	t.Run("通过ID查找 - 成功找到已创建的设备", func(t *testing.T) { | ||||||
|  | 		var err error | ||||||
|  | 		createdDevice, err = repo.FindByID(areaController.ID) | ||||||
|  | 		assert.NoError(t, err) | ||||||
|  | 		assert.NotNil(t, createdDevice) | ||||||
|  | 		assert.Equal(t, areaController.Name, createdDevice.Name) | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	t.Run("通过字符串ID查找 - 使用有效字符串ID找到设备", func(t *testing.T) { | ||||||
|  | 		foundDevice, err := repo.FindByIDString(strconv.FormatUint(uint64(areaController.ID), 10)) | ||||||
|  | 		assert.NoError(t, err) | ||||||
|  | 		assert.NotNil(t, foundDevice) | ||||||
|  | 		assert.Equal(t, areaController.ID, foundDevice.ID) | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	t.Run("通过字符串ID查找 - 使用无效字符串ID", func(t *testing.T) { | ||||||
|  | 		_, err := repo.FindByIDString("invalid-id") | ||||||
|  | 		assert.Error(t, err, "使用无效ID字符串应返回错误") | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	// 创建一个子设备 | ||||||
|  | 	childDevice := &models.Device{ | ||||||
|  | 		Name:       "1号猪舍温度传感器", | ||||||
|  | 		Type:       models.DeviceTypeDevice, | ||||||
|  | 		SubType:    models.SubTypeSensorTemp, | ||||||
|  | 		ParentID:   &areaController.ID, | ||||||
|  | 		Location:   "1号猪舍东侧", | ||||||
|  | 		Properties: busProps, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	t.Run("创建 - 成功创建子设备", func(t *testing.T) { | ||||||
|  | 		err := repo.Create(childDevice) | ||||||
|  | 		assert.NoError(t, err) | ||||||
|  | 		assert.NotZero(t, childDevice.ID) | ||||||
|  | 		assert.NotNil(t, childDevice.ParentID) | ||||||
|  | 		assert.Equal(t, areaController.ID, *childDevice.ParentID) | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	t.Run("通过父ID列出 - 找到子设备", func(t *testing.T) { | ||||||
|  | 		children, err := repo.ListByParentID(&areaController.ID) | ||||||
|  | 		assert.NoError(t, err) | ||||||
|  | 		assert.Len(t, children, 1, "应找到一个子设备") | ||||||
|  | 		assert.Equal(t, childDevice.ID, children[0].ID) | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	t.Run("通过父ID列出 - 找到顶层设备", func(t *testing.T) { | ||||||
|  | 		parents, err := repo.ListByParentID(nil) | ||||||
|  | 		assert.NoError(t, err) | ||||||
|  | 		assert.Len(t, parents, 1, "应找到一个顶层设备") | ||||||
|  | 		assert.Equal(t, areaController.ID, parents[0].ID) | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	t.Run("更新 - 成功更新设备信息", func(t *testing.T) { | ||||||
|  | 		childDevice.Location = "1号猪舍西侧" | ||||||
|  | 		err := repo.Update(childDevice) | ||||||
|  | 		assert.NoError(t, err) | ||||||
|  |  | ||||||
|  | 		updatedDevice, _ := repo.FindByID(childDevice.ID) | ||||||
|  | 		assert.Equal(t, "1号猪舍西侧", updatedDevice.Location) | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	t.Run("删除 - 成功删除设备", func(t *testing.T) { | ||||||
|  | 		err := repo.Delete(childDevice.ID) | ||||||
|  | 		assert.NoError(t, err) | ||||||
|  |  | ||||||
|  | 		// 验证设备已被软删除 | ||||||
|  | 		_, err = repo.FindByID(childDevice.ID) | ||||||
|  | 		assert.Error(t, err, "删除后应无法找到设备") | ||||||
|  | 		assert.ErrorIs(t, err, gorm.ErrRecordNotFound, "错误类型应为 RecordNotFound") | ||||||
|  | 	}) | ||||||
|  | } | ||||||
							
								
								
									
										38
									
								
								internal/infra/repository/main_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								internal/infra/repository/main_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | |||||||
|  | package repository_test | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"os" | ||||||
|  | 	"testing" | ||||||
|  |  | ||||||
|  | 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
|  | 	"gorm.io/driver/sqlite" | ||||||
|  | 	"gorm.io/gorm" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // setupTestDB 是一个共享的辅助函数,用于为集成测试创建一个干净的、内存中的 SQLite 数据库实例。 | ||||||
|  | func setupTestDB(t *testing.T) *gorm.DB { | ||||||
|  | 	// "file::memory:?cache=shared" 是 GORM 连接内存 SQLite 的标准方式,确保在同一测试中的不同连接可以访问相同的数据。 | ||||||
|  | 	db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{}) | ||||||
|  | 	assert.NoError(t, err, "连接内存数据库不应出错") | ||||||
|  |  | ||||||
|  | 	// 自动迁移所有需要的表结构 | ||||||
|  | 	err = db.AutoMigrate(&models.User{}, &models.Device{}) | ||||||
|  | 	assert.NoError(t, err, "数据库迁移不应出错") | ||||||
|  |  | ||||||
|  | 	return db | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // TestMain 是一个特殊的函数,它会在包内的所有测试运行之前被调用。 | ||||||
|  | // 我们可以在这里进行一些全局的设置和清理工作。 | ||||||
|  | func TestMain(m *testing.M) { | ||||||
|  | 	// 在所有测试运行前可以执行一些设置代码 | ||||||
|  |  | ||||||
|  | 	// 运行包中的所有测试 | ||||||
|  | 	code := m.Run() | ||||||
|  |  | ||||||
|  | 	// 在所有测试运行后可以执行一些清理代码 | ||||||
|  |  | ||||||
|  | 	// 退出测试 | ||||||
|  | 	os.Exit(code) | ||||||
|  | } | ||||||
| @@ -7,24 +7,9 @@ import ( | |||||||
| 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" | 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" | ||||||
| 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository" | 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository" | ||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
| 	"gorm.io/driver/sqlite" |  | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // setupTestDB 是一个辅助函数,用于为每个测试创建一个 |  | ||||||
| // 干净的、内存中的 SQLite 数据库实例。 |  | ||||||
| func setupTestDB(t *testing.T) *gorm.DB { |  | ||||||
| 	// "file::memory:?cache=shared" 是 GORM 连接内存 SQLite 的标准方式 |  | ||||||
| 	db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{}) |  | ||||||
| 	assert.NoError(t, err, "连接内存数据库不应出错") |  | ||||||
|  |  | ||||||
| 	// 自动迁移 User 表结构 |  | ||||||
| 	err = db.AutoMigrate(&models.User{}) |  | ||||||
| 	assert.NoError(t, err, "数据库迁移不应出错") |  | ||||||
|  |  | ||||||
| 	return db |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func TestGormUserRepository(t *testing.T) { | func TestGormUserRepository(t *testing.T) { | ||||||
| 	db := setupTestDB(t) | 	db := setupTestDB(t) | ||||||
| 	repo := repository.NewGormUserRepository(db) | 	repo := repository.NewGormUserRepository(db) | ||||||
| @@ -35,7 +20,7 @@ func TestGormUserRepository(t *testing.T) { | |||||||
| 		Password: plainPassword, // 我们提供的是明文密码 | 		Password: plainPassword, // 我们提供的是明文密码 | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	t.Run("Create - 成功创建并验证密码哈希", func(t *testing.T) { | 	t.Run("创建 - 成功创建并验证密码哈希", func(t *testing.T) { | ||||||
| 		err := repo.Create(userToCreate) | 		err := repo.Create(userToCreate) | ||||||
| 		assert.NoError(t, err) | 		assert.NoError(t, err) | ||||||
|  |  | ||||||
| @@ -53,7 +38,7 @@ func TestGormUserRepository(t *testing.T) { | |||||||
| 		assert.True(t, savedUser.CheckPassword(plainPassword), "存储的密码哈希应该能与原明文匹配") | 		assert.True(t, savedUser.CheckPassword(plainPassword), "存储的密码哈希应该能与原明文匹配") | ||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
| 	t.Run("Create - 用户名冲突", func(t *testing.T) { | 	t.Run("创建 - 用户名冲突", func(t *testing.T) { | ||||||
| 		// 尝试创建一个同名用户 | 		// 尝试创建一个同名用户 | ||||||
| 		duplicateUser := &models.User{Username: "testuser", Password: "anypassword"} | 		duplicateUser := &models.User{Username: "testuser", Password: "anypassword"} | ||||||
| 		err := repo.Create(duplicateUser) | 		err := repo.Create(duplicateUser) | ||||||
| @@ -64,7 +49,7 @@ func TestGormUserRepository(t *testing.T) { | |||||||
| 		assert.Contains(t, err.Error(), "UNIQUE constraint failed: users.username", "错误信息应包含唯一键冲突") | 		assert.Contains(t, err.Error(), "UNIQUE constraint failed: users.username", "错误信息应包含唯一键冲突") | ||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
| 	t.Run("FindByUsername - 找到用户", func(t *testing.T) { | 	t.Run("按用户名查找 - 找到用户", func(t *testing.T) { | ||||||
| 		foundUser, err := repo.FindByUsername("testuser") | 		foundUser, err := repo.FindByUsername("testuser") | ||||||
| 		assert.NoError(t, err) | 		assert.NoError(t, err) | ||||||
| 		assert.NotNil(t, foundUser) | 		assert.NotNil(t, foundUser) | ||||||
| @@ -72,20 +57,20 @@ func TestGormUserRepository(t *testing.T) { | |||||||
| 		assert.Equal(t, "testuser", foundUser.Username) | 		assert.Equal(t, "testuser", foundUser.Username) | ||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
| 	t.Run("FindByUsername - 未找到用户", func(t *testing.T) { | 	t.Run("按用户名查找 - 未找到用户", func(t *testing.T) { | ||||||
| 		_, err := repo.FindByUsername("nonexistent") | 		_, err := repo.FindByUsername("nonexistent") | ||||||
| 		assert.Error(t, err, "查找不存在的用户应该返回错误") | 		assert.Error(t, err, "查找不存在的用户应该返回错误") | ||||||
| 		assert.ErrorIs(t, err, gorm.ErrRecordNotFound, "错误类型应为 gorm.ErrRecordNotFound") | 		assert.ErrorIs(t, err, gorm.ErrRecordNotFound, "错误类型应为 gorm.ErrRecordNotFound") | ||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
| 	t.Run("FindByID - 找到用户", func(t *testing.T) { | 	t.Run("按ID查找 - 找到用户", func(t *testing.T) { | ||||||
| 		foundUser, err := repo.FindByID(userToCreate.ID) | 		foundUser, err := repo.FindByID(userToCreate.ID) | ||||||
| 		assert.NoError(t, err) | 		assert.NoError(t, err) | ||||||
| 		assert.NotNil(t, foundUser) | 		assert.NotNil(t, foundUser) | ||||||
| 		assert.Equal(t, userToCreate.ID, foundUser.ID) | 		assert.Equal(t, userToCreate.ID, foundUser.ID) | ||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
| 	t.Run("FindByID - 未找到用户", func(t *testing.T) { | 	t.Run("按ID查找 - 未找到用户", func(t *testing.T) { | ||||||
| 		_, err := repo.FindByID(99999) | 		_, err := repo.FindByID(99999) | ||||||
| 		assert.Error(t, err, "查找不存在的ID应该返回错误") | 		assert.Error(t, err, "查找不存在的ID应该返回错误") | ||||||
| 		assert.ErrorIs(t, err, gorm.ErrRecordNotFound, "错误类型应为 gorm.ErrRecordNotFound") | 		assert.ErrorIs(t, err, gorm.ErrRecordNotFound, "错误类型应为 gorm.ErrRecordNotFound") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user