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

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

			//----------------------------------------------------------------------
			// Upsert the relationship.

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

			//----------------------------------------------------------------------
			// Get the relationship.

			rel, err := relationship.GetByPredicate(tests.Context, db, rels[0].Predicate)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get the relationship by predicate : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get the relationship by predicate.", tests.Success)

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

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

			//----------------------------------------------------------------------
			// Delete the relationship.

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

			//----------------------------------------------------------------------
			// Get the relationship.

			_, err = relationship.GetByPredicate(tests.Context, db, rels[0].Predicate)
			if err == nil {
				t.Fatalf("\t%s\tShould generate an error when getting a relationship with the deleted predicate : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould generate an error when getting a relationship with the deleted predicate.", tests.Success)
		}
	}
}
Example #2
0
// Delete removes the specified Relationship from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (relationshipHandle) Delete(c *web.Context) error {
	if err := relationship.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["predicate"]); err != nil {
		if err == relationship.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(nil, http.StatusNoContent)
	return nil
}