fix housing database access

This commit is contained in:
Sky Johnson 2025-08-03 21:13:17 -05:00
parent aa362bde07
commit a3a10406d5
3 changed files with 2313 additions and 571 deletions

View File

@ -0,0 +1,777 @@
package housing
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
"zombiezen.com/go/sqlite/sqlitex"
)
// setupBenchmarkDB creates a test database with sample data for benchmarking
func setupBenchmarkDB(b *testing.B) (*DatabaseHousingManager, context.Context) {
// Create truly unique database name to avoid cross-benchmark contamination
dbName := fmt.Sprintf("file:bench_%s_%d.db?mode=memory&cache=shared", b.Name(), rand.Int63())
pool, err := sqlitex.NewPool(dbName, sqlitex.PoolOptions{})
if err != nil {
b.Fatalf("Failed to create benchmark database pool: %v", err)
}
dhm := NewDatabaseHousingManager(pool)
ctx := context.Background()
if err := dhm.EnsureHousingTables(ctx); err != nil {
b.Fatalf("Failed to create benchmark tables: %v", err)
}
return dhm, ctx
}
// insertBenchmarkData inserts a large dataset for benchmarking
func insertBenchmarkData(b *testing.B, dhm *DatabaseHousingManager, ctx context.Context, houseZones, playerHouses int) {
// Insert house zones
for i := 1; i <= houseZones; i++ {
zone := &HouseZone{
ID: int32(i),
Name: fmt.Sprintf("House Type %d", i),
ZoneID: int32(100 + i),
CostCoin: int64(50000 + i*10000),
CostStatus: int64(1000 + i*100),
UpkeepCoin: int64(5000 + i*1000),
UpkeepStatus: int64(100 + i*10),
Alignment: int8(i % 3 - 1), // -1, 0, 1
GuildLevel: int8(i % 50),
VaultSlots: int(4 + i%4),
MaxItems: int(100 + i*50),
MaxVisitors: int(10 + i*5),
UpkeepPeriod: 604800, // 1 week
Description: fmt.Sprintf("Benchmark house type %d description", i),
}
if err := dhm.SaveHouseZone(ctx, zone); err != nil {
b.Fatalf("Failed to insert benchmark house zone: %v", err)
}
}
// Insert player houses
for i := 1; i <= playerHouses; i++ {
houseData := PlayerHouseData{
CharacterID: int32(1000 + i),
HouseID: int32((i % houseZones) + 1),
InstanceID: int32(5000 + i),
UpkeepDue: time.Now().Add(time.Duration(i%168) * time.Hour), // Random within a week
EscrowCoins: int64(10000 + i*1000),
EscrowStatus: int64(200 + i*20),
Status: HouseStatusActive,
HouseName: fmt.Sprintf("House %d", i),
VisitPermission: int8(i % 3),
PublicNote: fmt.Sprintf("Welcome to house %d!", i),
PrivateNote: fmt.Sprintf("Private note %d", i),
AllowFriends: i%2 == 0,
AllowGuild: i%3 == 0,
RequireApproval: i%4 == 0,
ShowOnDirectory: i%5 != 0,
AllowDecoration: i%6 != 0,
TaxExempt: i%10 == 0,
}
_, err := dhm.AddPlayerHouse(ctx, houseData)
if err != nil {
b.Fatalf("Failed to insert benchmark player house: %v", err)
}
}
}
// insertHouseRelatedData inserts deposits, history, access, etc. for benchmarking
func insertHouseRelatedData(b *testing.B, dhm *DatabaseHousingManager, ctx context.Context, houseID int64, entries int) {
// Insert deposits
for i := 1; i <= entries; i++ {
deposit := HouseDeposit{
Timestamp: time.Now().Add(-time.Duration(i) * time.Hour),
Amount: int64(1000 + i*100),
LastAmount: int64(2000 + i*100),
Status: int64(50 + i*5),
LastStatus: int64(100 + i*5),
Name: fmt.Sprintf("Player %d", i%10+1),
CharacterID: int32(2000 + i%10),
}
if err := dhm.SaveDeposit(ctx, houseID, deposit); err != nil {
b.Fatalf("Failed to insert benchmark deposit: %v", err)
}
}
// Insert history
for i := 1; i <= entries; i++ {
history := HouseHistory{
Timestamp: time.Now().Add(-time.Duration(i*2) * time.Hour),
Amount: int64(500 + i*50),
Status: int64(25 + i*2),
Reason: fmt.Sprintf("Transaction %d", i),
Name: fmt.Sprintf("Player %d", i%10+1),
CharacterID: int32(2000 + i%10),
PosFlag: int8(i % 2),
Type: int(i % 5),
}
if err := dhm.AddHistory(ctx, houseID, history); err != nil {
b.Fatalf("Failed to insert benchmark history: %v", err)
}
}
// Insert access entries
accessList := make([]HouseAccess, 0, entries/10) // Fewer access entries
for i := 1; i <= entries/10; i++ {
access := HouseAccess{
CharacterID: int32(3000 + i),
PlayerName: fmt.Sprintf("AccessPlayer%d", i),
AccessLevel: int8(i % 3),
Permissions: int32(i % 16), // 0-15
GrantedBy: int32(1001),
GrantedDate: time.Now().Add(-time.Duration(i*24) * time.Hour),
ExpiresDate: time.Now().Add(time.Duration(30-i) * 24 * time.Hour),
Notes: fmt.Sprintf("Access notes for player %d", i),
}
accessList = append(accessList, access)
}
if len(accessList) > 0 {
if err := dhm.SaveHouseAccess(ctx, houseID, accessList); err != nil {
b.Fatalf("Failed to insert benchmark access: %v", err)
}
}
// Insert amenities
for i := 1; i <= entries/5; i++ {
amenity := HouseAmenity{
ID: int32(i),
Type: int(i % 10),
Name: fmt.Sprintf("Amenity %d", i),
Cost: int64(1000 + i*500),
StatusCost: int64(20 + i*10),
PurchaseDate: time.Now().Add(-time.Duration(i*12) * time.Hour),
X: float32(100 + i*10),
Y: float32(200 + i*15),
Z: float32(50 + i*5),
Heading: float32(i % 360),
IsActive: i%2 == 0,
}
if err := dhm.SaveHouseAmenity(ctx, houseID, amenity); err != nil {
b.Fatalf("Failed to insert benchmark amenity: %v", err)
}
}
// Insert items
for i := 1; i <= entries/3; i++ {
item := HouseItem{
ID: int64(i),
ItemID: int32(10000 + i),
CharacterID: int32(1001),
X: float32(150 + i*5),
Y: float32(250 + i*7),
Z: float32(75 + i*3),
Heading: float32(i % 360),
PitchX: float32(i % 10),
PitchY: float32(i % 5),
RollX: float32(i % 15),
RollY: float32(i % 8),
PlacedDate: time.Now().Add(-time.Duration(i*6) * time.Hour),
Quantity: int32(1 + i%5),
Condition: int8(100 - i%100),
House: fmt.Sprintf("room_%d", i%5),
}
if err := dhm.SaveHouseItem(ctx, houseID, item); err != nil {
b.Fatalf("Failed to insert benchmark item: %v", err)
}
}
}
// BenchmarkLoadHouseZones benchmarks loading all house zones
func BenchmarkLoadHouseZones(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 100, 0) // 100 house zones, no player houses
b.ResetTimer()
for i := 0; i < b.N; i++ {
zones, err := dhm.LoadHouseZones(ctx)
if err != nil {
b.Fatalf("LoadHouseZones failed: %v", err)
}
if len(zones) != 100 {
b.Errorf("Expected 100 zones, got %d", len(zones))
}
}
}
// BenchmarkLoadHouseZone benchmarks loading a single house zone
func BenchmarkLoadHouseZone(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 100, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
zone, err := dhm.LoadHouseZone(ctx, int32((i%100)+1))
if err != nil {
b.Fatalf("LoadHouseZone failed: %v", err)
}
if zone == nil {
b.Error("LoadHouseZone returned nil zone")
}
}
}
// BenchmarkSaveHouseZone benchmarks saving a house zone
func BenchmarkSaveHouseZone(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
zone := &HouseZone{
ID: int32(1000 + i),
Name: fmt.Sprintf("Benchmark House %d", i),
ZoneID: int32(2000 + i),
CostCoin: int64(50000 + i*1000),
CostStatus: int64(1000 + i*10),
UpkeepCoin: int64(5000 + i*100),
UpkeepStatus: int64(100 + i),
Alignment: int8(i % 3 - 1),
GuildLevel: int8(i % 50),
VaultSlots: int(4 + i%4),
MaxItems: int(100 + i*10),
MaxVisitors: int(10 + i),
UpkeepPeriod: 604800,
Description: fmt.Sprintf("Benchmark description %d", i),
}
if err := dhm.SaveHouseZone(ctx, zone); err != nil {
b.Fatalf("SaveHouseZone failed: %v", err)
}
}
}
// BenchmarkLoadPlayerHouses benchmarks loading player houses
func BenchmarkLoadPlayerHouses(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 10, 1000) // 10 house types, 1000 player houses
b.ResetTimer()
for i := 0; i < b.N; i++ {
characterID := int32(1000 + (i%1000) + 1)
houses, err := dhm.LoadPlayerHouses(ctx, characterID)
if err != nil {
b.Fatalf("LoadPlayerHouses failed: %v", err)
}
if len(houses) != 1 {
b.Errorf("Expected 1 house for character %d, got %d", characterID, len(houses))
}
}
}
// BenchmarkLoadPlayerHouse benchmarks loading a single player house
func BenchmarkLoadPlayerHouse(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 10, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
houseID := int64((i % 100) + 1)
house, err := dhm.LoadPlayerHouse(ctx, houseID)
if err != nil {
b.Fatalf("LoadPlayerHouse failed: %v", err)
}
if house == nil {
b.Error("LoadPlayerHouse returned nil house")
}
}
}
// BenchmarkAddPlayerHouse benchmarks adding player houses
func BenchmarkAddPlayerHouse(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 10, 0) // Just house zones
b.ResetTimer()
for i := 0; i < b.N; i++ {
houseData := PlayerHouseData{
CharacterID: int32(5000 + i),
HouseID: int32((i % 10) + 1),
InstanceID: int32(10000 + i),
UpkeepDue: time.Now().Add(24 * time.Hour),
EscrowCoins: int64(25000 + i*100),
EscrowStatus: int64(500 + i*5),
Status: HouseStatusActive,
HouseName: fmt.Sprintf("Benchmark House %d", i),
VisitPermission: int8(i % 3),
PublicNote: fmt.Sprintf("Welcome to benchmark house %d", i),
PrivateNote: fmt.Sprintf("Private note %d", i),
AllowFriends: i%2 == 0,
AllowGuild: i%3 == 0,
RequireApproval: i%4 == 0,
ShowOnDirectory: i%5 != 0,
AllowDecoration: i%6 != 0,
TaxExempt: i%10 == 0,
}
_, err := dhm.AddPlayerHouse(ctx, houseData)
if err != nil {
b.Fatalf("AddPlayerHouse failed: %v", err)
}
}
}
// BenchmarkLoadDeposits benchmarks loading house deposits
func BenchmarkLoadDeposits(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
// Insert deposit data for house ID 1
insertHouseRelatedData(b, dhm, ctx, 1, 500) // 500 deposits
b.ResetTimer()
for i := 0; i < b.N; i++ {
deposits, err := dhm.LoadDeposits(ctx, 1)
if err != nil {
b.Fatalf("LoadDeposits failed: %v", err)
}
if len(deposits) == 0 {
b.Error("LoadDeposits returned no deposits")
}
}
}
// BenchmarkSaveDeposit benchmarks saving deposits
func BenchmarkSaveDeposit(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
deposit := HouseDeposit{
Timestamp: time.Now(),
Amount: int64(1000 + i*10),
LastAmount: int64(2000 + i*10),
Status: int64(50 + i),
LastStatus: int64(100 + i),
Name: fmt.Sprintf("Benchmark Player %d", i),
CharacterID: int32(2000 + i),
}
if err := dhm.SaveDeposit(ctx, 1, deposit); err != nil {
b.Fatalf("SaveDeposit failed: %v", err)
}
}
}
// BenchmarkLoadHistory benchmarks loading house history
func BenchmarkLoadHistory(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
// Insert history data for house ID 1
insertHouseRelatedData(b, dhm, ctx, 1, 500) // 500 history entries
b.ResetTimer()
for i := 0; i < b.N; i++ {
history, err := dhm.LoadHistory(ctx, 1)
if err != nil {
b.Fatalf("LoadHistory failed: %v", err)
}
if len(history) == 0 {
b.Error("LoadHistory returned no history")
}
}
}
// BenchmarkAddHistory benchmarks adding history entries
func BenchmarkAddHistory(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
history := HouseHistory{
Timestamp: time.Now(),
Amount: int64(500 + i*5),
Status: int64(25 + i),
Reason: fmt.Sprintf("Benchmark transaction %d", i),
Name: fmt.Sprintf("Benchmark Player %d", i),
CharacterID: int32(2000 + i),
PosFlag: int8(i % 2),
Type: int(i % 5),
}
if err := dhm.AddHistory(ctx, 1, history); err != nil {
b.Fatalf("AddHistory failed: %v", err)
}
}
}
// BenchmarkLoadHouseAccess benchmarks loading house access
func BenchmarkLoadHouseAccess(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
// Insert access data for house ID 1
insertHouseRelatedData(b, dhm, ctx, 1, 100) // Will create 10 access entries
b.ResetTimer()
for i := 0; i < b.N; i++ {
access, err := dhm.LoadHouseAccess(ctx, 1)
if err != nil {
b.Fatalf("LoadHouseAccess failed: %v", err)
}
if len(access) == 0 {
b.Error("LoadHouseAccess returned no access entries")
}
}
}
// BenchmarkSaveHouseAccess benchmarks saving house access
func BenchmarkSaveHouseAccess(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
accessList := []HouseAccess{
{
CharacterID: int32(4000 + i),
PlayerName: fmt.Sprintf("BenchPlayer%d", i),
AccessLevel: int8(i % 3),
Permissions: int32(i % 16),
GrantedBy: 1001,
GrantedDate: time.Now(),
ExpiresDate: time.Now().Add(30 * 24 * time.Hour),
Notes: fmt.Sprintf("Benchmark access %d", i),
},
}
if err := dhm.SaveHouseAccess(ctx, 1, accessList); err != nil {
b.Fatalf("SaveHouseAccess failed: %v", err)
}
}
}
// BenchmarkLoadHouseAmenities benchmarks loading house amenities
func BenchmarkLoadHouseAmenities(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
// Insert amenity data for house ID 1
insertHouseRelatedData(b, dhm, ctx, 1, 100) // Will create 20 amenities
b.ResetTimer()
for i := 0; i < b.N; i++ {
amenities, err := dhm.LoadHouseAmenities(ctx, 1)
if err != nil {
b.Fatalf("LoadHouseAmenities failed: %v", err)
}
if len(amenities) == 0 {
b.Error("LoadHouseAmenities returned no amenities")
}
}
}
// BenchmarkSaveHouseAmenity benchmarks saving house amenities
func BenchmarkSaveHouseAmenity(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
amenity := HouseAmenity{
ID: int32(5000 + i),
Type: int(i % 10),
Name: fmt.Sprintf("Benchmark Amenity %d", i),
Cost: int64(1000 + i*100),
StatusCost: int64(20 + i*2),
PurchaseDate: time.Now(),
X: float32(100 + i),
Y: float32(200 + i),
Z: float32(50 + i),
Heading: float32(i % 360),
IsActive: i%2 == 0,
}
if err := dhm.SaveHouseAmenity(ctx, 1, amenity); err != nil {
b.Fatalf("SaveHouseAmenity failed: %v", err)
}
}
}
// BenchmarkLoadHouseItems benchmarks loading house items
func BenchmarkLoadHouseItems(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
// Insert item data for house ID 1
insertHouseRelatedData(b, dhm, ctx, 1, 150) // Will create 50 items
b.ResetTimer()
for i := 0; i < b.N; i++ {
items, err := dhm.LoadHouseItems(ctx, 1)
if err != nil {
b.Fatalf("LoadHouseItems failed: %v", err)
}
if len(items) == 0 {
b.Error("LoadHouseItems returned no items")
}
}
}
// BenchmarkSaveHouseItem benchmarks saving house items
func BenchmarkSaveHouseItem(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
item := HouseItem{
ID: int64(6000 + i),
ItemID: int32(20000 + i),
CharacterID: 1001,
X: float32(150 + i),
Y: float32(250 + i),
Z: float32(75 + i),
Heading: float32(i % 360),
PitchX: float32(i % 10),
PitchY: float32(i % 5),
RollX: float32(i % 15),
RollY: float32(i % 8),
PlacedDate: time.Now(),
Quantity: int32(1 + i%5),
Condition: int8(100 - i%100),
House: "benchmark",
}
if err := dhm.SaveHouseItem(ctx, 1, item); err != nil {
b.Fatalf("SaveHouseItem failed: %v", err)
}
}
}
// BenchmarkUpdateHouseUpkeepDue benchmarks updating house upkeep due date
func BenchmarkUpdateHouseUpkeepDue(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
houseID := int64((i % 100) + 1)
newUpkeepDue := time.Now().Add(time.Duration(i) * time.Hour)
if err := dhm.UpdateHouseUpkeepDue(ctx, houseID, newUpkeepDue); err != nil {
b.Fatalf("UpdateHouseUpkeepDue failed: %v", err)
}
}
}
// BenchmarkUpdateHouseEscrow benchmarks updating house escrow
func BenchmarkUpdateHouseEscrow(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
houseID := int64((i % 100) + 1)
coins := int64(50000 + i*1000)
status := int64(1000 + i*10)
if err := dhm.UpdateHouseEscrow(ctx, houseID, coins, status); err != nil {
b.Fatalf("UpdateHouseEscrow failed: %v", err)
}
}
}
// BenchmarkGetHousesForUpkeep benchmarks getting houses for upkeep
func BenchmarkGetHousesForUpkeep(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 10, 1000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cutoffTime := time.Now().Add(time.Duration(i%200) * time.Hour)
houses, err := dhm.GetHousesForUpkeep(ctx, cutoffTime)
if err != nil {
b.Fatalf("GetHousesForUpkeep failed: %v", err)
}
_ = houses // Prevent unused variable warning
}
}
// BenchmarkGetHouseStatistics benchmarks getting house statistics
func BenchmarkGetHouseStatistics(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 20, 2000)
// Add some deposits and history for more realistic stats
insertHouseRelatedData(b, dhm, ctx, 1, 100)
insertHouseRelatedData(b, dhm, ctx, 2, 150)
b.ResetTimer()
for i := 0; i < b.N; i++ {
stats, err := dhm.GetHouseStatistics(ctx)
if err != nil {
b.Fatalf("GetHouseStatistics failed: %v", err)
}
if stats.TotalHouses != 2000 {
b.Errorf("Expected 2000 total houses, got %d", stats.TotalHouses)
}
}
}
// BenchmarkGetHouseByInstance benchmarks finding houses by instance ID
func BenchmarkGetHouseByInstance(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 10, 1000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
instanceID := int32(5001 + (i % 1000))
house, err := dhm.GetHouseByInstance(ctx, instanceID)
if err != nil {
b.Fatalf("GetHouseByInstance failed: %v", err)
}
if house == nil {
b.Error("GetHouseByInstance returned nil house")
}
}
}
// BenchmarkGetNextHouseID benchmarks getting the next house ID
func BenchmarkGetNextHouseID(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 5, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
nextID, err := dhm.GetNextHouseID(ctx)
if err != nil {
b.Fatalf("GetNextHouseID failed: %v", err)
}
if nextID <= 100 {
b.Errorf("Expected next ID > 100, got %d", nextID)
}
}
}
// BenchmarkDeletePlayerHouse benchmarks deleting player houses
func BenchmarkDeletePlayerHouse(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
// We need to create houses to delete in each iteration
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
// Create a house to delete
houseData := PlayerHouseData{
CharacterID: int32(7000 + i),
HouseID: 1,
InstanceID: int32(8000 + i),
UpkeepDue: time.Now().Add(24 * time.Hour),
EscrowCoins: 25000,
EscrowStatus: 500,
Status: HouseStatusActive,
HouseName: fmt.Sprintf("DeleteMe %d", i),
VisitPermission: 1,
AllowFriends: true,
ShowOnDirectory: true,
}
houseID, err := dhm.AddPlayerHouse(ctx, houseData)
if err != nil {
b.Fatalf("Failed to create house for deletion: %v", err)
}
b.StartTimer()
// Delete the house
if err := dhm.DeletePlayerHouse(ctx, houseID); err != nil {
b.Fatalf("DeletePlayerHouse failed: %v", err)
}
}
}
// BenchmarkConcurrentReads benchmarks concurrent read operations
func BenchmarkConcurrentReads(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 10, 100)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
characterID := int32(1001 + (i % 100))
houses, err := dhm.LoadPlayerHouses(ctx, characterID)
if err != nil {
b.Errorf("LoadPlayerHouses failed: %v", err)
}
if len(houses) != 1 {
b.Errorf("Expected 1 house, got %d", len(houses))
}
i++
}
})
}
// BenchmarkConcurrentWrites benchmarks concurrent write operations
func BenchmarkConcurrentWrites(b *testing.B) {
dhm, ctx := setupBenchmarkDB(b)
insertBenchmarkData(b, dhm, ctx, 10, 0)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
houseData := PlayerHouseData{
CharacterID: int32(10000 + i),
HouseID: int32((i % 10) + 1),
InstanceID: int32(20000 + i),
UpkeepDue: time.Now().Add(24 * time.Hour),
EscrowCoins: 25000,
EscrowStatus: 500,
Status: HouseStatusActive,
HouseName: fmt.Sprintf("Concurrent House %d", i),
VisitPermission: 1,
AllowFriends: true,
ShowOnDirectory: true,
}
_, err := dhm.AddPlayerHouse(ctx, houseData)
if err != nil {
b.Errorf("AddPlayerHouse failed: %v", err)
}
i++
}
})
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,816 @@
package housing
import (
"context"
"fmt"
"testing"
"time"
"zombiezen.com/go/sqlite/sqlitex"
)
// createTestPool creates an in-memory SQLite database pool for testing
func createTestPool(t *testing.T) *sqlitex.Pool {
dbName := fmt.Sprintf("file:test_%s.db?mode=memory&cache=shared", t.Name())
pool, err := sqlitex.NewPool(dbName, sqlitex.PoolOptions{})
if err != nil {
t.Fatalf("Failed to create test database pool: %v", err)
}
return pool
}
// setupTestDB creates test tables and returns a DatabaseHousingManager
func setupTestDB(t *testing.T) *DatabaseHousingManager {
pool := createTestPool(t)
dhm := NewDatabaseHousingManager(pool)
ctx := context.Background()
if err := dhm.EnsureHousingTables(ctx); err != nil {
t.Fatalf("Failed to create test tables: %v", err)
}
return dhm
}
// insertTestData inserts sample data for testing
func insertTestData(t *testing.T, dhm *DatabaseHousingManager) {
ctx := context.Background()
// Insert test house zones
testZones := []*HouseZone{
{
ID: 1,
Name: "Small Studio",
ZoneID: 100,
CostCoin: 50000,
CostStatus: 1000,
UpkeepCoin: 5000,
UpkeepStatus: 100,
Alignment: 0, // Neutral
GuildLevel: 0,
VaultSlots: 4,
MaxItems: 100,
MaxVisitors: 10,
UpkeepPeriod: 604800, // 1 week
Description: "A cozy small studio apartment",
},
{
ID: 2,
Name: "Large House",
ZoneID: 101,
CostCoin: 500000,
CostStatus: 10000,
UpkeepCoin: 50000,
UpkeepStatus: 1000,
Alignment: 1, // Good
GuildLevel: 20,
VaultSlots: 8,
MaxItems: 500,
MaxVisitors: 50,
UpkeepPeriod: 604800,
Description: "A spacious large house",
},
}
for _, zone := range testZones {
if err := dhm.SaveHouseZone(ctx, zone); err != nil {
t.Fatalf("Failed to insert test house zone: %v", err)
}
}
// Insert test player houses
testPlayerHouses := []PlayerHouseData{
{
CharacterID: 1001,
HouseID: 1,
InstanceID: 5001,
UpkeepDue: time.Now().Add(24 * time.Hour),
EscrowCoins: 25000,
EscrowStatus: 500,
Status: HouseStatusActive,
HouseName: "Alice's Studio",
VisitPermission: 1,
PublicNote: "Welcome to my home!",
PrivateNote: "Remember to water plants",
AllowFriends: true,
AllowGuild: false,
RequireApproval: false,
ShowOnDirectory: true,
AllowDecoration: true,
TaxExempt: false,
},
{
CharacterID: 1002,
HouseID: 2,
InstanceID: 5002,
UpkeepDue: time.Now().Add(48 * time.Hour),
EscrowCoins: 100000,
EscrowStatus: 2000,
Status: HouseStatusActive,
HouseName: "Bob's Manor",
VisitPermission: 2,
PublicNote: "Guild meetings welcome",
PrivateNote: "Check security settings",
AllowFriends: true,
AllowGuild: true,
RequireApproval: true,
ShowOnDirectory: true,
AllowDecoration: false,
TaxExempt: true,
},
}
for _, house := range testPlayerHouses {
_, err := dhm.AddPlayerHouse(ctx, house)
if err != nil {
t.Fatalf("Failed to insert test player house: %v", err)
}
}
}
func TestNewDatabaseHousingManager(t *testing.T) {
pool := createTestPool(t)
dhm := NewDatabaseHousingManager(pool)
if dhm == nil {
t.Fatal("NewDatabaseHousingManager returned nil")
}
if dhm.pool != pool {
t.Error("Database pool not set correctly")
}
}
func TestEnsureHousingTables(t *testing.T) {
dhm := setupTestDB(t)
ctx := context.Background()
// Test that tables were created (this should not error on second call)
if err := dhm.EnsureHousingTables(ctx); err != nil {
t.Errorf("EnsureHousingTables failed on second call: %v", err)
}
}
func TestHouseZoneOperations(t *testing.T) {
dhm := setupTestDB(t)
ctx := context.Background()
// Test SaveHouseZone and LoadHouseZone
testZone := &HouseZone{
ID: 100,
Name: "Test House",
ZoneID: 200,
CostCoin: 100000,
CostStatus: 2000,
UpkeepCoin: 10000,
UpkeepStatus: 200,
Alignment: -1, // Evil
GuildLevel: 10,
VaultSlots: 6,
MaxItems: 250,
MaxVisitors: 25,
UpkeepPeriod: 1209600, // 2 weeks
Description: "A test house for unit testing",
}
// Save house zone
if err := dhm.SaveHouseZone(ctx, testZone); err != nil {
t.Fatalf("SaveHouseZone failed: %v", err)
}
// Load house zone
loadedZone, err := dhm.LoadHouseZone(ctx, testZone.ID)
if err != nil {
t.Fatalf("LoadHouseZone failed: %v", err)
}
// Verify loaded data
if loadedZone.ID != testZone.ID {
t.Errorf("Expected ID %d, got %d", testZone.ID, loadedZone.ID)
}
if loadedZone.Name != testZone.Name {
t.Errorf("Expected Name %s, got %s", testZone.Name, loadedZone.Name)
}
if loadedZone.Description != testZone.Description {
t.Errorf("Expected Description %s, got %s", testZone.Description, loadedZone.Description)
}
// Test LoadHouseZones
zones, err := dhm.LoadHouseZones(ctx)
if err != nil {
t.Fatalf("LoadHouseZones failed: %v", err)
}
if len(zones) != 1 {
t.Errorf("Expected 1 zone, got %d", len(zones))
}
// Test DeleteHouseZone
if err := dhm.DeleteHouseZone(ctx, testZone.ID); err != nil {
t.Fatalf("DeleteHouseZone failed: %v", err)
}
// Verify deletion
_, err = dhm.LoadHouseZone(ctx, testZone.ID)
if err == nil {
t.Error("Expected error when loading deleted house zone, got nil")
}
}
func TestPlayerHouseOperations(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Test LoadPlayerHouses
houses, err := dhm.LoadPlayerHouses(ctx, 1001)
if err != nil {
t.Fatalf("LoadPlayerHouses failed: %v", err)
}
if len(houses) != 1 {
t.Errorf("Expected 1 house for character 1001, got %d", len(houses))
}
if houses[0].HouseName != "Alice's Studio" {
t.Errorf("Expected house name 'Alice's Studio', got %s", houses[0].HouseName)
}
// Test LoadPlayerHouse by unique ID
house, err := dhm.LoadPlayerHouse(ctx, houses[0].UniqueID)
if err != nil {
t.Fatalf("LoadPlayerHouse failed: %v", err)
}
if house.CharacterID != 1001 {
t.Errorf("Expected character ID 1001, got %d", house.CharacterID)
}
// Test GetHouseByInstance
houseByInstance, err := dhm.GetHouseByInstance(ctx, 5001)
if err != nil {
t.Fatalf("GetHouseByInstance failed: %v", err)
}
if houseByInstance.CharacterID != 1001 {
t.Errorf("Expected character ID 1001, got %d", houseByInstance.CharacterID)
}
// Test GetNextHouseID
nextID, err := dhm.GetNextHouseID(ctx)
if err != nil {
t.Fatalf("GetNextHouseID failed: %v", err)
}
if nextID <= houses[0].UniqueID {
t.Errorf("Expected next ID > %d, got %d", houses[0].UniqueID, nextID)
}
}
func TestPlayerHouseUpdates(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Get a test house
houses, err := dhm.LoadPlayerHouses(ctx, 1001)
if err != nil || len(houses) == 0 {
t.Fatalf("Failed to load test house: %v", err)
}
houseID := houses[0].UniqueID
// Test UpdateHouseUpkeepDue
newUpkeepDue := time.Now().Add(72 * time.Hour)
if err := dhm.UpdateHouseUpkeepDue(ctx, houseID, newUpkeepDue); err != nil {
t.Fatalf("UpdateHouseUpkeepDue failed: %v", err)
}
// Verify update
updatedHouse, err := dhm.LoadPlayerHouse(ctx, houseID)
if err != nil {
t.Fatalf("Failed to load updated house: %v", err)
}
if updatedHouse.UpkeepDue.Unix() != newUpkeepDue.Unix() {
t.Errorf("Expected upkeep due %v, got %v", newUpkeepDue, updatedHouse.UpkeepDue)
}
// Test UpdateHouseEscrow
if err := dhm.UpdateHouseEscrow(ctx, houseID, 50000, 1000); err != nil {
t.Fatalf("UpdateHouseEscrow failed: %v", err)
}
// Verify escrow update
updatedHouse, err = dhm.LoadPlayerHouse(ctx, houseID)
if err != nil {
t.Fatalf("Failed to load updated house: %v", err)
}
if updatedHouse.EscrowCoins != 50000 {
t.Errorf("Expected escrow coins 50000, got %d", updatedHouse.EscrowCoins)
}
if updatedHouse.EscrowStatus != 1000 {
t.Errorf("Expected escrow status 1000, got %d", updatedHouse.EscrowStatus)
}
}
func TestHouseDeposits(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Get a test house
houses, err := dhm.LoadPlayerHouses(ctx, 1001)
if err != nil || len(houses) == 0 {
t.Fatalf("Failed to load test house: %v", err)
}
houseID := houses[0].UniqueID
// Test SaveDeposit
testDeposit := HouseDeposit{
Timestamp: time.Now(),
Amount: 10000,
LastAmount: 15000,
Status: 200,
LastStatus: 300,
Name: "Alice",
CharacterID: 1001,
}
if err := dhm.SaveDeposit(ctx, houseID, testDeposit); err != nil {
t.Fatalf("SaveDeposit failed: %v", err)
}
// Test LoadDeposits
deposits, err := dhm.LoadDeposits(ctx, houseID)
if err != nil {
t.Fatalf("LoadDeposits failed: %v", err)
}
if len(deposits) != 1 {
t.Errorf("Expected 1 deposit, got %d", len(deposits))
}
if deposits[0].Amount != testDeposit.Amount {
t.Errorf("Expected deposit amount %d, got %d", testDeposit.Amount, deposits[0].Amount)
}
if deposits[0].Name != testDeposit.Name {
t.Errorf("Expected deposit name %s, got %s", testDeposit.Name, deposits[0].Name)
}
}
func TestHouseHistory(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Get a test house
houses, err := dhm.LoadPlayerHouses(ctx, 1001)
if err != nil || len(houses) == 0 {
t.Fatalf("Failed to load test house: %v", err)
}
houseID := houses[0].UniqueID
// Test AddHistory
testHistory := HouseHistory{
Timestamp: time.Now(),
Amount: 5000,
Status: 100,
Reason: "Weekly upkeep",
Name: "System",
CharacterID: 0, // System transaction
PosFlag: 0, // Withdrawal
Type: 1, // Upkeep
}
if err := dhm.AddHistory(ctx, houseID, testHistory); err != nil {
t.Fatalf("AddHistory failed: %v", err)
}
// Test LoadHistory
history, err := dhm.LoadHistory(ctx, houseID)
if err != nil {
t.Fatalf("LoadHistory failed: %v", err)
}
if len(history) != 1 {
t.Errorf("Expected 1 history entry, got %d", len(history))
}
if history[0].Reason != testHistory.Reason {
t.Errorf("Expected history reason %s, got %s", testHistory.Reason, history[0].Reason)
}
if history[0].Amount != testHistory.Amount {
t.Errorf("Expected history amount %d, got %d", testHistory.Amount, history[0].Amount)
}
}
func TestHouseAccess(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Get a test house
houses, err := dhm.LoadPlayerHouses(ctx, 1001)
if err != nil || len(houses) == 0 {
t.Fatalf("Failed to load test house: %v", err)
}
houseID := houses[0].UniqueID
// Test SaveHouseAccess
testAccess := []HouseAccess{
{
CharacterID: 2001,
PlayerName: "Bob",
AccessLevel: 1,
Permissions: 15, // Full permissions
GrantedBy: 1001,
GrantedDate: time.Now(),
ExpiresDate: time.Now().Add(30 * 24 * time.Hour),
Notes: "Trusted friend",
},
{
CharacterID: 2002,
PlayerName: "Charlie",
AccessLevel: 2,
Permissions: 7, // Limited permissions
GrantedBy: 1001,
GrantedDate: time.Now(),
ExpiresDate: time.Now().Add(7 * 24 * time.Hour),
Notes: "Temporary access",
},
}
if err := dhm.SaveHouseAccess(ctx, houseID, testAccess); err != nil {
t.Fatalf("SaveHouseAccess failed: %v", err)
}
// Test LoadHouseAccess
accessList, err := dhm.LoadHouseAccess(ctx, houseID)
if err != nil {
t.Fatalf("LoadHouseAccess failed: %v", err)
}
if len(accessList) != 2 {
t.Errorf("Expected 2 access entries, got %d", len(accessList))
}
// Test DeleteHouseAccess
if err := dhm.DeleteHouseAccess(ctx, houseID, 2002); err != nil {
t.Fatalf("DeleteHouseAccess failed: %v", err)
}
// Verify deletion
accessList, err = dhm.LoadHouseAccess(ctx, houseID)
if err != nil {
t.Fatalf("LoadHouseAccess after deletion failed: %v", err)
}
if len(accessList) != 1 {
t.Errorf("Expected 1 access entry after deletion, got %d", len(accessList))
}
if accessList[0].CharacterID != 2001 {
t.Errorf("Expected remaining access for character 2001, got %d", accessList[0].CharacterID)
}
}
func TestHouseAmenities(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Get a test house
houses, err := dhm.LoadPlayerHouses(ctx, 1001)
if err != nil || len(houses) == 0 {
t.Fatalf("Failed to load test house: %v", err)
}
houseID := houses[0].UniqueID
// Test SaveHouseAmenity
testAmenity := HouseAmenity{
ID: 1,
Type: 1, // Furniture
Name: "Comfortable Chair",
Cost: 1000,
StatusCost: 20,
PurchaseDate: time.Now(),
X: 100.5,
Y: 200.0,
Z: 50.25,
Heading: 180.0,
IsActive: true,
}
if err := dhm.SaveHouseAmenity(ctx, houseID, testAmenity); err != nil {
t.Fatalf("SaveHouseAmenity failed: %v", err)
}
// Test LoadHouseAmenities
amenities, err := dhm.LoadHouseAmenities(ctx, houseID)
if err != nil {
t.Fatalf("LoadHouseAmenities failed: %v", err)
}
if len(amenities) != 1 {
t.Errorf("Expected 1 amenity, got %d", len(amenities))
}
if amenities[0].Name != testAmenity.Name {
t.Errorf("Expected amenity name %s, got %s", testAmenity.Name, amenities[0].Name)
}
if amenities[0].X != testAmenity.X {
t.Errorf("Expected X position %f, got %f", testAmenity.X, amenities[0].X)
}
// Test DeleteHouseAmenity
if err := dhm.DeleteHouseAmenity(ctx, houseID, testAmenity.ID); err != nil {
t.Fatalf("DeleteHouseAmenity failed: %v", err)
}
// Verify deletion
amenities, err = dhm.LoadHouseAmenities(ctx, houseID)
if err != nil {
t.Fatalf("LoadHouseAmenities after deletion failed: %v", err)
}
if len(amenities) != 0 {
t.Errorf("Expected 0 amenities after deletion, got %d", len(amenities))
}
}
func TestHouseItems(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Get a test house
houses, err := dhm.LoadPlayerHouses(ctx, 1001)
if err != nil || len(houses) == 0 {
t.Fatalf("Failed to load test house: %v", err)
}
houseID := houses[0].UniqueID
// Test SaveHouseItem
testItem := HouseItem{
ID: 1,
ItemID: 12345,
CharacterID: 1001,
X: 150.0,
Y: 250.0,
Z: 75.5,
Heading: 90.0,
PitchX: 5.0,
PitchY: 0.0,
RollX: 0.0,
RollY: 2.5,
PlacedDate: time.Now(),
Quantity: 1,
Condition: 100,
House: "main",
}
if err := dhm.SaveHouseItem(ctx, houseID, testItem); err != nil {
t.Fatalf("SaveHouseItem failed: %v", err)
}
// Test LoadHouseItems
items, err := dhm.LoadHouseItems(ctx, houseID)
if err != nil {
t.Fatalf("LoadHouseItems failed: %v", err)
}
if len(items) != 1 {
t.Errorf("Expected 1 item, got %d", len(items))
}
if items[0].ItemID != testItem.ItemID {
t.Errorf("Expected item ID %d, got %d", testItem.ItemID, items[0].ItemID)
}
if items[0].House != testItem.House {
t.Errorf("Expected house %s, got %s", testItem.House, items[0].House)
}
// Test DeleteHouseItem
if err := dhm.DeleteHouseItem(ctx, houseID, testItem.ID); err != nil {
t.Fatalf("DeleteHouseItem failed: %v", err)
}
// Verify deletion
items, err = dhm.LoadHouseItems(ctx, houseID)
if err != nil {
t.Fatalf("LoadHouseItems after deletion failed: %v", err)
}
if len(items) != 0 {
t.Errorf("Expected 0 items after deletion, got %d", len(items))
}
}
func TestGetHousesForUpkeep(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Test with cutoff time in the future (should find houses)
cutoffTime := time.Now().Add(72 * time.Hour)
houses, err := dhm.GetHousesForUpkeep(ctx, cutoffTime)
if err != nil {
t.Fatalf("GetHousesForUpkeep failed: %v", err)
}
if len(houses) != 2 {
t.Errorf("Expected 2 houses for upkeep, got %d", len(houses))
}
// Test with cutoff time in the past (should find no houses)
cutoffTime = time.Now().Add(-24 * time.Hour)
houses, err = dhm.GetHousesForUpkeep(ctx, cutoffTime)
if err != nil {
t.Fatalf("GetHousesForUpkeep with past cutoff failed: %v", err)
}
if len(houses) != 0 {
t.Errorf("Expected 0 houses for past upkeep, got %d", len(houses))
}
}
func TestGetHouseStatistics(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Add some test data for statistics
houses, err := dhm.LoadPlayerHouses(ctx, 1001)
if err != nil || len(houses) == 0 {
t.Fatalf("Failed to load test house: %v", err)
}
houseID := houses[0].UniqueID
// Add some deposits and history for stats
testDeposit := HouseDeposit{
Timestamp: time.Now(),
Amount: 5000,
LastAmount: 10000,
Status: 100,
LastStatus: 200,
Name: "Alice",
CharacterID: 1001,
}
if err := dhm.SaveDeposit(ctx, houseID, testDeposit); err != nil {
t.Fatalf("Failed to save test deposit: %v", err)
}
testHistory := HouseHistory{
Timestamp: time.Now(),
Amount: 2000,
Status: 50,
Reason: "Withdrawal",
Name: "Alice",
CharacterID: 1001,
PosFlag: 0, // Withdrawal
Type: 2,
}
if err := dhm.AddHistory(ctx, houseID, testHistory); err != nil {
t.Fatalf("Failed to add test history: %v", err)
}
// Test GetHouseStatistics
stats, err := dhm.GetHouseStatistics(ctx)
if err != nil {
t.Fatalf("GetHouseStatistics failed: %v", err)
}
if stats.TotalHouses != 2 {
t.Errorf("Expected 2 total houses, got %d", stats.TotalHouses)
}
if stats.ActiveHouses != 2 {
t.Errorf("Expected 2 active houses, got %d", stats.ActiveHouses)
}
if stats.TotalDeposits != 1 {
t.Errorf("Expected 1 total deposits, got %d", stats.TotalDeposits)
}
if stats.TotalWithdrawals != 1 {
t.Errorf("Expected 1 total withdrawals, got %d", stats.TotalWithdrawals)
}
}
func TestDeletePlayerHouse(t *testing.T) {
dhm := setupTestDB(t)
insertTestData(t, dhm)
ctx := context.Background()
// Get a test house
houses, err := dhm.LoadPlayerHouses(ctx, 1001)
if err != nil || len(houses) == 0 {
t.Fatalf("Failed to load test house: %v", err)
}
houseID := houses[0].UniqueID
// Add some related data that should be cascade deleted
testDeposit := HouseDeposit{
Timestamp: time.Now(),
Amount: 5000,
LastAmount: 10000,
Status: 100,
LastStatus: 200,
Name: "Alice",
CharacterID: 1001,
}
if err := dhm.SaveDeposit(ctx, houseID, testDeposit); err != nil {
t.Fatalf("Failed to save test deposit: %v", err)
}
// Test DeletePlayerHouse
if err := dhm.DeletePlayerHouse(ctx, houseID); err != nil {
t.Fatalf("DeletePlayerHouse failed: %v", err)
}
// Verify deletion
_, err = dhm.LoadPlayerHouse(ctx, houseID)
if err == nil {
t.Error("Expected error when loading deleted player house, got nil")
}
// Verify related data was also deleted
deposits, err := dhm.LoadDeposits(ctx, houseID)
if err != nil {
t.Fatalf("LoadDeposits after house deletion failed: %v", err)
}
if len(deposits) != 0 {
t.Errorf("Expected 0 deposits after house deletion, got %d", len(deposits))
}
// Verify other houses are still there
remainingHouses, err := dhm.LoadPlayerHouses(ctx, 1002)
if err != nil {
t.Fatalf("Failed to load remaining houses: %v", err)
}
if len(remainingHouses) != 1 {
t.Errorf("Expected 1 remaining house, got %d", len(remainingHouses))
}
}
func TestErrorCases(t *testing.T) {
dhm := setupTestDB(t)
ctx := context.Background()
// Test loading non-existent house zone
_, err := dhm.LoadHouseZone(ctx, 999)
if err == nil {
t.Error("Expected error when loading non-existent house zone, got nil")
}
// Test loading non-existent player house
_, err = dhm.LoadPlayerHouse(ctx, 999)
if err == nil {
t.Error("Expected error when loading non-existent player house, got nil")
}
// Test loading house by non-existent instance
_, err = dhm.GetHouseByInstance(ctx, 999)
if err == nil {
t.Error("Expected error when loading house by non-existent instance, got nil")
}
// Test operations on non-existent house
nonExistentHouseID := int64(999)
deposits, err := dhm.LoadDeposits(ctx, nonExistentHouseID)
if err != nil {
t.Errorf("LoadDeposits should not error on non-existent house: %v", err)
}
if len(deposits) != 0 {
t.Errorf("Expected 0 deposits for non-existent house, got %d", len(deposits))
}
history, err := dhm.LoadHistory(ctx, nonExistentHouseID)
if err != nil {
t.Errorf("LoadHistory should not error on non-existent house: %v", err)
}
if len(history) != 0 {
t.Errorf("Expected 0 history entries for non-existent house, got %d", len(history))
}
}
// Helper function to compare times with tolerance for database precision
func timesEqual(t1, t2 time.Time, tolerance time.Duration) bool {
diff := t1.Sub(t2)
if diff < 0 {
diff = -diff
}
return diff <= tolerance
}