2025-06-11 16:01:49 +02:00
|
|
|
package fmp
|
|
|
|
|
2025-06-22 19:14:22 +02:00
|
|
|
import (
|
|
|
|
"slices"
|
|
|
|
"testing"
|
|
|
|
)
|
2025-06-11 16:01:49 +02:00
|
|
|
|
|
|
|
func TestOpenFile(t *testing.T) {
|
|
|
|
f, err := OpenFile("../files/Untitled.fmp12")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2025-06-22 20:08:53 +02:00
|
|
|
defer f.Close()
|
|
|
|
|
2025-06-22 18:43:06 +02:00
|
|
|
if f.FileSize != 229376 {
|
2025-06-22 20:08:53 +02:00
|
|
|
t.Errorf("expected file size to be 229376, got %d", f.FileSize)
|
2025-06-11 16:19:31 +02:00
|
|
|
}
|
2025-06-22 18:43:06 +02:00
|
|
|
if f.numSectors != 55 {
|
|
|
|
t.Errorf("expected number of sectors to be 55, got %d", f.numSectors)
|
2025-06-11 16:19:31 +02:00
|
|
|
}
|
2025-06-11 21:52:52 +02:00
|
|
|
if f.CreatorName != "Pro 12.0" {
|
|
|
|
t.Errorf("expected application name to be 'Pro 12.0', got '%s'", f.CreatorName)
|
2025-06-11 16:01:49 +02:00
|
|
|
}
|
|
|
|
if f.VersionDate.Format("2006-01-02") != "2025-01-11" {
|
|
|
|
t.Errorf("expected version date to be '2025-01-11', got '%s'", f.VersionDate.Format("2006-01-02"))
|
|
|
|
}
|
2025-06-22 20:08:53 +02:00
|
|
|
|
2025-06-12 10:53:31 +02:00
|
|
|
f.ToDebugFile("../private/output")
|
2025-06-11 16:01:49 +02:00
|
|
|
}
|
2025-06-12 11:44:57 +02:00
|
|
|
|
|
|
|
func TestTables(t *testing.T) {
|
|
|
|
f, err := OpenFile("../files/Untitled.fmp12")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2025-06-22 20:08:53 +02:00
|
|
|
defer f.Close()
|
2025-06-12 11:44:57 +02:00
|
|
|
|
2025-06-22 18:43:06 +02:00
|
|
|
expectedNames := []string{"Untitled"}
|
2025-06-14 15:30:42 +02:00
|
|
|
tableNames := []string{}
|
2025-06-22 19:14:22 +02:00
|
|
|
for _, table := range f.tables {
|
2025-06-14 15:30:42 +02:00
|
|
|
tableNames = append(tableNames, table.Name)
|
2025-06-14 15:19:36 +02:00
|
|
|
}
|
2025-06-14 15:30:42 +02:00
|
|
|
if !slicesHaveSameElements(tableNames, expectedNames) {
|
|
|
|
t.Errorf("tables do not match")
|
2025-06-12 11:44:57 +02:00
|
|
|
}
|
2025-06-15 21:09:58 +02:00
|
|
|
|
2025-06-22 19:14:22 +02:00
|
|
|
table := f.Table("Untitled")
|
|
|
|
if table == nil {
|
|
|
|
t.Errorf("expected table to exist, but it does not")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if table.Name != "Untitled" {
|
|
|
|
t.Errorf("expected table name to be 'Untitled', but it is '%s'", table.Name)
|
|
|
|
}
|
|
|
|
if len(table.Records) != 3 {
|
|
|
|
t.Errorf("expected table to have 3 records, but it has %d", len(table.Records))
|
|
|
|
}
|
|
|
|
if table.Records[1].Values[1] != "629FAA83-50D8-401F-A560-C8D45217D17B" {
|
|
|
|
t.Errorf("first record has an incorrect ID '%s'", table.Records[0].Values[0])
|
2025-06-15 21:09:58 +02:00
|
|
|
}
|
|
|
|
|
2025-06-22 19:14:22 +02:00
|
|
|
col := table.Column("PrimaryKey")
|
|
|
|
if col == nil {
|
|
|
|
t.Errorf("expected column to exist, but it does not")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if col.Name != "PrimaryKey" {
|
|
|
|
t.Errorf("expected column name to be 'PrimaryKey', but it is '%s'", col.Name)
|
|
|
|
}
|
|
|
|
if col.Type != FmpFieldSimple {
|
2025-06-15 21:09:58 +02:00
|
|
|
t.Errorf("expected field type to be simple, but it is not")
|
|
|
|
}
|
2025-06-22 19:14:22 +02:00
|
|
|
if col.DataType != FmpDataText {
|
2025-06-15 21:09:58 +02:00
|
|
|
t.Errorf("expected field data type to be text, but it is not")
|
|
|
|
}
|
2025-06-22 19:14:22 +02:00
|
|
|
if col.StorageType != FmpFieldStorageRegular {
|
2025-06-15 21:09:58 +02:00
|
|
|
t.Errorf("expected field storage type to be regular, but it is not")
|
|
|
|
}
|
2025-06-22 19:14:22 +02:00
|
|
|
if col.Repetitions != 1 {
|
|
|
|
t.Errorf("expected field repetition count to be 1, but it is %d", col.Repetitions)
|
2025-06-16 11:29:48 +02:00
|
|
|
}
|
2025-06-22 19:14:22 +02:00
|
|
|
if !col.Indexed {
|
2025-06-15 21:09:58 +02:00
|
|
|
t.Errorf("expected field to be indexed, but it is not")
|
|
|
|
}
|
2025-06-22 19:14:22 +02:00
|
|
|
if col.AutoEnter != FmpAutoEnterCalculationReplacingExistingValue {
|
2025-06-15 21:09:58 +02:00
|
|
|
t.Errorf("expected field to have auto enter calculation replacing existing value, but it does not")
|
|
|
|
}
|
2025-06-22 19:14:22 +02:00
|
|
|
|
|
|
|
newRecord, err := table.NewRecord(map[string]string{"PrimaryKey": "629FAA83-50D8-401F-A560-C8D45217D17B"})
|
|
|
|
if newRecord == nil || err != nil {
|
|
|
|
t.Errorf("expected new record to be created, but it is nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if newRecord.Index != 4 {
|
|
|
|
t.Errorf("expected new record index to be 4, but it is %d", newRecord.Index)
|
|
|
|
}
|
|
|
|
if newRecord.Value("PrimaryKey") != "629FAA83-50D8-401F-A560-C8D45217D17B" {
|
|
|
|
t.Errorf("expected new record primary key to be '629FAA83-50D8-401F-A560-C8D45217D17B', but it is '%s'", newRecord.Value("PrimaryKey"))
|
2025-06-22 18:43:06 +02:00
|
|
|
}
|
2025-06-22 19:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func slicesHaveSameElements[Type comparable](a, b []Type) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, av := range a {
|
|
|
|
found := slices.Contains(b, av)
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
2025-06-22 18:43:06 +02:00
|
|
|
}
|
2025-06-22 19:14:22 +02:00
|
|
|
return true
|
2025-06-12 11:44:57 +02:00
|
|
|
}
|