MCPcopy
hub / github.com/pocketbase/pocketbase / TestFileFieldIntercept

Function TestFileFieldIntercept

core/field_file_test.go:828–963  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

826}
827
828func TestFileFieldIntercept(t *testing.T) {
829 testApp, _ := tests.NewTestApp()
830 defer testApp.Cleanup()
831
832 demo1, err := testApp.FindCollectionByNameOrId("demo1")
833 if err != nil {
834 t.Fatal(err)
835 }
836 demo1.Fields.GetByName("text").(*core.TextField).Required = true // trigger validation error
837
838 f1, err := filesystem.NewFileFromBytes([]byte("test"), "new1.txt")
839 if err != nil {
840 t.Fatal(err)
841 }
842
843 f2, err := filesystem.NewFileFromBytes([]byte("test"), "new2.txt")
844 if err != nil {
845 t.Fatal(err)
846 }
847
848 f3, err := filesystem.NewFileFromBytes([]byte("test"), "new3.txt")
849 if err != nil {
850 t.Fatal(err)
851 }
852
853 f4, err := filesystem.NewFileFromBytes([]byte("test"), "new4.txt")
854 if err != nil {
855 t.Fatal(err)
856 }
857
858 record := core.NewRecord(demo1)
859
860 ok := t.Run("1. create - with validation error", func(t *testing.T) {
861 record.Set("file_many", []any{f1, f2})
862
863 err := testApp.Save(record)
864
865 tests.TestValidationErrors(t, err, []string{"text"})
866
867 value, _ := record.GetRaw("file_many").([]any)
868 if len(value) != 2 {
869 t.Fatalf("Expected the file field value to be unchanged, got %v", value)
870 }
871 })
872 if !ok {
873 return
874 }
875
876 ok = t.Run("2. create - fixing the validation error", func(t *testing.T) {
877 record.Set("text", "abc")
878
879 err := testApp.Save(record)
880 if err != nil {
881 t.Fatalf("Expected save to succeed, got %v", err)
882 }
883
884 expectedKeys := []string{f1.Name, f2.Name}
885

Callers

nothing calls this directly

Calls 14

CleanupMethod · 0.95
SetMethod · 0.95
GetRawMethod · 0.95
NewTestAppFunction · 0.92
NewFileFromBytesFunction · 0.92
NewRecordFunction · 0.92
TestValidationErrorsFunction · 0.92
ToUniqueStringSliceFunction · 0.92
checkRecordFilesFunction · 0.85
GetByNameMethod · 0.80
SaveMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…