tests for timeline

This commit is contained in:
Zadat Olayinka 2021-07-29 09:00:22 +01:00
parent 859a884ec1
commit a72f735aea

View File

@ -2,11 +2,19 @@ package fyipe
import (
"fmt"
"reflect"
"testing"
"time"
)
var errorTracker map[string]interface{}
var customTimeline = &Timeline{
Category: "testing",
Data: "heyy",
Type: "info",
}
func init() {
fmt.Println("setting up, please wait...")
var sampleUser = GetUser()
@ -36,11 +44,7 @@ func init() {
}
func TestTakeInCustomTimelineEvent(t *testing.T) {
customTimeline := &Timeline{
Category: "testing",
Data: "heyy",
Type: "info",
}
expectedResponse := customTimeline.Category
timelineOpt := TrackerOption{
MaxTimeline: 10,
@ -66,3 +70,159 @@ func TestTakeInCustomTimelineEvent(t *testing.T) {
t.Logf("TestTakeInCustomTimelineEvent success expected %v, got %v", expectedResponse, actualResponse)
}
}
func TestPositiveTimelineIsRequired(t *testing.T) {
expectedResponse := ErrInvalidTimeline
timelineOpt := TrackerOption{
MaxTimeline: -5,
}
option := FyipeTrackerOption{
ErrorTrackerId: errorTracker["_id"].(string),
ErrorTrackerKey: errorTracker["key"].(string),
ApiUrl: apiUrl,
Options: timelineOpt,
}
setupResponse := InitTracker(option)
if fmt.Sprint(setupResponse) != expectedResponse {
t.Errorf("TestPositiveTimelineIsRequired failed expected %v, got %v", expectedResponse, setupResponse)
} else {
t.Logf("TestPositiveTimelineIsRequired success expected %v, got %v", expectedResponse, setupResponse)
}
}
func TestCustomTimelineContainsimeStamp(t *testing.T) {
expectedResponse := reflect.TypeOf(time.Now()).String()
timelineOpt := TrackerOption{
MaxTimeline: 10,
}
option := FyipeTrackerOption{
ErrorTrackerId: errorTracker["_id"].(string),
ErrorTrackerKey: errorTracker["key"].(string),
ApiUrl: apiUrl,
Options: timelineOpt,
}
InitTracker(option)
AddToTimeline(customTimeline)
currentTimeline := GetTimeline()
actualResponse := reflect.TypeOf(currentTimeline[0].Timestamp).String()
if fmt.Sprint(actualResponse) != expectedResponse {
t.Errorf("TestCustomTimelineContainsimeStamp failed expected %v, got %v", expectedResponse, actualResponse)
} else {
t.Logf("TestCustomTimelineContainsimeStamp success expected %v, got %v", expectedResponse, actualResponse)
}
}
func TestCustomTimelineContainsEventId(t *testing.T) {
expectedResponse := reflect.TypeOf("hi").String()
timelineOpt := TrackerOption{
MaxTimeline: 10,
}
option := FyipeTrackerOption{
ErrorTrackerId: errorTracker["_id"].(string),
ErrorTrackerKey: errorTracker["key"].(string),
ApiUrl: apiUrl,
Options: timelineOpt,
}
InitTracker(option)
AddToTimeline(customTimeline)
currentTimeline := GetTimeline()
actualResponse := reflect.TypeOf(currentTimeline[0].EventId).String()
if fmt.Sprint(actualResponse) != expectedResponse {
t.Errorf("TestCustomTimelineContainsEventId failed expected %v, got %v", expectedResponse, actualResponse)
} else {
t.Logf("TestCustomTimelineContainsEventId success expected %v, got %v", expectedResponse, actualResponse)
}
}
func TestTwoTimelineContainsSameEventId(t *testing.T) {
timelineOpt := TrackerOption{
MaxTimeline: 10,
}
option := FyipeTrackerOption{
ErrorTrackerId: errorTracker["_id"].(string),
ErrorTrackerKey: errorTracker["key"].(string),
ApiUrl: apiUrl,
Options: timelineOpt,
}
InitTracker(option)
AddToTimeline(customTimeline)
secondCustomTimeline := customTimeline
secondCustomTimeline.Category = "Randoms"
AddToTimeline(secondCustomTimeline)
currentTimeline := GetTimeline()
expectedResponse := currentTimeline[0].EventId
actualResponse := currentTimeline[1].EventId
if fmt.Sprint(actualResponse) != expectedResponse {
t.Errorf("TestTwoTimelineContainsSameEventId failed expected %v, got %v", expectedResponse, actualResponse)
} else {
t.Logf("TestTwoTimelineContainsSameEventId success expected %v, got %v", expectedResponse, actualResponse)
}
}
func TestOlderTimelineAreDiscarded(t *testing.T) {
timelineOpt := TrackerOption{
MaxTimeline: 2,
}
option := FyipeTrackerOption{
ErrorTrackerId: errorTracker["_id"].(string),
ErrorTrackerKey: errorTracker["key"].(string),
ApiUrl: apiUrl,
Options: timelineOpt,
}
InitTracker(option)
AddToTimeline(customTimeline)
secondCustomTimeline := customTimeline
secondCustomTimeline.Category = "Randoms"
AddToTimeline(secondCustomTimeline)
thirdCustomTimeline := &Timeline{
Category: "Common Grounds",
Type: "error",
}
AddToTimeline(thirdCustomTimeline)
// the second and third timeline should remain
expectedFirstTimelineResponse := secondCustomTimeline.Category // this should be the first time in the timeline stored
expectedSecondTimelineResponse := thirdCustomTimeline.Type // this should be the second time in the timeline stored
currentTimeline := GetTimeline()
actualFirstTimelineResponse := currentTimeline[0].Category
actualSecondTimelineResponse := currentTimeline[1].Type
if fmt.Sprint(actualFirstTimelineResponse) != expectedFirstTimelineResponse {
t.Errorf("TestTwoTimelineContainsSameEventId failed expected %v, got %v", expectedFirstTimelineResponse, actualFirstTimelineResponse)
} else {
t.Logf("TestTwoTimelineContainsSameEventId success expected %v, got %v", expectedFirstTimelineResponse, actualFirstTimelineResponse)
}
if fmt.Sprint(actualSecondTimelineResponse) != expectedSecondTimelineResponse {
t.Errorf("TestTwoTimelineContainsSameEventId failed expected %v, got %v", expectedSecondTimelineResponse, actualSecondTimelineResponse)
} else {
t.Logf("TestTwoTimelineContainsSameEventId success expected %v, got %v", expectedSecondTimelineResponse, actualSecondTimelineResponse)
}
}