frm/frm_test.go

132 lines
3.1 KiB
Go
Raw Normal View History

2025-01-18 16:09:16 +00:00
package frm_test
import (
"context"
2025-02-11 21:54:17 +00:00
"fmt"
2025-01-18 16:09:16 +00:00
"os"
"testing"
"github.com/acaloiaro/frm"
"github.com/acaloiaro/frm/internal"
"github.com/acaloiaro/frm/types"
2025-02-11 21:54:17 +00:00
"github.com/google/uuid"
2025-01-18 16:09:16 +00:00
)
func TestCreateShortCode(t *testing.T) {
ctx := context.Background()
f, err := frm.New(frm.Args{
PostgresURL: os.Getenv("POSTGRES_URL"),
PostgresDisableSSL: true,
WorkspaceID: "1",
WorkspaceIDUrlParam: "client_id",
2025-01-22 15:54:40 +00:00
PostgresSchema: "frm_test",
2025-01-18 16:09:16 +00:00
})
if err != nil {
t.Error(err)
}
draft, err := internal.Q(ctx, internal.DBArgs{
URL: os.Getenv("POSTGRES_URL"),
DisableSSL: true,
Schema: "frm_test",
2025-02-11 21:54:17 +00:00
}).SaveForm(ctx, internal.SaveFormParams{
2025-01-18 16:09:16 +00:00
Name: "hello world",
Fields: types.FormFields{},
WorkspaceID: "1",
})
if err != nil {
t.Error(err)
return
}
sc, err := f.CreateShortCode(ctx, frm.CreateShortCodeArgs{
FormID: draft.ID,
SubjectID: "foobar_idx",
})
if err != nil {
t.Error(err)
}
if len(sc.ShortCode) != internal.DefaultShortcodeLen {
t.Fatal("shortcode length is incorrect")
}
sc2, err := f.CreateShortCode(ctx, frm.CreateShortCodeArgs{
FormID: draft.ID,
SubjectID: "foobar_idx",
})
if err != nil {
t.Error(err)
}
if sc2.ShortCode != sc.ShortCode {
t.Error("successive SaveShortCode calls should create same short code")
}
2025-01-18 16:09:16 +00:00
}
2025-02-11 21:54:17 +00:00
func TestCopyForm(t *testing.T) {
copiedFormNameSuffix := "(COPY)"
ctx := context.Background()
f, err := frm.New(frm.Args{
PostgresURL: os.Getenv("POSTGRES_URL"),
PostgresDisableSSL: true,
WorkspaceID: "1",
WorkspaceIDUrlParam: "client_id",
PostgresSchema: "frm_test",
})
if err != nil {
t.Error(err)
}
draft, err := internal.Q(ctx, internal.DBArgs{
URL: os.Getenv("POSTGRES_URL"),
DisableSSL: true,
Schema: "frm_test",
}).SaveForm(ctx, internal.SaveFormParams{
Name: "hello world",
Fields: types.FormFields{
uuid.New().String(): {
Label: "What's your name?",
Order: 0,
Required: true,
Type: types.FormFieldTypeTextSingle,
},
},
WorkspaceID: "1",
})
if err != nil {
t.Error(err)
return
}
clonedForm, err := f.CopyForm(ctx, frm.CopyFormArgs{
ID: draft.ID,
NameSuffix: copiedFormNameSuffix,
})
if err != nil {
t.Error(err)
return
}
expectedCopiedFormName := fmt.Sprintf("%s %s", draft.Name, copiedFormNameSuffix)
if clonedForm.Name != expectedCopiedFormName {
t.Error(fmt.Errorf("form name should be '%s'", draft.Name), "got:", clonedForm.Name)
return
}
if len(clonedForm.Fields) != len(draft.Fields) {
t.Error("cloned form should have the same number of fields as the original, but does not.", "actual number of fields:", len(clonedForm.Fields), "expected number of fields:", len(draft.Fields))
return
}
}
func TestNoopWhenPoolUnavailable(t *testing.T) {
ctx := context.Background()
_, err := internal.Q(ctx, internal.DBArgs{
URL: "INVALID URL",
DisableSSL: true,
Schema: "frm_test",
}).GetForm(ctx, internal.GetFormParams{})
if err != internal.ErrNoopDatabase {
t.Error(fmt.Errorf("expected: '%s' but got: '%s'", internal.ErrNoopDatabase, err))
}
}