예제 #1
0
// TestUpsertDelete tests if we can add/remove a view to/from the db.
func TestUpsertDelete(t *testing.T) {
	views, db := setup(t)
	defer teardown(t, db)

	t.Log("Given the need to upsert and delete views.")
	{
		t.Log("\tWhen starting from an empty views collection")
		{

			//----------------------------------------------------------------------
			// Upsert the view.

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

			//----------------------------------------------------------------------
			// Get the view.

			v, err := view.GetByName(tests.Context, db, views[0].Name)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get the view by name : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get the view by name.", tests.Success)

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

			if !reflect.DeepEqual(views[0], *v) {
				t.Logf("\t%+v", views[0])
				t.Logf("\t%+v", v)
				t.Fatalf("\t%s\tShould be able to get back the same view.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same view.", tests.Success)

			//----------------------------------------------------------------------
			// Delete the view.

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

			//----------------------------------------------------------------------
			// Get the view.

			_, err = view.GetByName(tests.Context, db, views[0].Name)
			if err == nil {
				t.Fatalf("\t%s\tShould generate an error when getting a view with the deleted name : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould generate an error when getting a view with the deleted name.", tests.Success)
		}
	}
}
예제 #2
0
파일: wire.go 프로젝트: coralproject/xenia
// Execute executes a graph query to generate the specified view.
func Execute(context interface{}, mgoDB *db.DB, graphDB *cayley.Handle, viewParams *ViewParams) (*Result, error) {
	log.Dev(context, "Execute", "Started : Name[%s]", viewParams.ViewName)

	// Get the view.
	v, err := view.GetByName(context, mgoDB, viewParams.ViewName)
	if err != nil {
		log.Error(context, "Execute", err, "Completed")
		return errResult(err), err
	}

	// Validate the start type.
	if err := validateStartType(context, mgoDB, v); err != nil {
		log.Error(context, "Execute", err, "Completed")
		return errResult(err), err
	}

	// Translate the view path into a graph query path.
	graphPath, err := viewPathToGraphPath(v, viewParams.ItemKey, graphDB)
	if err != nil {
		log.Error(context, "Execute", err, "Completed")
		return errResult(err), err
	}

	// Retrieve the item IDs for the view along with any related item IDs
	// that should be embedded in view items (i.e., "embeds").
	ids, embeds, err := viewIDs(v, graphPath, viewParams.ItemKey, graphDB)
	if err != nil {
		log.Error(context, "Execute", err, "Completed")
		return errResult(err), err
	}

	// Persist the items in the view, if an output Collection is provided.
	if viewParams.ResultsCollection != "" {
		if err := viewSave(context, mgoDB, v, viewParams, ids, embeds); err != nil {
			log.Error(context, "Execute", err, "Completed")
			return errResult(err), err
		}
		result := Result{
			Results: bson.M{"number_of_results": len(ids)},
		}
		return &result, nil
	}

	// Otherwise, gather the items in the view.
	items, err := viewItems(context, mgoDB, v, ids, embeds)
	if err != nil {
		log.Error(context, "Execute", err, "Completed")
		return errResult(err), err
	}
	result := Result{
		Results: items,
	}

	log.Dev(context, "Execute", "Completed")
	return &result, nil
}
예제 #3
0
파일: view.go 프로젝트: coralproject/xenia
// Retrieve returns the specified View from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (viewHandle) Retrieve(c *web.Context) error {
	v, err := view.GetByName(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["name"])
	if err != nil {
		if err == view.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(v, http.StatusOK)
	return nil
}