Skip to content
Snippets Groups Projects
Select Git revision
  • 35a556e8b8b5361a276c6c7d242e74983d4e9973
  • master default protected
  • refactor/PRXS-3053-Files
  • feature/PRXS-3143-3235-ReferenceOptions
  • feature/PRXS-3421-ImplementNewRefAPI
  • feature/PRXS-3143-LimitReferenceFields
  • feature/PRXS-3234-FeaturePruneIdents
  • feature/3149-LocaleCodeAsID-Feature
  • feature/PRXS-3383-CollectionsRankSortAPI
  • PRXS-3421-RecursiveReferences
  • feature/PRXS-3383-CollectionsSort
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

protobuf_endpoint_converters.microgen.go

Blame
  • field_test.go 3.87 KiB
    package field
    
    import (
    	"testing"
    
    	"github.com/stretchr/testify/assert"
    )
    
    func TestField_GetField(t *testing.T) {
    	sch := Object(
    		"f1", Object(
    			"a", String(),
    			"b", String().SetSingleLocale(true),
    		),
    		"f3", Object( // inline object
    			true,
    			"a", String(),
    			"b", Object(true, "c", String()),
    		).SetSingleLocale(true),
    		"f4", Array(Object("a", String())),
    		"f5", Array(String()),
    		"f6", Object(true, "f6", Object("a", String())),
    	)
    
    	sch.EnableState()
    
    	tests := []struct {
    		name string
    		path string
    		want *State
    	}{
    		{"Object", "f1", &State{Name: "f1", DataPath: "f1", SchemaPath: "f1", Parent: sch}},
    		{"Object field", "f1.a", &State{Name: "a", DataPath: "f1.a", SchemaPath: "f1.a", Parent: sch.GetField("f1")}},
    		{"Field with SingleLocale", "f1.b", &State{Name: "b", DataPath: "f1.b", SchemaPath: "f1.b", Parent: sch.GetField("f1"), SingleLocale: true}},
    		{"Object with SingleLocale", "f3", &State{Name: "f3", DataPath: "f3", SchemaPath: "f3", Parent: sch, SingleLocale: true}},
    		{"Inline", "a", &State{Name: "a", DataPath: "a", SchemaPath: "f3.a", Parent: sch.GetField("f3"), SingleLocale: true, Inlined: true, HasInline: true}},
    		{"Inline of inline", "c", &State{Name: "c", DataPath: "c", SchemaPath: "f3.b.c", Parent: sch.GetField("f3.b"), SingleLocale: true, Inlined: true, HasInline: true}},
    		{"Inline of inline (direct)", "f3.b.c", &State{Name: "c", DataPath: "c", SchemaPath: "f3.b.c", Parent: sch.GetField("f3.b"), SingleLocale: true, Inlined: true, HasInline: true}},
    		{"Array of Objects", "f4", &State{Name: "f4", DataPath: "f4", SchemaPath: "f4", Parent: sch}},
    		{"Array of Objects (Item)", "f4.Item", &State{Name: "Item", DataPath: "f4", SchemaPath: "f4.Item", Parent: sch.GetField("f4")}},
    		{"Array of Objects (Item field)", "f4.Item.a", &State{Name: "a", DataPath: "f4.a", SchemaPath: "f4.Item.a", Parent: sch.GetField("f4.Item")}},
    		{"Array of Objects (Item field direct)", "f4.a", &State{Name: "a", DataPath: "f4.a", SchemaPath: "f4.Item.a", Parent: sch.GetField("f4.Item")}},
    		{"Array of Strings", "f5", &State{Name: "f5", DataPath: "f5", SchemaPath: "f5", Parent: sch}},
    		{"Array of Strings (Item)", "f5.Item", &State{Name: "Item", DataPath: "f5", SchemaPath: "f5.Item", Parent: sch.GetField("f5")}},
    		{"Inline Same name not found", "f6.a", nil},
    		{"Inline Same name (direct)", "f6.f6.a", &State{Name: "a", DataPath: "f6.a", SchemaPath: "f6.f6.a", Parent: sch.GetField("f6.f6"), HasInline: true}},
    	}
    
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			got := sch.GetField(tt.path)
    			var st *State
    			if got != nil {
    				st = got.State
    			}
    
    			assert.Equal(t, tt.want, st)
    			sch.ClearState()
    			sch.EnableState()
    			if got != nil {
    				assert.Nil(t, got.State)
    			}
    		})
    	}
    }
    
    func TestField_ListFieldsRecursive(t *testing.T) {
    	sch := Object(
    		"f1", Object(
    			"f1a", String(),
    			"f1b", String().SetSingleLocale(true),
    		),
    		"f2", String(),
    		"f3", Object( // inline object
    			true,
    			"f3a", String(),
    			"f3b", Object(true, "f3bc", String()),
    		).SetSingleLocale(true),
    		"f4", Array(Object("f4a", String())),
    		"f5", Array(String()),
    		"f6", Object(true, "f6", Object("f6a", String())),
    	)
    
    	sch.EnableState()
    
    	fields := sch.ListFieldsRecursive()
    	assert.Len(t, fields, 16)
    	for _, f := range fields {
    		assert.NotNil(t, f.State)
    		assert.NotEmpty(t, f.State.Name)
    	}
    }
    
    func TestField_ListFieldsRecursive_WithFilter(t *testing.T) {
    	sch := Object(
    		"f1", Object(
    			"b", Object(
    				"c", String().SetSingleLocale(true),
    			),
    		),
    		"f2", Object(
    			"b", Object(
    				"c", String().SetSingleLocale(true),
    			),
    		).SetSingleLocale(true),
    	)
    
    	sch.EnableState()
    
    	fields := sch.ListFieldsRecursive(func(f *Field) bool { return f.SingleLocale == true })
    	assert.Len(t, fields, 3)
    }
    
    func TestField_CloneWithState(t *testing.T) {
    	f := Object("a", String())
    	fld := f.Clone(false)
    	assert.Nil(t, fld.State)
    	f.EnableState()
    	fld = f.Clone(false)
    	assert.NotNil(t, fld.State)
    }