コード例 #1
0
ファイル: setup_test.go プロジェクト: coralproject/xenia
// runTest initializes the environment for the tests and allows for
// the proper return code if the test fails or succeeds.
func runTest(m *testing.M) int {

	// Initialize MongoDB using the `tests.TestSession` as the name of the
	// master session.
	if err := db.RegMasterSession(tests.Context, tests.TestSession, cfg.MustURL("MONGO_URI").String(), 0); err != nil {
		fmt.Println("Can't register master session: " + err.Error())
		return 1
	}

	// Setup the app for performing tests.
	a = routes.API()

	// Snatch the mongo session so we can create some test data.
	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		fmt.Println("Unable to get Mongo session")
		return 1
	}
	defer db.CloseMGO(tests.Context)

	if err := loadItems(tests.Context, db); err != nil {
		fmt.Println("Could not load items")
		return 1
	}
	defer itemfix.Remove(tests.Context, db, itemPrefix)

	if err := loadPatterns(tests.Context, db); err != nil {
		fmt.Println("Could not load patterns")
		return 1
	}
	defer patternfix.Remove(tests.Context, db, patternPrefix)

	return m.Run()
}
コード例 #2
0
ファイル: item_test.go プロジェクト: coralproject/xenia
// TestGetByID tests if we can get a single item from the db.
func TestGetByID(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	defer func() {
		if err := itemfix.Remove(tests.Context, db, prefix); err != nil {
			t.Fatalf("\t%s\tShould be able to remove the items : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove the items.", tests.Success)
	}()

	t.Log("Given the need to get an item in the database by ID.")
	{
		t.Log("\tWhen starting from an empty items collection")
		{
			items, err := itemfix.Get()
			if err != nil {
				t.Fatalf("\t%s\tShould be able retrieve item fixture : %s", tests.Failed, err)
			}

			var itemIDs []string
			for _, it := range items {
				if err := item.Upsert(tests.Context, db, &it); err != nil {
					t.Fatalf("\t%s\tShould be able to upsert items : %s", tests.Failed, err)
				}
				itemIDs = append(itemIDs, it.ID)
			}
			t.Logf("\t%s\tShould be able to upsert items.", tests.Success)

			itmBack, err := item.GetByID(tests.Context, db, itemIDs[0])
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get an item by ID : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get an item by ID.", tests.Success)

			// Check equality for all immutable fields: ID, Version, Data. Timestamps will change on Upsert.
			if !reflect.DeepEqual(items[0].Data, itmBack.Data) || (items[0].ID != itmBack.ID) || (items[0].Version != itmBack.Version) {
				t.Logf("\t%+v", items[0])
				t.Logf("\t%+v", itmBack)
				t.Fatalf("\t%s\tShould be able to get back the same item.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same item.", tests.Success)
		}
	}
}
コード例 #3
0
ファイル: item_test.go プロジェクト: coralproject/xenia
// TestGetByIDs tests if we can get items from the db.
func TestGetByIDs(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	defer func() {
		if err := itemfix.Remove(tests.Context, db, prefix); err != nil {
			t.Fatalf("\t%s\tShould be able to remove the items : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove the items.", tests.Success)
	}()

	t.Log("Given the need to get items in the database by IDs.")
	{
		t.Log("\tWhen starting from an empty items collection")
		{
			items1, err := itemfix.Get()
			if err != nil {
				t.Fatalf("\t%s\tShould be able retrieve item fixture : %s", tests.Failed, err)
			}

			var itemIDs []string
			for _, it := range items1 {
				if err := item.Upsert(tests.Context, db, &it); err != nil {
					t.Fatalf("\t%s\tShould be able to upsert items : %s", tests.Failed, err)
				}
				itemIDs = append(itemIDs, it.ID)
			}
			t.Logf("\t%s\tShould be able to upsert items.", tests.Success)

			items2, err := item.GetByIDs(tests.Context, db, itemIDs)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get items by IDs : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get items by IDs.", tests.Success)

			if len(items1) != len(items2) {
				t.Logf("\t%+v", items1)
				t.Logf("\t%+v", items2)
				t.Fatalf("\t%s\tShould be able to get back the same items.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same items.", tests.Success)
		}
	}
}
コード例 #4
0
ファイル: sponge_test.go プロジェクト: coralproject/xenia
// runTest initializes the environment for the tests and allows for
// the proper return code if the test fails or succeeds.
func runTest(m *testing.M) int {

	// Initialize MongoDB using the `tests.TestSession` as the name of the
	// master session.
	if err := db.RegMasterSession(tests.Context, tests.TestSession, cfg.MustURL("MONGO_URI").String(), 0); err != nil {
		fmt.Println("Can't register master session: " + err.Error())
		return 1
	}

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		fmt.Println("Can't get mongo session: " + err.Error())
		return 1
	}
	defer db.CloseMGO(tests.Context)

	loadPatterns("context", db)
	defer patternfix.Remove("context", db, patternPrefix)

	defer itemfix.Remove("context", db, itemPrefix)

	return m.Run()
}
コード例 #5
0
ファイル: item_test.go プロジェクト: coralproject/xenia
// TestUpsertDelete tests if we can add/remove an item to/from the db.
func TestUpsertDelete(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	defer func() {
		if err := itemfix.Remove(tests.Context, db, prefix); err != nil {
			t.Fatalf("\t%s\tShould be able to remove the items : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove the items.", tests.Success)
	}()

	t.Log("Given the need to upsert and delete items.")
	{
		t.Log("\tWhen starting from an empty items collection")
		{
			//----------------------------------------------------------------------
			// Get the fixture.

			items, err := itemfix.Get()
			if err != nil {
				t.Fatalf("\t%s\tShould be able retrieve item fixture : %s", tests.Failed, err)
			}

			//----------------------------------------------------------------------
			// Upsert the item.

			if err := item.Upsert(tests.Context, db, &items[0]); err != nil {
				t.Fatalf("\t%s\tShould be able to upsert a item : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to upsert a item.", tests.Success)

			//----------------------------------------------------------------------
			// Get the item.

			itemsBack, err := item.GetByIDs(tests.Context, db, []string{items[0].ID})
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get the item by ID : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get the item by ID.", tests.Success)

			//----------------------------------------------------------------------
			// Check that we got the item we expected.

			if items[0].ID != itemsBack[0].ID {
				t.Logf("\t%+v", items[0])
				t.Logf("\t%+v", itemsBack[0])
				t.Fatalf("\t%s\tShould be able to get back the same item.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same item.", tests.Success)

			//----------------------------------------------------------------------
			// Check that CreatedAt and UpdatedAt were set.

			if itemsBack[0].CreatedAt.IsZero() || itemsBack[0].UpdatedAt.IsZero() {
				t.Fatalf("\t%s\tShould set CreatedAt and UpdatedAt on upsert.", tests.Failed)
			}
			t.Logf("\t%s\tShould set CreatedAt and UpdatedAt on upsert.", tests.Success)

			//----------------------------------------------------------------------
			// Delete the item.

			if err := item.Delete(tests.Context, db, items[0].ID); err != nil {
				t.Fatalf("\t%s\tShould be able to delete the item : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to delete the item.", tests.Success)

			//----------------------------------------------------------------------
			// Get the item.

			itemsBack, err = item.GetByIDs(tests.Context, db, []string{items[0].ID})
			if len(itemsBack) != 0 {
				t.Fatalf("\t%s\tShould generate an error when getting an item with the deleted ID : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould generate an error when getting an item with the deleted ID.", tests.Success)
		}
	}
}