Skip to content

Commit a7f7cdb

Browse files
author
Francisco Souza
committed
encodingcom: simplify and add tests to PresetFormat.Stream
1 parent e35e9d7 commit a7f7cdb

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

preset.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package encodingcom
22

3-
import (
4-
"encoding/json"
5-
"log"
6-
)
3+
import "encoding/json"
74

85
const (
96
// AllPresets is used to retrieve all presets in the response of
@@ -86,21 +83,17 @@ type PresetFormat struct {
8683
// Stream function returns a slice of Advanced HLS stream settings for a
8784
// preset format.
8885
func (p PresetFormat) Stream() []Stream {
89-
streamSlice := []Stream{}
90-
newStream := Stream{}
86+
var (
87+
stream Stream
88+
streams []Stream
89+
)
9190
streamRaw, _ := json.Marshal(p.StreamRawMap)
92-
err := json.Unmarshal(streamRaw, &newStream)
93-
if err != nil {
94-
errWithSlice := json.Unmarshal(streamRaw, &streamSlice)
95-
if errWithSlice != nil {
96-
log.Printf("Could NOT parse Preset stream data into Stream object: %s", err.Error())
97-
log.Printf("Could NOT parse Preset stream data into Stream slice: %s", errWithSlice.Error())
98-
return streamSlice
99-
}
91+
if err := json.Unmarshal(streamRaw, &stream); err != nil {
92+
json.Unmarshal(streamRaw, &streams)
10093
} else {
101-
streamSlice = append(streamSlice, newStream)
94+
streams = append(streams, stream)
10295
}
103-
return streamSlice
96+
return streams
10497
}
10598

10699
// SavePresetResponse is the response returned in the SavePreset method.

preset_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,68 @@ func (s *S) TestGetPresetsList(c *check.C) {
267267
}
268268
}
269269

270+
func (s *S) TestPresetFormatStream(c *check.C) {
271+
var tests = []struct {
272+
testCase string
273+
streamRaw interface{}
274+
expected []Stream
275+
}{
276+
{
277+
"single stream",
278+
map[string]interface{}{
279+
"audio_bitrate": "64k",
280+
"audio_volume": "100",
281+
"size": "1080x720",
282+
"two_pass": "yes",
283+
},
284+
[]Stream{
285+
{
286+
AudioBitrate: "64k",
287+
AudioVolume: 100,
288+
Size: "1080x720",
289+
TwoPass: YesNoBoolean(true),
290+
},
291+
},
292+
},
293+
{
294+
"multiple streams",
295+
[]map[string]interface{}{
296+
{
297+
"audio_bitrate": "64k",
298+
"audio_volume": "100",
299+
"size": "1080x720",
300+
"two_pass": "yes",
301+
},
302+
{
303+
"audio_bitrate": "128k",
304+
"audio_volume": "100",
305+
"size": "1920x1080",
306+
"two_pass": "yes",
307+
},
308+
},
309+
[]Stream{
310+
{
311+
AudioBitrate: "64k",
312+
AudioVolume: 100,
313+
Size: "1080x720",
314+
TwoPass: YesNoBoolean(true),
315+
},
316+
{
317+
AudioBitrate: "128k",
318+
AudioVolume: 100,
319+
Size: "1920x1080",
320+
TwoPass: YesNoBoolean(true),
321+
},
322+
},
323+
},
324+
}
325+
for _, test := range tests {
326+
p := PresetFormat{StreamRawMap: test.streamRaw}
327+
streams := p.Stream()
328+
c.Check(streams, check.DeepEquals, test.expected)
329+
}
330+
}
331+
270332
func (s *S) TestGetPreset(c *check.C) {
271333
server, requests := s.startServer(`
272334
{

0 commit comments

Comments
 (0)