streaming

Media streaming and broadcast systems in Go
Log | Files | Refs | README | LICENSE

commit 9787ba1ad2c23ae5095f8a61854daec5b1fc42eb
parent e471dede46d0565f974f7fc295f5cb55985eba4d
Author: Oliver Lowe <o@olowe.co>
Date:   Tue, 30 Apr 2024 22:18:32 +1000

internal/scte35: shorten TestSpliceInfo*

This deduplicates all the SpliceInfoSection testing logic and literals
into 4 plain files and about 15 lines of code. Less reliance on testify.

Diffstat:
Minternal/scte35/splice_info_section_test.go | 290++++++++++++++++---------------------------------------------------------------
Ainternal/scte35/testdata/saptype_missing.json | 35+++++++++++++++++++++++++++++++++++
Ainternal/scte35/testdata/saptype_missing.xml | 11+++++++++++
Ainternal/scte35/testdata/saptype_specified.json | 36++++++++++++++++++++++++++++++++++++
Ainternal/scte35/testdata/saptype_specified.xml | 11+++++++++++
5 files changed, 152 insertions(+), 231 deletions(-)

diff --git a/internal/scte35/splice_info_section_test.go b/internal/scte35/splice_info_section_test.go @@ -3,250 +3,78 @@ package scte35 import ( "encoding/json" "encoding/xml" + "os" + "path" + "strings" "testing" "github.com/stretchr/testify/require" ) -func TestSpliceInfoSection_UnmarshalXML(t *testing.T) { - cases := map[string]struct { - xml string - expected *SpliceInfoSection - }{ - "SAPType Missing": { - xml: ` - <SpliceInfoSection xmlns="http://www.scte.org/schemas/35" tier="4095"> - <EncryptedPacket xmlns="http://www.scte.org/schemas/35" cwIndex="255"></EncryptedPacket> - <TimeSignal xmlns="http://www.scte.org/schemas/35"> - <SpliceTime xmlns="http://www.scte.org/schemas/35" ptsTime="1924989008"></SpliceTime> - </TimeSignal> - <SegmentationDescriptor xmlns="http://www.scte.org/schemas/35" segmentationEventId="1207959694" segmentationDuration="27630000" segmentationTypeId="52" segmentNum="2"> - <DeliveryRestrictions xmlns="http://www.scte.org/schemas/35" archiveAllowedFlag="true" webDeliveryAllowedFlag="false" noRegionalBlackoutFlag="true" deviceRestrictions="3"></DeliveryRestrictions> - <SegmentationUpid xmlns="http://www.scte.org/schemas/35" segmentationUpidType="8">748724618</SegmentationUpid> - </SegmentationDescriptor> - </SpliceInfoSection>`, - expected: &SpliceInfoSection{ - Tier: uint32(4095), - SAPType: SAPTypeNotSpecified, - EncryptedPacket: EncryptedPacket{CWIndex: 255}, - SpliceCommand: &TimeSignal{ - SpliceTime: SpliceTime{PTSTime: uint64ptr(1924989008)}, - }, - SpliceDescriptors: SpliceDescriptors{ - &SegmentationDescriptor{ - DeliveryRestrictions: &DeliveryRestrictions{ - ArchiveAllowedFlag: true, - WebDeliveryAllowedFlag: false, - NoRegionalBlackoutFlag: true, - DeviceRestrictions: 3, - }, - SegmentationUPIDs: []SegmentationUPID{ - { - Type: SegmentationUPIDTypeTI, - Value: "748724618", - }, - }, - SegmentationEventID: uint32(1207959694), - SegmentationDuration: uint64ptr(27630000), - SegmentationTypeID: SegmentationTypeProviderPOStart, - SegmentNum: 2, - }, - }, - }, - }, - "SAPType Specified": { - xml: ` - <SpliceInfoSection xmlns="http://www.scte.org/schemas/35" tier="4095" sapType="0"> - <EncryptedPacket xmlns="http://www.scte.org/schemas/35" cwIndex="255"></EncryptedPacket> - <TimeSignal xmlns="http://www.scte.org/schemas/35"> - <SpliceTime xmlns="http://www.scte.org/schemas/35" ptsTime="1924989008"></SpliceTime> - </TimeSignal> - <SegmentationDescriptor xmlns="http://www.scte.org/schemas/35" segmentationEventId="1207959694" segmentationDuration="27630000" segmentationTypeId="52" segmentNum="2"> - <DeliveryRestrictions xmlns="http://www.scte.org/schemas/35" archiveAllowedFlag="true" webDeliveryAllowedFlag="false" noRegionalBlackoutFlag="true" deviceRestrictions="3"></DeliveryRestrictions> - <SegmentationUpid xmlns="http://www.scte.org/schemas/35" segmentationUpidType="8">748724618</SegmentationUpid> - </SegmentationDescriptor> - </SpliceInfoSection>`, - expected: &SpliceInfoSection{ - Tier: uint32(4095), - SAPType: SAPType1, - EncryptedPacket: EncryptedPacket{CWIndex: 255}, - SpliceCommand: &TimeSignal{ - SpliceTime: SpliceTime{PTSTime: uint64ptr(1924989008)}, - }, - SpliceDescriptors: SpliceDescriptors{ - &SegmentationDescriptor{ - DeliveryRestrictions: &DeliveryRestrictions{ - ArchiveAllowedFlag: true, - WebDeliveryAllowedFlag: false, - NoRegionalBlackoutFlag: true, - DeviceRestrictions: 3, - }, - SegmentationUPIDs: []SegmentationUPID{ - { - Type: SegmentationUPIDTypeTI, - Value: "748724618", - }, - }, - SegmentationEventID: uint32(1207959694), - SegmentationDuration: uint64ptr(27630000), - SegmentationTypeID: SegmentationTypeProviderPOStart, - SegmentNum: 2, - }, - }, - }, - }, - } - - for k, c := range cases { - t.Run(k, func(t *testing.T) { - var sis SpliceInfoSection - require.NoError(t, xml.Unmarshal([]byte(c.xml), &sis)) - require.Equal(t, toXML(c.expected), toXML(&sis)) - }) - } +type sistest struct { + name string + sis SpliceInfoSection } -func TestSpliceInfoSection_UnmarshalJSON(t *testing.T) { - cases := map[string]struct { - json string - expected *SpliceInfoSection - }{ - "SAPType Missing": { - json: `{ - "encryptedPacket": { - "cwIndex": 255 - }, - "spliceCommand": { - "type": 6, - "spliceTime": { - "ptsTime": 1924989008 - } - }, - "spliceDescriptors": [ - { - "type": 2, - "deliveryRestrictions": { - "archiveAllowedFlag": true, - "webDeliveryAllowedFlag": false, - "noRegionalBlackoutFlag": true, - "deviceRestrictions": 3 - }, - "segmentationUpids": [ - { - "segmentationUpidType": 8, - "format": "text", - "value": "748724618" - } - ], - "segmentationEventId": 1207959694, - "segmentationDuration": 27630000, - "segmentationTypeId": 52, - "segmentNum": 2 - } - ], - "tier": 4095 - }`, - expected: &SpliceInfoSection{ - Tier: uint32(4095), - SAPType: SAPTypeNotSpecified, - EncryptedPacket: EncryptedPacket{CWIndex: 255}, - SpliceCommand: &TimeSignal{ - SpliceTime: SpliceTime{PTSTime: uint64ptr(1924989008)}, - }, - SpliceDescriptors: SpliceDescriptors{ - &SegmentationDescriptor{ - DeliveryRestrictions: &DeliveryRestrictions{ - ArchiveAllowedFlag: true, - WebDeliveryAllowedFlag: false, - NoRegionalBlackoutFlag: true, - DeviceRestrictions: 3, - }, - SegmentationUPIDs: []SegmentationUPID{ - { - Type: SegmentationUPIDTypeTI, - Value: "748724618", - }, - }, - SegmentationEventID: uint32(1207959694), - SegmentationDuration: uint64ptr(27630000), - SegmentationTypeID: SegmentationTypeProviderPOStart, - SegmentNum: 2, - }, - }, +var tsis SpliceInfoSection = SpliceInfoSection{ + Tier: uint32(4095), + SAPType: SAPTypeNotSpecified, + EncryptedPacket: EncryptedPacket{CWIndex: 255}, + SpliceCommand: &TimeSignal{ + SpliceTime: SpliceTime{PTSTime: uint64ptr(1924989008)}, + }, + SpliceDescriptors: SpliceDescriptors{ + &SegmentationDescriptor{ + DeliveryRestrictions: &DeliveryRestrictions{ + ArchiveAllowedFlag: true, + NoRegionalBlackoutFlag: true, + DeviceRestrictions: 3, }, - }, - "SAPType Specified": { - json: `{ - "encryptedPacket": { - "cwIndex": 255 - }, - "sapType": 0, - "spliceCommand": { - "type": 6, - "spliceTime": { - "ptsTime": 1924989008 - } - }, - "spliceDescriptors": [ - { - "type": 2, - "deliveryRestrictions": { - "archiveAllowedFlag": true, - "webDeliveryAllowedFlag": false, - "noRegionalBlackoutFlag": true, - "deviceRestrictions": 3 - }, - "segmentationUpids": [ - { - "segmentationUpidType": 8, - "format": "text", - "value": "748724618" - } - ], - "segmentationEventId": 1207959694, - "segmentationDuration": 27630000, - "segmentationTypeId": 52, - "segmentNum": 2 - } - ], - "tier": 4095 - }`, - expected: &SpliceInfoSection{ - Tier: uint32(4095), - SAPType: SAPType1, - EncryptedPacket: EncryptedPacket{CWIndex: 255}, - SpliceCommand: &TimeSignal{ - SpliceTime: SpliceTime{PTSTime: uint64ptr(1924989008)}, - }, - SpliceDescriptors: SpliceDescriptors{ - &SegmentationDescriptor{ - DeliveryRestrictions: &DeliveryRestrictions{ - ArchiveAllowedFlag: true, - WebDeliveryAllowedFlag: false, - NoRegionalBlackoutFlag: true, - DeviceRestrictions: 3, - }, - SegmentationUPIDs: []SegmentationUPID{ - { - Type: SegmentationUPIDTypeTI, - Value: "748724618", - }, - }, - SegmentationEventID: uint32(1207959694), - SegmentationDuration: uint64ptr(27630000), - SegmentationTypeID: SegmentationTypeProviderPOStart, - SegmentNum: 2, - }, + SegmentationUPIDs: []SegmentationUPID{ + { + Type: SegmentationUPIDTypeTI, + Value: "748724618", }, }, + SegmentationEventID: uint32(1207959694), + SegmentationDuration: uint64ptr(27630000), + SegmentationTypeID: SegmentationTypeProviderPOStart, + SegmentNum: 2, }, + }, +} + +func TestSpliceInfoSection(t *testing.T) { + missingSAP := tsis + withSAP := tsis + withSAP.SAPType = SAPType1 + + var tests = []sistest{ + {"testdata/saptype_missing.xml", missingSAP}, + {"testdata/saptype_missing.json", missingSAP}, + {"testdata/saptype_specified.xml", withSAP}, + {"testdata/saptype_specified.json", withSAP}, } - for k, c := range cases { - t.Run(k, func(t *testing.T) { - var sis SpliceInfoSection - require.NoError(t, json.Unmarshal([]byte(c.json), &sis)) - require.Equal(t, toXML(c.expected), toXML(&sis)) + for _, tt := range tests { + tname := path.Base(tt.name) + t.Run(tname, func(t *testing.T) { + b, err := os.ReadFile(tt.name) + if err != nil { + t.Fatal(err) + } + var got SpliceInfoSection + if strings.HasSuffix(tt.name, "json") { + if err := json.Unmarshal(b, &got); err != nil { + t.Fatal(err) + } + require.Equal(t, toJSON(&tt.sis), toJSON(&got)) + } else { + if err := xml.Unmarshal(b, &got); err != nil { + t.Fatal(err) + } + require.Equal(t, toXML(&tt.sis), toXML(&got)) + } }) } } diff --git a/internal/scte35/testdata/saptype_missing.json b/internal/scte35/testdata/saptype_missing.json @@ -0,0 +1,34 @@ +{ + "encryptedPacket": { + "cwIndex": 255 + }, + "spliceCommand": { + "type": 6, + "spliceTime": { + "ptsTime": 1924989008 + } + }, + "spliceDescriptors": [ + { + "type": 2, + "deliveryRestrictions": { + "archiveAllowedFlag": true, + "webDeliveryAllowedFlag": false, + "noRegionalBlackoutFlag": true, + "deviceRestrictions": 3 + }, + "segmentationUpids": [ + { + "segmentationUpidType": 8, + "format": "text", + "value": "748724618" + } + ], + "segmentationEventId": 1207959694, + "segmentationDuration": 27630000, + "segmentationTypeId": 52, + "segmentNum": 2 + } + ], + "tier": 4095 +} +\ No newline at end of file diff --git a/internal/scte35/testdata/saptype_missing.xml b/internal/scte35/testdata/saptype_missing.xml @@ -0,0 +1,10 @@ +<SpliceInfoSection xmlns="http://www.scte.org/schemas/35" tier="4095"> + <EncryptedPacket xmlns="http://www.scte.org/schemas/35" cwIndex="255"></EncryptedPacket> + <TimeSignal xmlns="http://www.scte.org/schemas/35"> + <SpliceTime xmlns="http://www.scte.org/schemas/35" ptsTime="1924989008"></SpliceTime> + </TimeSignal> + <SegmentationDescriptor xmlns="http://www.scte.org/schemas/35" segmentationEventId="1207959694" segmentationDuration="27630000" segmentationTypeId="52" segmentNum="2"> + <DeliveryRestrictions xmlns="http://www.scte.org/schemas/35" archiveAllowedFlag="true" webDeliveryAllowedFlag="false" noRegionalBlackoutFlag="true" deviceRestrictions="3"></DeliveryRestrictions> + <SegmentationUpid xmlns="http://www.scte.org/schemas/35" segmentationUpidType="8">748724618</SegmentationUpid> + </SegmentationDescriptor> +</SpliceInfoSection> +\ No newline at end of file diff --git a/internal/scte35/testdata/saptype_specified.json b/internal/scte35/testdata/saptype_specified.json @@ -0,0 +1,35 @@ +{ + "encryptedPacket": { + "cwIndex": 255 + }, + "sapType": 0, + "spliceCommand": { + "type": 6, + "spliceTime": { + "ptsTime": 1924989008 + } + }, + "spliceDescriptors": [ + { + "type": 2, + "deliveryRestrictions": { + "archiveAllowedFlag": true, + "webDeliveryAllowedFlag": false, + "noRegionalBlackoutFlag": true, + "deviceRestrictions": 3 + }, + "segmentationUpids": [ + { + "segmentationUpidType": 8, + "format": "text", + "value": "748724618" + } + ], + "segmentationEventId": 1207959694, + "segmentationDuration": 27630000, + "segmentationTypeId": 52, + "segmentNum": 2 + } + ], + "tier": 4095 +} +\ No newline at end of file diff --git a/internal/scte35/testdata/saptype_specified.xml b/internal/scte35/testdata/saptype_specified.xml @@ -0,0 +1,10 @@ +<SpliceInfoSection xmlns="http://www.scte.org/schemas/35" tier="4095" sapType="0"> + <EncryptedPacket xmlns="http://www.scte.org/schemas/35" cwIndex="255"></EncryptedPacket> + <TimeSignal xmlns="http://www.scte.org/schemas/35"> + <SpliceTime xmlns="http://www.scte.org/schemas/35" ptsTime="1924989008"></SpliceTime> + </TimeSignal> + <SegmentationDescriptor xmlns="http://www.scte.org/schemas/35" segmentationEventId="1207959694" segmentationDuration="27630000" segmentationTypeId="52" segmentNum="2"> + <DeliveryRestrictions xmlns="http://www.scte.org/schemas/35" archiveAllowedFlag="true" webDeliveryAllowedFlag="false" noRegionalBlackoutFlag="true" deviceRestrictions="3"></DeliveryRestrictions> + <SegmentationUpid xmlns="http://www.scte.org/schemas/35" segmentationUpidType="8">748724618</SegmentationUpid> + </SegmentationDescriptor> +</SpliceInfoSection> +\ No newline at end of file