Ejemplo n.º 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
}
Ejemplo n.º 2
0
// Add inserts relationships for testing.
func Add(context interface{}, db *db.DB, rels []relationship.Relationship) error {
	for _, rel := range rels {
		if err := relationship.Upsert(context, db, &rel); err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 3
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)
		}
	}
}
Ejemplo n.º 4
0
// Upsert inserts or updates the posted Relationship document into the database.
// 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal
func (relationshipHandle) Upsert(c *web.Context) error {
	var rel relationship.Relationship
	if err := json.NewDecoder(c.Request.Body).Decode(&rel); err != nil {
		return err
	}

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

	c.Respond(nil, http.StatusNoContent)
	return nil
}
Ejemplo n.º 5
0
// TestGetAll tests if we can get all relationships from the db.
func TestGetAll(t *testing.T) {
	rels, db := setup(t)
	defer teardown(t, db)

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

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

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

			var filteredRels []relationship.Relationship
			for _, rel := range relsBack {
				if rel.Predicate[0:len(prefix)] == prefix {
					filteredRels = append(filteredRels, rel)
				}
			}

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