From a72f735aead5aa799d5487e84daf05a587744809 Mon Sep 17 00:00:00 2001 From: Zadat Olayinka Date: Thu, 29 Jul 2021 09:00:22 +0100 Subject: [PATCH] tests for timeline --- go-sdk/tracker_test.go | 170 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 165 insertions(+), 5 deletions(-) diff --git a/go-sdk/tracker_test.go b/go-sdk/tracker_test.go index 35182fe3d8..56b8dd1a3f 100644 --- a/go-sdk/tracker_test.go +++ b/go-sdk/tracker_test.go @@ -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) + } + +}