Last active
August 29, 2015 14:25
-
-
Save johntdyer/3c5375bacdbc196eb4b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
. "github.com/smartystreets/goconvey/convey" | |
"os" | |
"testing" | |
) | |
func TestSpeechCalculatorSpec(t *testing.T) { | |
// Only pass t into top-level Convey calls | |
Convey("speech-calculator", t, func() { | |
Convey("#shouldSkip", func() { | |
config := &Config{} | |
Convey("Should return false is config has no skip so cdr can not match", func() { | |
cdrStruct := &CallDetailRecord{} | |
_ = json.Unmarshal([]byte(`{"call":{"recording_duration":0,"app_type":"tropo_web","parent_session_id":"none","phone_number_sid":"unknown","app_id":432004,"parent_call_id":"none","sip_session_id":"ss_vzwgbcwmre1i","date_created":"Sat, 27 Jun 2015 23:55:01 +0000","date_updated":"Sat, 27 Jun 2015 23:55:01 +0000","service_id":"5128871","account_id":156596,"browser_ip":"10.6.69.189","session_id":"972dc2aa732b421fee00c50844c9bcb7","start_time":"Sat, 27 Jun 2015 23:55:03 +0000","duration":306209,"start_url":"https://foo.com","end_time":"Sun, 28 Jun 2015 00:00:10 +0000","network":"SIP","channel":"VOICE","called":"8778472781","status":"Success","caller":"2078622429","call_id":"7d0866ff31982a8e469609ccba7201be","flags":"in","ppid":461,"tts":[],"id":"00168183229-00000000000004195437"},"@version":"1","@timestamp":"2015-06-28T00:00:10.000Z","tags":["cdr","filtered","cdr_submit","production","production_cdr"],"account_id":156596,"app_id":432004,"ppid":461}`), &cdrStruct) | |
So(shouldSkip(cdrStruct, config), ShouldBeFalse) | |
}) | |
Convey("Should return false if cdr does not match ignore list", func() { | |
config.IgnoredPpids = []int{462} | |
cdrStruct := &CallDetailRecord{} | |
_ = json.Unmarshal([]byte(`{"call":{"recording_duration":0,"app_type":"tropo_web","parent_session_id":"none","phone_number_sid":"unknown","app_id":432004,"parent_call_id":"none","sip_session_id":"ss_vzwgbcwmre1i","date_created":"Sat, 27 Jun 2015 23:55:01 +0000","date_updated":"Sat, 27 Jun 2015 23:55:01 +0000","service_id":"5128871","account_id":156596,"browser_ip":"10.6.69.189","session_id":"972dc2aa732b421fee00c50844c9bcb7","start_time":"Sat, 27 Jun 2015 23:55:03 +0000","duration":306209,"start_url":"https://foo.com","end_time":"Sun, 28 Jun 2015 00:00:10 +0000","network":"SIP","channel":"VOICE","called":"8778472781","status":"Success","caller":"2078622429","call_id":"7d0866ff31982a8e469609ccba7201be","flags":"in","ppid":461,"tts":[],"id":"00168183229-00000000000004195437"},"@version":"1","@timestamp":"2015-06-28T00:00:10.000Z","tags":["cdr","filtered","cdr_submit","production","production_cdr"],"account_id":156596,"app_id":432004,"ppid":461}`), &cdrStruct) | |
So(shouldSkip(cdrStruct, config), ShouldBeFalse) | |
}) | |
Convey("Should return true if cdr ppid is in ignore list", func() { | |
config.IgnoredPpids = []int{461} | |
cdrStruct := &CallDetailRecord{} | |
_ = json.Unmarshal([]byte(`{"call":{"recording_duration":0,"app_type":"tropo_web","parent_session_id":"none","phone_number_sid":"unknown","app_id":432004,"parent_call_id":"none","sip_session_id":"ss_vzwgbcwmre1i","date_created":"Sat, 27 Jun 2015 23:55:01 +0000","date_updated":"Sat, 27 Jun 2015 23:55:01 +0000","service_id":"5128871","account_id":156596,"browser_ip":"10.6.69.189","session_id":"972dc2aa732b421fee00c50844c9bcb7","start_time":"Sat, 27 Jun 2015 23:55:03 +0000","duration":306209,"start_url":"https://foo.com","end_time":"Sun, 28 Jun 2015 00:00:10 +0000","network":"SIP","channel":"VOICE","called":"8778472781","status":"Success","caller":"2078622429","call_id":"7d0866ff31982a8e469609ccba7201be","flags":"in","ppid":461,"tts":[],"id":"00168183229-00000000000004195437"},"@version":"1","@timestamp":"2015-06-28T00:00:10.000Z","tags":["cdr","filtered","cdr_submit","production","production_cdr"],"account_id":156596,"app_id":432004,"ppid":461}`), &cdrStruct) | |
So(shouldSkip(cdrStruct, config), ShouldBeTrue) | |
}) | |
}) | |
Convey("#filterSpeechUsage", func() { | |
usage := make(map[string]float64) | |
usage["en-us"] = 40.1 | |
Convey("single lanugage", func() { | |
config := &Config{ | |
Resource: []string{"en-us"}, | |
} | |
So(filterSpeechUsage(config, usage), ShouldEqual, 40.1) | |
}) | |
Convey("multiple lanugage inclusive", func() { | |
usage["es-mx"] = 10.1 | |
config := &Config{ | |
Resource: []string{"en-us", "es-mx"}, | |
} | |
So(filterSpeechUsage(config, usage), ShouldEqual, 50.2) | |
}) | |
Convey("multiple lanugage exclused", func() { | |
usage["es-mx"] = 10.1 | |
usage["pt-br"] = 10.1 | |
config := &Config{ | |
Resource: []string{"en-us", "es-mx"}, | |
} | |
So(filterSpeechUsage(config, usage), ShouldEqual, 50.2) | |
}) | |
}) | |
Convey("#intSliceToList", func() { | |
Convey("Convert a single int", func() { | |
So(intSliceToList([]int{1}), ShouldEqual, "1") | |
}) | |
Convey("Convert a two ints", func() { | |
So(intSliceToList([]int{1, 2}), ShouldEqual, "1,2") | |
}) | |
}) | |
Convey("#cdrUnmarshal", func() { | |
data := cdrUnmarshal(`{"call":{"recording_duration":0,"app_type":"tropo_web","parent_session_id":"none","phone_number_sid":"unknown","app_id":432004,"parent_call_id":"none","sip_session_id":"ss_vzwgbcwmre1i","date_created":"Sat, 27 Jun 2015 23:55:01 +0000","date_updated":"Sat, 27 Jun 2015 23:55:01 +0000","service_id":"5128871","account_id":156596,"browser_ip":"10.6.69.189","session_id":"972dc2aa732b421fee00c50844c9bcb7","start_time":"Sat, 27 Jun 2015 23:55:03 +0000","duration":306209,"start_url":"https://foo.com","end_time":"Sun, 28 Jun 2015 00:00:10 +0000","network":"SIP","channel":"VOICE","called":"8778472781","status":"Success","caller":"2078622429","call_id":"7d0866ff31982a8e469609ccba7201be","flags":"in","ppid":461,"tts":[],"id":"00168183229-00000000000004195437"},"@version":"1","@timestamp":"2015-06-28T00:00:10.000Z","tags":["cdr","filtered","cdr_submit","production","production_cdr"],"account_id":156596,"app_id":432004,"ppid":461}`) | |
Convey("app type should be found", func() { | |
So(data.Data.AppType, ShouldEqual, "tropo_web") | |
}) | |
Convey("ppid should be found", func() { | |
So(data.Data.Ppid, ShouldEqual, 461) | |
So(data.Ppid, ShouldEqual, 461) | |
}) | |
Convey("tts slice should be empty", func() { | |
So(data.Data.Tts, ShouldBeEmpty) | |
}) | |
}) | |
Convey("#processJSONCdrNew", func() { | |
comms := &Comms{Duration: make(chan float64)} | |
config := &Config{ | |
IgnoredPpids: []int{410, 445}, | |
Resource: []string{"en-us", "es-mx"}, | |
Rounding: 30.0, | |
} | |
Convey("Process ASR", func() { | |
Convey("Process ASR", func() { | |
cdr := `{"call": | |
{ | |
"tts": [ | |
{ "Duration": 1000, "ResourceName": "en-us"} | |
], | |
"asr": [ | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"} | |
]},"ppid": 461}` | |
config.Type = "asr" | |
comms.FileWaitGroup.Add(1) | |
go processJSONCdrNew(cdr, comms, config) | |
duration := <-comms.Duration | |
So(duration, ShouldEqual, 30.0) | |
}) | |
Convey("Process ASR and round up 1 second to 30", func() { | |
cdr := `{"call": { | |
"tts": [ | |
{ "Duration": 1000, "ResourceName": "en-us"} | |
], | |
"asr": [ | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"} | |
]},"ppid": 461}` | |
config.Type = "asr" | |
comms.FileWaitGroup.Add(1) | |
go processJSONCdrNew(cdr, comms, config) | |
duration := <-comms.Duration | |
So(config.Rounding, ShouldEqual, 30.0) | |
So(config.Type, ShouldEqual, "asr") | |
So(duration, ShouldEqual, 36.0) | |
}) | |
Convey("Process ASR and not round up 31 seconds", func() { | |
cdr := `{"call": { | |
"tts": [ | |
{ "Duration": 1000, "ResourceName": "en-us"} | |
], | |
"asr": [ | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 6000, "ResourceName": "en-us"}, | |
{ "Duration": 2000, "ResourceName": "en-us"} | |
]},"ppid": 461}` | |
config.Type = "asr" | |
comms.FileWaitGroup.Add(1) | |
go processJSONCdrNew(cdr, comms, config) | |
duration := <-comms.Duration | |
So(config.Rounding, ShouldEqual, 30.0) | |
So(config.Type, ShouldEqual, "asr") | |
So(duration, ShouldEqual, 32.0) | |
}) | |
}) | |
Convey("Process TTS", func() { | |
Convey("Process a cdr", func() { | |
cdr := `{"call": { | |
"tts": [ | |
{ "Duration": 1000, "ResourceName": "en-us"} | |
], | |
"asr": [ | |
{ "Duration": 6000, "ResourceName": "en-us"} | |
]},"ppid": 461}` | |
config.Type = "tts" | |
comms.FileWaitGroup.Add(1) | |
go processJSONCdrNew(cdr, comms, config) | |
duration := <-comms.Duration | |
So(duration, ShouldEqual, 30.0) | |
}) | |
Convey("Process TTS and round up 1 second to 30", func() { | |
cdr := `{"call": { | |
"tts": [ | |
{ "Duration": 1000, "ResourceName": "en-us"} | |
], | |
"asr": [ | |
{ "Duration": 6000, "ResourceName": "en-us"} | |
]},"ppid": 461}` | |
config.Type = "tts" | |
comms.FileWaitGroup.Add(1) | |
go processJSONCdrNew(cdr, comms, config) | |
duration := <-comms.Duration | |
So(config.Rounding, ShouldEqual, 30.0) | |
So(config.Type, ShouldEqual, "tts") | |
So(duration, ShouldEqual, 30.0) | |
}) | |
Convey("Process TTS and not round up 31 seconds", func() { | |
cdr := `{"call": { | |
"tts": [ | |
{ "Duration": 31000, "ResourceName": "en-us"} | |
], | |
"asr": [ | |
{ "Duration": 6000, "ResourceName": "en-us"} | |
]},"ppid": 461}` | |
config.Type = "tts" | |
comms.FileWaitGroup.Add(1) | |
go processJSONCdrNew(cdr, comms, config) | |
duration := <-comms.Duration | |
So(config.Rounding, ShouldEqual, 30.0) | |
So(config.Type, ShouldEqual, "tts") | |
So(duration, ShouldEqual, 31.0) | |
}) | |
}) | |
}) | |
Convey("#lineCounter", func() { | |
Convey("Count example lines", func() { | |
fh, _ := os.Open("./test_data/foo.txt") | |
defer fh.Close() | |
count, _ := lineCounter(fh) | |
So(count, ShouldEqual, 188631) | |
}) | |
}) | |
SkipConvey("#setupProgressBar", func() { | |
}) | |
SkipConvey("#processFile", func() { | |
}) | |
SkipConvey("#readCDRFile", func() { | |
}) | |
SkipConvey("#incrementTotals", func() { | |
}) | |
Convey("#roundUpUsage", func() { | |
Convey("Round up lanugage to nearest billing increment", func() { | |
usageData := make(map[string]float64) | |
usageData["en-us"] = 20.0 | |
usageData["en-mx"] = 29.0 | |
d := roundUpUsage(usageData, &Config{Rounding: 30.0}) | |
So(d["en-us"], ShouldEqual, 30) | |
So(d["en-mx"], ShouldEqual, 30) | |
}) | |
Convey("Not round up if language does not exist", func() { | |
usageData := make(map[string]float64) | |
d := roundUpUsage(usageData, &Config{Rounding: 30.0}) | |
So(d["en-fo"], ShouldEqual, 0.0) | |
}) | |
Convey("Not round up if rounding is not set", func() { | |
usageData := make(map[string]float64) | |
usageData["en-us"] = 20.0 | |
usageData["en-mx"] = 29.0 | |
d := roundUpUsage(usageData, &Config{}) | |
So(d["en-us"], ShouldEqual, 20) | |
So(d["en-mx"], ShouldEqual, 29) | |
}) | |
}) | |
Convey("#aggregateSpeechUsage", func() { | |
Convey("Process asr payload with no durations", func() { | |
Convey("empty ", func() { | |
So(aggregateSpeechUsage([]SpeechMeter{}), | |
ShouldResemble, | |
make(map[string]float64), | |
) | |
}) | |
}) | |
Convey("Process asr payload with durations", func() { | |
sm := []SpeechMeter{ | |
SpeechMeter{Duration: 8549, ResourceName: "en-us"}, | |
SpeechMeter{Duration: 8549, ResourceName: "en-us"}, | |
SpeechMeter{Duration: 1878, ResourceName: "en-us"}, | |
SpeechMeter{Duration: 3511, ResourceName: "en-us"}, | |
SpeechMeter{Duration: 4066, ResourceName: "en-us"}, | |
SpeechMeter{Duration: 3030, ResourceName: "en-us"}, | |
SpeechMeter{Duration: 5627, ResourceName: "en-us"}, | |
SpeechMeter{Duration: 4589, ResourceName: "es-mx"}, | |
SpeechMeter{Duration: 1234, ResourceName: ""}, | |
} | |
Convey("Calculate proper usaged", func() { | |
Convey("asr - en-us", func() { | |
So(aggregateSpeechUsage(sm)["en-us"], ShouldEqual, 35.21) | |
}) | |
Convey("asr - 'es-mx", func() { | |
So(aggregateSpeechUsage(sm)["es-mx"], ShouldEqual, 4.589) | |
}) | |
Convey("asr 'none'", func() { | |
So(aggregateSpeechUsage(sm)["none"], ShouldEqual, 1.234) | |
}) | |
}) | |
}) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment