github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/apiv3/servicecontext/test_test.go (about) 1 package servicecontext 2 3 import ( 4 "fmt" 5 "net/http" 6 "sort" 7 "testing" 8 9 "github.com/evergreen-ci/evergreen/apiv3" 10 "github.com/evergreen-ci/evergreen/db" 11 "github.com/evergreen-ci/evergreen/model/task" 12 "github.com/evergreen-ci/evergreen/testutil" 13 . "github.com/smartystreets/goconvey/convey" 14 ) 15 16 func TestFindTestsByTaskId(t *testing.T) { 17 testutil.ConfigureIntegrationTest(t, testConfig, "TestFindTestsByTaskId") 18 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig)) 19 20 serviceContext := &DBServiceContext{} 21 numTests := 10 22 numTasks := 2 23 testFileNames := make([]string, numTests) 24 for ix := range testFileNames { 25 testFileNames[ix] = fmt.Sprintf("file_%d", ix) 26 } 27 sort.StringSlice(testFileNames).Sort() 28 29 Convey("When there are task documents in the database with tests", t, func() { 30 testutil.HandleTestingErr(db.Clear(task.Collection), t, "Error clearing"+ 31 " '%v' collection", task.Collection) 32 for i := 0; i < numTasks; i++ { 33 testTask := &task.Task{ 34 Id: fmt.Sprintf("task_%d", i), 35 } 36 tests := make([]task.TestResult, numTests) 37 for j := 0; j < numTests; j++ { 38 status := "pass" 39 if j%2 == 0 { 40 status = "fail" 41 } 42 tests[j] = task.TestResult{ 43 Status: status, 44 TestFile: testFileNames[j], 45 } 46 } 47 testTask.TestResults = tests 48 So(testTask.Insert(), ShouldBeNil) 49 } 50 51 Convey("then properly finding each set of tests should succeed", func() { 52 for i := 0; i < numTasks; i++ { 53 foundTests, err := serviceContext.FindTestsByTaskId(fmt.Sprintf("task_%d", i), "", "", 0, 1) 54 So(err, ShouldBeNil) 55 So(len(foundTests), ShouldEqual, numTests) 56 } 57 }) 58 Convey("then properly finding only tasks with status should return correct set", func() { 59 for _, status := range []string{"pass", "fail"} { 60 for i := 0; i < numTasks; i++ { 61 foundTests, err := serviceContext.FindTestsByTaskId(fmt.Sprintf("task_%d", i), "", status, 0, 1) 62 So(err, ShouldBeNil) 63 So(len(foundTests), ShouldEqual, numTests/2) 64 for _, t := range foundTests { 65 So(t.Status, ShouldEqual, status) 66 } 67 } 68 } 69 }) 70 Convey("then properly finding only tasks from test file should return correct set", func() { 71 taskId := "task_1" 72 for _, sort := range []int{1, -1} { 73 for i := 0; i < numTests; i++ { 74 foundTests, err := serviceContext.FindTestsByTaskId(taskId, testFileNames[i], "", 0, sort) 75 So(err, ShouldBeNil) 76 77 startAt := 0 78 if sort < 0 { 79 startAt = len(testFileNames) - 1 80 } 81 82 So(len(foundTests), ShouldEqual, (numTests-startAt)-i*sort) 83 for ix, t := range foundTests { 84 index := ix 85 if sort > 0 { 86 index += i 87 } 88 So(t.TestFile, ShouldEqual, testFileNames[index]) 89 } 90 } 91 } 92 }) 93 Convey("then adding a limit should return correct number and set of results"+ 94 " fail with an APIError", func() { 95 taskname := "task_0" 96 limit := 2 97 for i := 0; i < numTests/limit; i++ { 98 index := i * limit 99 testName := testFileNames[index] 100 foundTests, err := serviceContext.FindTestsByTaskId(taskname, testName, "", limit, 1) 101 So(err, ShouldBeNil) 102 So(len(foundTests), ShouldEqual, limit) 103 for ix, t := range foundTests { 104 So(t.TestFile, ShouldEqual, testFileNames[ix+index]) 105 } 106 } 107 108 }) 109 Convey("then searching for task that doesn't exist should"+ 110 " fail with an APIError", func() { 111 foundTests, err := serviceContext.FindTestsByTaskId("fake_task", "", "", 0, 1) 112 So(err, ShouldNotBeNil) 113 So(len(foundTests), ShouldEqual, 0) 114 115 So(err, ShouldHaveSameTypeAs, &apiv3.APIError{}) 116 apiErr, ok := err.(*apiv3.APIError) 117 So(ok, ShouldBeTrue) 118 So(apiErr.StatusCode, ShouldEqual, http.StatusNotFound) 119 }) 120 Convey("then searching for a task with no test_file should return first result", 121 func() { 122 taskname := "task_0" 123 foundTests, err := serviceContext.FindTestsByTaskId(taskname, "", "", 1, 1) 124 So(err, ShouldBeNil) 125 So(len(foundTests), ShouldEqual, 1) 126 test1 := foundTests[0] 127 So(test1.TestFile, ShouldEqual, testFileNames[0]) 128 }) 129 Convey("then starting at a test that doesn't exist"+ 130 " fail with an APIError", func() { 131 taskId := "task_1" 132 foundTests, err := serviceContext.FindTestsByTaskId(taskId, "fake_test", "", 0, 1) 133 So(err, ShouldNotBeNil) 134 So(len(foundTests), ShouldEqual, 0) 135 136 So(err, ShouldHaveSameTypeAs, &apiv3.APIError{}) 137 apiErr, ok := err.(*apiv3.APIError) 138 So(ok, ShouldBeTrue) 139 So(apiErr.StatusCode, ShouldEqual, http.StatusNotFound) 140 }) 141 }) 142 }