github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/artifact/artifact_file_test.go (about)

     1  package artifact
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/evergreen-ci/evergreen/db"
     7  	"github.com/evergreen-ci/evergreen/testutil"
     8  	. "github.com/smartystreets/goconvey/convey"
     9  	"gopkg.in/mgo.v2/bson"
    10  )
    11  
    12  func init() {
    13  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testutil.TestConfig()))
    14  }
    15  
    16  func reset(t *testing.T) {
    17  	testutil.HandleTestingErr(
    18  		db.Clear(Collection),
    19  		t, "Error clearing collection")
    20  }
    21  
    22  func TestEntryUpsert(t *testing.T) {
    23  	Convey("With an artifact file entry", t, func() {
    24  		reset(t)
    25  
    26  		testEntry := Entry{
    27  			TaskId:          "task1",
    28  			TaskDisplayName: "Task One",
    29  			BuildId:         "build1",
    30  			Files: []File{
    31  				{"cat_pix", "http://placekitten.com/800/600", ""},
    32  				{"fast_download", "https://fastdl.mongodb.org", ""},
    33  			},
    34  		}
    35  
    36  		Convey("upsert should succeed", func() {
    37  			So(testEntry.Upsert(), ShouldBeNil)
    38  
    39  			Convey("so all fields should be present in the db", func() {
    40  				entryFromDb, err := FindOne(ByTaskId("task1"))
    41  				So(err, ShouldBeNil)
    42  				So(entryFromDb.TaskId, ShouldEqual, "task1")
    43  				So(entryFromDb.TaskDisplayName, ShouldEqual, "Task One")
    44  				So(entryFromDb.BuildId, ShouldEqual, "build1")
    45  				So(len(entryFromDb.Files), ShouldEqual, 2)
    46  				So(entryFromDb.Files[0].Name, ShouldEqual, "cat_pix")
    47  				So(entryFromDb.Files[0].Link, ShouldEqual, "http://placekitten.com/800/600")
    48  				So(entryFromDb.Files[1].Name, ShouldEqual, "fast_download")
    49  				So(entryFromDb.Files[1].Link, ShouldEqual, "https://fastdl.mongodb.org")
    50  			})
    51  
    52  			Convey("and with a following update", func() {
    53  				// reusing test entry but overwriting files field --
    54  				// consider this as an additional update from the agent
    55  				testEntry.Files = []File{
    56  					{"cat_pix", "http://placekitten.com/300/400", ""},
    57  					{"the_value_of_four", "4", ""},
    58  				}
    59  				So(testEntry.Upsert(), ShouldBeNil)
    60  				count, err := db.Count(Collection, bson.M{})
    61  				So(err, ShouldBeNil)
    62  				So(count, ShouldEqual, 1)
    63  
    64  				Convey("all updated fields should change,", func() {
    65  					entryFromDb, err := FindOne(ByTaskId("task1"))
    66  					So(err, ShouldBeNil)
    67  					So(len(entryFromDb.Files), ShouldEqual, 4)
    68  					So(entryFromDb.Files[0].Name, ShouldEqual, "cat_pix")
    69  					So(entryFromDb.Files[0].Link, ShouldEqual, "http://placekitten.com/800/600")
    70  					So(entryFromDb.Files[1].Name, ShouldEqual, "fast_download")
    71  					So(entryFromDb.Files[1].Link, ShouldEqual, "https://fastdl.mongodb.org")
    72  					So(entryFromDb.Files[2].Name, ShouldEqual, "cat_pix")
    73  					So(entryFromDb.Files[2].Link, ShouldEqual, "http://placekitten.com/300/400")
    74  					So(entryFromDb.Files[3].Name, ShouldEqual, "the_value_of_four")
    75  					So(entryFromDb.Files[3].Link, ShouldEqual, "4")
    76  
    77  					Convey("but non-updated fields should remain unchanged in db", func() {
    78  						So(entryFromDb.TaskId, ShouldEqual, "task1")
    79  						So(entryFromDb.TaskDisplayName, ShouldEqual, "Task One")
    80  						So(entryFromDb.BuildId, ShouldEqual, "build1")
    81  					})
    82  				})
    83  			})
    84  		})
    85  	})
    86  }