Exemple #1
0
// loadItems adds items to run tests.
func loadItems(context interface{}, db *db.DB) error {
	items, err := itemfix.Get()
	if err != nil {
		return err
	}

	if err := itemfix.Add(context, db, items[1:]); err != nil {
		return err
	}

	return nil
}
Exemple #2
0
// 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)
		}
	}
}
Exemple #3
0
// 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)
		}
	}
}
Exemple #4
0
// loadItems adds items to run tests.
func loadItems(context interface{}, db *db.DB, store *cayley.Handle) error {
	items, err := itemfix.Get()
	if err != nil {
		return err
	}

	for _, itm := range items {
		if err := sponge.Import(context, db, store, &itm); err != nil {
			return err
		}
	}

	return nil
}
Exemple #5
0
// loadItems adds items to run tests.
func loadItems(context interface{}, db *db.DB) error {

	store, err := db.GraphHandle(context)
	if err != nil {
		return err
	}

	items, err := itemfix.Get()
	if err != nil {
		return err
	}

	for _, itm := range items {
		if err := sponge.Import(context, db, store, &itm); err != nil {
			return err
		}
	}

	return nil
}
Exemple #6
0
// 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)
		}
	}
}
Exemple #7
0
// TestUpsertItem tests the insert and update of an item.
func TestUpsertItem(t *testing.T) {
	store := setup(t)
	defer teardown(t, store)

	t.Log("Given the need to insert and then update an item.")
	{
		//----------------------------------------------------------------------
		// Get the fixture.

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

		itemStrData, err := json.Marshal(&items[0])
		if err != nil {
			t.Fatalf("\t%s\tShould be able to marshal the fixture : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to marshal the fixture.", tests.Success)

		//----------------------------------------------------------------------
		// Insert the Item.

		url := "/v1/item"
		r := httptest.NewRequest("PUT", url, bytes.NewBuffer(itemStrData))
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url to insert : %s", url)
		{
			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to insert the item : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to insert the item.", tests.Success)
		}

		//----------------------------------------------------------------------
		// Check the inferred relationship.

		p := cayley.StartPath(store, quad.String("ITEST_80aa936a-f618-4234-a7be-df59a14cf8de")).Out(quad.String("authored"))
		it, _ := p.BuildIterator().Optimize()
		defer it.Close()
		for it.Next() {
			token := it.Result()
			value := store.NameOf(token)
			if quad.NativeOf(value) != "ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82" {
				t.Fatalf("\t%s\tShould be able to get the inferred relationships from the graph", tests.Failed)
			}
		}
		if err := it.Err(); err != nil {
			t.Fatalf("\t%s\tShould be able to get the inferred relationships from the graph : %s", tests.Failed, err)
		}
		it.Close()
		t.Logf("\t%s\tShould be able to get the inferred relationships from the graph.", tests.Success)

		//----------------------------------------------------------------------
		// Retrieve the item.

		url = "/v1/item/" + items[0].ID
		r = httptest.NewRequest("GET", url, nil)
		w = httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url to get : %s", url)
		{
			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to retrieve the item : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the item.", tests.Success)

			var itemsBack []item.Item
			if err := json.Unmarshal(w.Body.Bytes(), &itemsBack); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)

			if itemsBack[0].ID != items[0].ID || itemsBack[0].Type != items[0].Type {
				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)
		}

		//----------------------------------------------------------------------
		// Update the Item.

		items[0].Version = 2

		itemStrData, err = json.Marshal(items[0])
		if err != nil {
			t.Fatalf("\t%s\tShould be able to marshal the changed fixture : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to marshal the changed fixture.", tests.Success)

		url = "/v1/item"
		r = httptest.NewRequest("PUT", url, bytes.NewBuffer(itemStrData))
		w = httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url to update : %s", url)
		{
			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to update the item : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to update the item.", tests.Success)
		}

		//----------------------------------------------------------------------
		// Retrieve the Item.

		url = "/v1/item/" + items[0].ID
		r = httptest.NewRequest("GET", url, nil)
		w = httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url to get : %s", url)
		{
			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to retrieve the item : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the item.", tests.Success)

			var itUpdated []item.Item
			if err := json.Unmarshal(w.Body.Bytes(), &itUpdated); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)

			if itUpdated[0].Version != 2 {
				t.Log(w.Body.String())
				t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
			}
			t.Logf("\t%s\tShould get the expected result.", tests.Success)
		}
	}
}
Exemple #8
0
// TestImportRemoveItem tests the insert and update of an item.
func TestImportRemoveItem(t *testing.T) {
	db, store := setup(t)
	defer teardown(t, db, store)

	t.Log("Given the need to import an item.")
	{
		//----------------------------------------------------------------------
		// Get the fixture.

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

		//----------------------------------------------------------------------
		// Import the Item.

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

		//----------------------------------------------------------------------
		// Check the inferred relationship.

		p := cayley.StartPath(store, quad.String("ITEST_80aa936a-f618-4234-a7be-df59a14cf8de")).Out(quad.String("authored"))
		it, _ := p.BuildIterator().Optimize()
		defer it.Close()
		for it.Next() {
			token := it.Result()
			value := store.NameOf(token)
			if quad.NativeOf(value) != "ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82" {
				t.Fatalf("\t%s\tShould be able to get the inferred relationships from the graph", tests.Failed)
			}
		}
		if err := it.Err(); err != nil {
			t.Fatalf("\t%s\tShould be able to get the inferred relationships from the graph : %s", tests.Failed, err)
		}
		it.Close()
		t.Logf("\t%s\tShould be able to get the inferred relationships from the graph.", tests.Success)

		//----------------------------------------------------------------------
		// Import the Item again to test for duplicate imports.

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

		//----------------------------------------------------------------------
		// Remove the item.

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

		//----------------------------------------------------------------------
		// Check the inferred relationships.

		p = cayley.StartPath(store, quad.String("ITEST_80aa936a-f618-4234-a7be-df59a14cf8de")).Out(quad.String("authored"))
		it, _ = p.BuildIterator().Optimize()
		defer it.Close()

		var count int
		for it.Next() {
			count++
		}
		if err := it.Err(); err != nil {
			t.Fatalf("\t%s\tShould be able to confirm removed relationships : %s", tests.Failed, err)
		}

		if count > 0 {
			t.Fatalf("\t%s\tShould be able to confirm removed relationships.", tests.Failed)
		}
		t.Logf("\t%s\tShould be able to confirm removed relationships.", tests.Success)
	}
}