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
// Retrieve returns the specified Relationship from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (relationshipHandle) Retrieve(c *web.Context) error {
	rel, err := relationship.GetByPredicate(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["predicate"])
	if err != nil {
		if err == relationship.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(rel, http.StatusOK)
	return nil
}
Example #3
0
// validateStartType verifies the start type of a view path.
func validateStartType(context interface{}, db *db.DB, v *view.View) error {

	// Loop over paths to validate the start type for each.
PathLoop:
	for _, path := range v.Paths {

		// Declare variables to track the first level relationship predicate.
		var firstRel string
		var firstDir string

		// Extract the first level relationship predicate.
		for _, segment := range path.Segments {
			if segment.Level == 1 {
				firstRel = segment.Predicate
				firstDir = segment.Direction
			}
		}

		// Get the relationship metadata.
		rel, err := relationship.GetByPredicate(context, db, firstRel)
		if err != nil {
			return err
		}

		// Get the relevant item types based on the direction of the
		// first relationship in the path.
		var itemTypes []string
		switch firstDir {
		case outString:
			itemTypes = rel.SubjectTypes
		case inString:
			itemTypes = rel.ObjectTypes
		}

		// Validate the starting type provided in the view.
		for _, itemType := range itemTypes {
			if itemType == v.StartType {
				continue PathLoop
			}
		}

		return fmt.Errorf("Start type %s does not match relationship subject types %v", v.StartType, itemTypes)
	}

	return nil
}