Exemplo n.º 1
0
func TestNewGroupField(t *testing.T) {
	ft := datadictionary.NewFieldType("aname", 11, "INT")
	fg := datadictionary.NewGroupFieldDef(ft, true, []datadictionary.MessagePart{})
	assert.NotNil(t, fg)
	assert.Equal(t, "aname", fg.Name())
	assert.Equal(t, true, fg.Required())
}
Exemplo n.º 2
0
func TestNewFieldType(t *testing.T) {
	ft := datadictionary.NewFieldType("myname", 10, "STRING")
	assert.NotNil(t, ft)
	assert.Equal(t, "myname", ft.Name())
	assert.Equal(t, 10, ft.Tag())
	assert.Equal(t, "STRING", ft.Type)
}
Exemplo n.º 3
0
func TestNewFieldDef(t *testing.T) {
	ft := datadictionary.NewFieldType("aname", 11, "INT")

	var tests = []struct {
		required bool
	}{
		{false},
	}

	for _, test := range tests {
		fd := datadictionary.NewFieldDef(ft, test.required)
		assert.False(t, fd.IsGroup(), "field def is not a group")
		assert.Equal(t, "aname", fd.Name())
		assert.Equal(t, test.required, fd.Required())
	}
}
Exemplo n.º 4
0
func TestValidateVisitField(t *testing.T) {
	fieldType0 := datadictionary.NewFieldType("myfield", 11, "STRING")
	fieldDef0 := &datadictionary.FieldDef{FieldType: fieldType0}

	fieldType1 := datadictionary.NewFieldType("myfield", 2, "STRING")
	fieldDef1 := &datadictionary.FieldDef{FieldType: fieldType1, Fields: []*datadictionary.FieldDef{}}

	fieldType2 := datadictionary.NewFieldType("myfield", 3, "STRING")
	fieldDef2 := &datadictionary.FieldDef{FieldType: fieldType2, Fields: []*datadictionary.FieldDef{}}

	groupFieldType := datadictionary.NewFieldType("mygroupfield", 1, "INT")
	groupFieldDef := &datadictionary.FieldDef{FieldType: groupFieldType, Fields: []*datadictionary.FieldDef{fieldDef1, fieldDef2}}

	var field TagValue
	field.init(Tag(11), []byte("value"))

	var repField1 TagValue
	var repField2 TagValue
	repField1.init(Tag(2), []byte("a"))
	repField2.init(Tag(3), []byte("a"))

	var groupID TagValue
	groupID.init(Tag(1), []byte("1"))

	var groupID2 TagValue
	groupID2.init(Tag(1), []byte("2"))

	var groupID3 TagValue
	groupID3.init(Tag(1), []byte("3"))

	var tests = []struct {
		fieldDef             *datadictionary.FieldDef
		fields               TagValues
		expectedRemFields    int
		expectReject         bool
		expectedRejectReason int
	}{
		//non-repeating
		{expectedRemFields: 0,
			fieldDef: fieldDef0,
			fields:   TagValues{field}},
		//single field group
		{expectedRemFields: 0,
			fieldDef: groupFieldDef,
			fields:   TagValues{groupID, repField1}},
		//multiple field group
		{expectedRemFields: 0,
			fieldDef: groupFieldDef,
			fields:   TagValues{groupID, repField1, repField2}},
		//test with trailing tag not in group
		{expectedRemFields: 1,
			fieldDef: groupFieldDef,
			fields:   TagValues{groupID, repField1, repField2, field}},
		//repeats
		{expectedRemFields: 1,
			fieldDef: groupFieldDef,
			fields:   TagValues{groupID2, repField1, repField2, repField1, repField2, field}},
		//REJECT: group size declared > actual group size
		{expectReject: true,
			fieldDef:             groupFieldDef,
			fields:               TagValues{groupID3, repField1, repField2, repField1, repField2, field},
			expectedRejectReason: rejectReasonIncorrectNumInGroupCountForRepeatingGroup,
		},
		{expectReject: true,
			fieldDef:             groupFieldDef,
			fields:               TagValues{groupID3, repField1, repField1, field},
			expectedRejectReason: rejectReasonIncorrectNumInGroupCountForRepeatingGroup,
		},
		//REJECT: group size declared < actual group size
		{expectReject: true,
			fieldDef:             groupFieldDef,
			fields:               TagValues{groupID, repField1, repField2, repField1, repField2, field},
			expectedRejectReason: rejectReasonIncorrectNumInGroupCountForRepeatingGroup,
		},
	}

	for _, test := range tests {
		remFields, reject := validateVisitField(test.fieldDef, test.fields)

		if test.expectReject {
			if reject == nil {
				t.Error("Expected Reject")
			}

			if reject.RejectReason() != test.expectedRejectReason {
				t.Errorf("Expected reject reason %v got %v", test.expectedRejectReason, reject.RejectReason())
			}
			continue
		}

		if reject != nil {
			t.Errorf("Unexpected reject: %v", reject)
		}

		if len(remFields) != test.expectedRemFields {
			t.Errorf("Expected len %v got %v", test.expectedRemFields, len(remFields))
		}
	}
}
Exemplo n.º 5
0
func TestNewMessageDef(t *testing.T) {

	ft1 := datadictionary.NewFieldType("type1", 11, "STRING")
	ft2 := datadictionary.NewFieldType("type2", 12, "STRING")
	ft3 := datadictionary.NewFieldType("type3", 13, "INT")

	optionalfd1 := datadictionary.NewFieldDef(ft1, false)
	requiredfd1 := datadictionary.NewFieldDef(ft1, true)

	optionalfd2 := datadictionary.NewFieldDef(ft2, false)
	//	requiredfd2 := datadictionary.NewFieldDef(ft2, true)

	optionalGroup1 := datadictionary.NewGroupFieldDef(ft3, false, []datadictionary.MessagePart{requiredfd1, optionalfd2})
	requiredGroup1 := datadictionary.NewGroupFieldDef(ft3, true, []datadictionary.MessagePart{requiredfd1, optionalfd2})

	ct1 := datadictionary.NewComponentType("ct1", []datadictionary.MessagePart{requiredGroup1})

	optionalComp1 := datadictionary.NewComponent(ct1, false)

	var tests = []struct {
		parts                 []datadictionary.MessagePart
		expectedTags          datadictionary.TagSet
		expectedRequiredTags  datadictionary.TagSet
		expectedRequiredParts []datadictionary.MessagePart
	}{
		{
			parts:                 []datadictionary.MessagePart{},
			expectedTags:          datadictionary.TagSet{},
			expectedRequiredTags:  datadictionary.TagSet{},
			expectedRequiredParts: []datadictionary.MessagePart(nil),
		},
		{
			parts:                 []datadictionary.MessagePart{optionalfd1},
			expectedTags:          datadictionary.TagSet{11: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{},
			expectedRequiredParts: []datadictionary.MessagePart(nil),
		},
		{
			parts:                 []datadictionary.MessagePart{requiredfd1, optionalfd2},
			expectedTags:          datadictionary.TagSet{11: struct{}{}, 12: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{11: struct{}{}},
			expectedRequiredParts: []datadictionary.MessagePart{requiredfd1},
		},
		{
			parts:                 []datadictionary.MessagePart{optionalGroup1},
			expectedTags:          datadictionary.TagSet{11: struct{}{}, 12: struct{}{}, 13: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{},
			expectedRequiredParts: []datadictionary.MessagePart(nil),
		},
		{
			parts:                 []datadictionary.MessagePart{requiredGroup1},
			expectedTags:          datadictionary.TagSet{11: struct{}{}, 12: struct{}{}, 13: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{13: struct{}{}},
			expectedRequiredParts: []datadictionary.MessagePart{requiredGroup1},
		},
		{
			parts:                 []datadictionary.MessagePart{optionalComp1},
			expectedTags:          datadictionary.TagSet{11: struct{}{}, 12: struct{}{}, 13: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{},
			expectedRequiredParts: []datadictionary.MessagePart(nil),
		},
	}

	for _, test := range tests {
		md := datadictionary.NewMessageDef("some message", "X", test.parts)

		assert.NotNil(t, md)
		assert.Equal(t, "some message", md.Name)
		assert.Equal(t, "X", md.MsgType)
		assert.Equal(t, test.expectedTags, md.Tags)
		assert.Equal(t, test.expectedRequiredTags, md.RequiredTags)
		assert.Equal(t, test.parts, md.Parts)
		assert.Equal(t, test.expectedRequiredParts, md.RequiredParts())
	}
}
Exemplo n.º 6
0
func TestNewComponentType(t *testing.T) {
	ft1 := datadictionary.NewFieldType("aname1", 11, "INT")
	ft2 := datadictionary.NewFieldType("aname2", 12, "INT")

	optionalField1 := datadictionary.NewFieldDef(ft1, false)
	requiredField1 := datadictionary.NewFieldDef(ft1, true)

	optionalField2 := datadictionary.NewFieldDef(ft2, false)
	requiredField2 := datadictionary.NewFieldDef(ft2, true)

	requiredComp1 := datadictionary.NewComponent(
		datadictionary.NewComponentType("comp1", []datadictionary.MessagePart{requiredField1}),
		true)

	optionalComp1 := datadictionary.NewComponent(
		datadictionary.NewComponentType("comp1", []datadictionary.MessagePart{requiredField1}),
		false)

	var tests = []struct {
		testName               string
		parts                  []datadictionary.MessagePart
		expectedFields         []*datadictionary.FieldDef
		expectedRequiredParts  []datadictionary.MessagePart
		expectedRequiredFields []*datadictionary.FieldDef
	}{
		{
			testName:       "test1",
			parts:          []datadictionary.MessagePart{optionalField1},
			expectedFields: []*datadictionary.FieldDef{optionalField1},
		},
		{
			testName:               "test2",
			parts:                  []datadictionary.MessagePart{requiredField1},
			expectedFields:         []*datadictionary.FieldDef{requiredField1},
			expectedRequiredFields: []*datadictionary.FieldDef{requiredField1},
			expectedRequiredParts:  []datadictionary.MessagePart{requiredField1},
		},
		{
			testName:               "test3",
			parts:                  []datadictionary.MessagePart{requiredField1, optionalField2},
			expectedFields:         []*datadictionary.FieldDef{requiredField1, optionalField2},
			expectedRequiredFields: []*datadictionary.FieldDef{requiredField1},
			expectedRequiredParts:  []datadictionary.MessagePart{requiredField1},
		},
		{
			testName:               "test4",
			parts:                  []datadictionary.MessagePart{requiredField2, optionalComp1},
			expectedFields:         []*datadictionary.FieldDef{requiredField2, requiredField1},
			expectedRequiredFields: []*datadictionary.FieldDef{requiredField2},
			expectedRequiredParts:  []datadictionary.MessagePart{requiredField2},
		},
		{
			testName:               "test5",
			parts:                  []datadictionary.MessagePart{requiredField2, requiredComp1},
			expectedFields:         []*datadictionary.FieldDef{requiredField2, requiredField1},
			expectedRequiredFields: []*datadictionary.FieldDef{requiredField2, requiredField1},
			expectedRequiredParts:  []datadictionary.MessagePart{requiredField2, requiredComp1},
		},
	}

	for _, test := range tests {
		ct := datadictionary.NewComponentType("cname", test.parts)
		assert.NotNil(t, ct, test.testName)
		assert.Equal(t, "cname", ct.Name(), test.testName)
		assert.Equal(t, test.expectedFields, ct.Fields(), test.testName)
		assert.Equal(t, test.parts, ct.Parts(), test.testName)
		assert.Equal(t, test.expectedRequiredFields, ct.RequiredFields(), test.testName)
		assert.Equal(t, test.expectedRequiredParts, ct.RequiredParts(), test.testName)
	}
}