273 lines
5.8 KiB
Go
273 lines
5.8 KiB
Go
package items
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"dk/internal/database"
|
|
)
|
|
|
|
func setupTestDB(t *testing.T) *database.DB {
|
|
testDB := "test_items.db"
|
|
t.Cleanup(func() {
|
|
os.Remove(testDB)
|
|
})
|
|
|
|
db, err := database.Open(testDB)
|
|
if err != nil {
|
|
t.Fatalf("Failed to open test database: %v", err)
|
|
}
|
|
|
|
createTable := `CREATE TABLE items (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
type INTEGER NOT NULL DEFAULT 0,
|
|
name TEXT NOT NULL,
|
|
value INTEGER NOT NULL DEFAULT 0,
|
|
att INTEGER NOT NULL DEFAULT 0,
|
|
special TEXT NOT NULL DEFAULT ''
|
|
)`
|
|
|
|
if err := db.Exec(createTable); err != nil {
|
|
t.Fatalf("Failed to create items table: %v", err)
|
|
}
|
|
|
|
testItems := `INSERT INTO items (type, name, value, att, special) VALUES
|
|
(1, 'Test Sword', 100, 10, 'strength,5'),
|
|
(2, 'Test Armor', 200, 15, 'maxhp,25'),
|
|
(3, 'Test Shield', 150, 8, '')`
|
|
|
|
if err := db.Exec(testItems); err != nil {
|
|
t.Fatalf("Failed to insert test items: %v", err)
|
|
}
|
|
|
|
return db
|
|
}
|
|
|
|
func TestFind(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
defer db.Close()
|
|
|
|
// Test finding existing item
|
|
item, err := Find(db, 1)
|
|
if err != nil {
|
|
t.Fatalf("Failed to find item: %v", err)
|
|
}
|
|
|
|
if item.ID != 1 {
|
|
t.Errorf("Expected ID 1, got %d", item.ID)
|
|
}
|
|
if item.Name != "Test Sword" {
|
|
t.Errorf("Expected name 'Test Sword', got '%s'", item.Name)
|
|
}
|
|
if item.Type != TypeWeapon {
|
|
t.Errorf("Expected type %d, got %d", TypeWeapon, item.Type)
|
|
}
|
|
if item.Value != 100 {
|
|
t.Errorf("Expected value 100, got %d", item.Value)
|
|
}
|
|
if item.Att != 10 {
|
|
t.Errorf("Expected att 10, got %d", item.Att)
|
|
}
|
|
if item.Special != "strength,5" {
|
|
t.Errorf("Expected special 'strength,5', got '%s'", item.Special)
|
|
}
|
|
|
|
// Test finding non-existent item
|
|
_, err = Find(db, 999)
|
|
if err == nil {
|
|
t.Error("Expected error when finding non-existent item")
|
|
}
|
|
}
|
|
|
|
func TestAll(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
defer db.Close()
|
|
|
|
items, err := All(db)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get all items: %v", err)
|
|
}
|
|
|
|
if len(items) != 3 {
|
|
t.Errorf("Expected 3 items, got %d", len(items))
|
|
}
|
|
|
|
// Check first item
|
|
if items[0].Name != "Test Sword" {
|
|
t.Errorf("Expected first item to be 'Test Sword', got '%s'", items[0].Name)
|
|
}
|
|
}
|
|
|
|
func TestByType(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
defer db.Close()
|
|
|
|
weapons, err := ByType(db, TypeWeapon)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get weapons: %v", err)
|
|
}
|
|
|
|
if len(weapons) != 1 {
|
|
t.Errorf("Expected 1 weapon, got %d", len(weapons))
|
|
}
|
|
|
|
if weapons[0].Name != "Test Sword" {
|
|
t.Errorf("Expected weapon to be 'Test Sword', got '%s'", weapons[0].Name)
|
|
}
|
|
|
|
armor, err := ByType(db, TypeArmor)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get armor: %v", err)
|
|
}
|
|
|
|
if len(armor) != 1 {
|
|
t.Errorf("Expected 1 armor, got %d", len(armor))
|
|
}
|
|
}
|
|
|
|
func TestBuilder(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
defer db.Close()
|
|
|
|
// Create new item using builder
|
|
item, err := NewBuilder(db).
|
|
WithType(TypeWeapon).
|
|
WithName("Builder Sword").
|
|
WithValue(500).
|
|
WithAtt(25).
|
|
WithSpecial("dexterity,10").
|
|
Create()
|
|
|
|
if err != nil {
|
|
t.Fatalf("Failed to create item with builder: %v", err)
|
|
}
|
|
|
|
if item.ID == 0 {
|
|
t.Error("Expected non-zero ID after creation")
|
|
}
|
|
if item.Name != "Builder Sword" {
|
|
t.Errorf("Expected name 'Builder Sword', got '%s'", item.Name)
|
|
}
|
|
if item.Type != TypeWeapon {
|
|
t.Errorf("Expected type %d, got %d", TypeWeapon, item.Type)
|
|
}
|
|
if item.Value != 500 {
|
|
t.Errorf("Expected value 500, got %d", item.Value)
|
|
}
|
|
if item.Att != 25 {
|
|
t.Errorf("Expected att 25, got %d", item.Att)
|
|
}
|
|
if item.Special != "dexterity,10" {
|
|
t.Errorf("Expected special 'dexterity,10', got '%s'", item.Special)
|
|
}
|
|
|
|
// Verify it was saved to database
|
|
foundItem, err := Find(db, item.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to find created item: %v", err)
|
|
}
|
|
|
|
if foundItem.Name != "Builder Sword" {
|
|
t.Errorf("Created item not found in database")
|
|
}
|
|
}
|
|
|
|
func TestSave(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
defer db.Close()
|
|
|
|
item, err := Find(db, 1)
|
|
if err != nil {
|
|
t.Fatalf("Failed to find item: %v", err)
|
|
}
|
|
|
|
// Modify item
|
|
item.Name = "Updated Sword"
|
|
item.Value = 150
|
|
|
|
// Save changes
|
|
err = item.Save()
|
|
if err != nil {
|
|
t.Fatalf("Failed to save item: %v", err)
|
|
}
|
|
|
|
// Verify changes were saved
|
|
updatedItem, err := Find(db, 1)
|
|
if err != nil {
|
|
t.Fatalf("Failed to find updated item: %v", err)
|
|
}
|
|
|
|
if updatedItem.Name != "Updated Sword" {
|
|
t.Errorf("Expected updated name 'Updated Sword', got '%s'", updatedItem.Name)
|
|
}
|
|
if updatedItem.Value != 150 {
|
|
t.Errorf("Expected updated value 150, got %d", updatedItem.Value)
|
|
}
|
|
}
|
|
|
|
func TestDelete(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
defer db.Close()
|
|
|
|
item, err := Find(db, 1)
|
|
if err != nil {
|
|
t.Fatalf("Failed to find item: %v", err)
|
|
}
|
|
|
|
// Delete item
|
|
err = item.Delete()
|
|
if err != nil {
|
|
t.Fatalf("Failed to delete item: %v", err)
|
|
}
|
|
|
|
// Verify item was deleted
|
|
_, err = Find(db, 1)
|
|
if err == nil {
|
|
t.Error("Expected error when finding deleted item")
|
|
}
|
|
}
|
|
|
|
func TestItemTypeMethods(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
defer db.Close()
|
|
|
|
sword, _ := Find(db, 1)
|
|
armor, _ := Find(db, 2)
|
|
shield, _ := Find(db, 3)
|
|
|
|
// Test IsWeapon
|
|
if !sword.IsWeapon() {
|
|
t.Error("Expected sword to be weapon")
|
|
}
|
|
if armor.IsWeapon() {
|
|
t.Error("Expected armor not to be weapon")
|
|
}
|
|
|
|
// Test IsArmor
|
|
if !armor.IsArmor() {
|
|
t.Error("Expected armor to be armor")
|
|
}
|
|
if sword.IsArmor() {
|
|
t.Error("Expected sword not to be armor")
|
|
}
|
|
|
|
// Test IsShield
|
|
if !shield.IsShield() {
|
|
t.Error("Expected shield to be shield")
|
|
}
|
|
if sword.IsShield() {
|
|
t.Error("Expected sword not to be shield")
|
|
}
|
|
|
|
// Test TypeName
|
|
if sword.TypeName() != "Weapon" {
|
|
t.Errorf("Expected sword type name 'Weapon', got '%s'", sword.TypeName())
|
|
}
|
|
if armor.TypeName() != "Armor" {
|
|
t.Errorf("Expected armor type name 'Armor', got '%s'", armor.TypeName())
|
|
}
|
|
if shield.TypeName() != "Shield" {
|
|
t.Errorf("Expected shield type name 'Shield', got '%s'", shield.TypeName())
|
|
}
|
|
}
|