Example #1
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)
		}
	}
}
Example #2
0
// Import imports an item into the items collections and into the graph database.
func Import(context interface{}, db *db.DB, graph *cayley.Handle, itm *item.Item) error {
	log.Dev(context, "Import", "Started : ID[%s]", itm.ID)

	// If the item exists and is different than the provided item,
	// we need to remove existing relationships.
	if itm.ID != "" {

		// Get the item if it exists.
		itmOrig, err := item.GetByID(context, db, itm.ID)
		if err != nil {
			if err != item.ErrNotFound {
				log.Error(context, "Import", err, "Completed")
				return err
			}
		}

		// If the item exits, we need to determine if the item is the
		// same as the original.  If not, we need to update the
		// relationships.
		if itmOrig.ID != "" {

			// If the item is identical, we don't have to do anything.
			if reflect.DeepEqual(itmOrig, itm) {
				log.Dev(context, "Import", "Completed")
				return nil
			}

			// If the item is not identical, remove the stale relationships by
			// preparing an item map.
			itmMap := map[string]interface{}{
				"item_id": itmOrig.ID,
				"type":    itmOrig.Type,
				"version": itmOrig.Version,
				"data":    itmOrig.Data,
			}

			// Remove the corresponding relationships from the graph.
			if err := wire.RemoveFromGraph(context, db, graph, itmMap); err != nil {
				log.Error(context, "Import", err, "Completed")
				return err
			}
		}
	}

	// Add the item to the items collection.
	if err := item.Upsert(context, db, itm); err != nil {
		log.Error(context, "Import", err, "Completed")
		return err
	}

	// Prepare the generic item data map.
	itmMap := map[string]interface{}{
		"item_id": itm.ID,
		"type":    itm.Type,
		"version": itm.Version,
		"data":    itm.Data,
	}

	// Infer relationships and add them to the graph.
	if err := wire.AddToGraph(context, db, graph, itmMap); err != nil {
		log.Error(context, "Import", err, "Completed")
		return err
	}

	log.Dev(context, "Import", "Completed")
	return nil
}