1
0
mirror of https://github.com/garraflavatra/go-fmp.git synced 2025-06-28 04:25:11 +00:00

Record parsing

This commit is contained in:
2025-06-22 19:14:22 +02:00
parent d22b209ca5
commit 7cf3fd93ac
6 changed files with 232 additions and 168 deletions

View File

@ -1,6 +1,9 @@
package fmp
import "testing"
import (
"slices"
"testing"
)
func TestOpenFile(t *testing.T) {
f, err := OpenFile("../files/Untitled.fmp12")
@ -27,50 +30,80 @@ func TestTables(t *testing.T) {
if err != nil {
t.Fatal(err)
}
tables := f.Tables()
expectedNames := []string{"Untitled"}
tableNames := []string{}
for _, table := range tables {
for _, table := range f.tables {
tableNames = append(tableNames, table.Name)
}
if !slicesHaveSameElements(tableNames, expectedNames) {
t.Errorf("tables do not match")
}
var field FmpColumn
for _, table := range tables {
for _, column := range table.Columns {
if column.Name == "PrimaryKey" {
field = column
break
}
}
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])
}
if field.Type != FmpFieldSimple {
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 {
t.Errorf("expected field type to be simple, but it is not")
}
if field.DataType != FmpDataText {
if col.DataType != FmpDataText {
t.Errorf("expected field data type to be text, but it is not")
}
if field.StorageType != FmpFieldStorageRegular {
if col.StorageType != FmpFieldStorageRegular {
t.Errorf("expected field storage type to be regular, but it is not")
}
if field.Repetitions != 1 {
t.Errorf("expected field repetition count to be 1, but it is %d", field.Repetitions)
if col.Repetitions != 1 {
t.Errorf("expected field repetition count to be 1, but it is %d", col.Repetitions)
}
if !field.Indexed {
if !col.Indexed {
t.Errorf("expected field to be indexed, but it is not")
}
if field.AutoEnter != FmpAutoEnterCalculationReplacingExistingValue {
if col.AutoEnter != FmpAutoEnterCalculationReplacingExistingValue {
t.Errorf("expected field to have auto enter calculation replacing existing value, but it does not")
}
if len(tables[0].Records) != 3 {
t.Errorf("expected table to have 3 records, but it has %d", len(tables[0].Records))
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 tables[0].Records[1].Values[1] != "629FAA83-50D8-401F-A560-C8D45217D17B" {
t.Errorf("first record has an incorrect ID '%s'", tables[0].Records[0].Values[0])
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"))
}
}
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
}
}
return true
}