예제 #1
0
// Add inserts relationships, views, and patterns for testing.
func Add(context interface{}, db *db.DB, items []item.Item, rels []relationship.Relationship, views []view.View, patterns []pattern.Pattern) error {
	for _, itm := range items {
		if err := item.Upsert(context, db, &itm); err != nil {
			return err
		}
	}

	for _, rel := range rels {
		if err := relationship.Upsert(context, db, &rel); err != nil {
			return err
		}
	}

	for _, pat := range patterns {
		if err := pattern.Upsert(context, db, &pat); err != nil {
			return err
		}
	}

	for _, vw := range views {
		if err := view.Upsert(context, db, &vw); err != nil {
			return err
		}
	}

	return nil
}
예제 #2
0
// Add inserts views for testing.
func Add(context interface{}, db *db.DB, views []view.View) error {
	for _, v := range views {
		if err := view.Upsert(context, db, &v); err != nil {
			return err
		}
	}

	return nil
}
예제 #3
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)
		}
	}
}
예제 #4
0
파일: view.go 프로젝트: coralproject/xenia
// Upsert inserts or updates the posted View document into the database.
// 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal
func (viewHandle) Upsert(c *web.Context) error {
	var v view.View
	if err := json.NewDecoder(c.Request.Body).Decode(&v); err != nil {
		return err
	}

	if err := view.Upsert(c.SessionID, c.Ctx["DB"].(*db.DB), &v); err != nil {
		return err
	}

	c.Respond(nil, http.StatusNoContent)
	return nil
}
예제 #5
0
// TestGetAll tests if we can get all views from the db.
func TestGetAll(t *testing.T) {
	views1, db := setup(t)
	defer teardown(t, db)

	t.Log("Given the need to get all the views in the database.")
	{
		t.Log("\tWhen starting from an empty views collection")
		{

			for _, v := range views1 {
				if err := view.Upsert(tests.Context, db, &v); err != nil {
					t.Fatalf("\t%s\tShould be able to upsert views : %s", tests.Failed, err)
				}
			}
			t.Logf("\t%s\tShould be able to upsert views.", tests.Success)

			views2, err := view.GetAll(tests.Context, db)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get all views : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get all views.", tests.Success)

			var filteredViews []view.View
			for _, vw := range views2 {
				if vw.Name[0:len(prefix)] == prefix {
					filteredViews = append(filteredViews, vw)
				}
			}

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