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 patterns for testing.
func Add(context interface{}, db *db.DB, patterns []pattern.Pattern) error {
	for _, pat := range patterns {
		if err := pattern.Upsert(context, db, &pat); err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 3
0
// TestUpsertDelete tests if we can add/remove a pattern to/from the db.
func TestUpsertDelete(t *testing.T) {
	patterns, db := setup(t)
	defer teardown(t, db)

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

			//----------------------------------------------------------------------
			// Upsert the pattern.

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

			//----------------------------------------------------------------------
			// Get the pattern.

			pat, err := pattern.GetByType(tests.Context, db, patterns[0].Type)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get the pattern by type : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get the pattern by type.", tests.Success)

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

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

			//----------------------------------------------------------------------
			// Delete the pattern.

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

			//----------------------------------------------------------------------
			// Get the pattern.

			_, err = pattern.GetByType(tests.Context, db, patterns[0].Type)
			if err == nil {
				t.Fatalf("\t%s\tShould generate an error when getting a pattern with the deleted type : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould generate an error when getting a pattern with the deleted type.", tests.Success)
		}
	}
}
Ejemplo n.º 4
0
// Upsert inserts or updates the posted Pattern document into the database.
// 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal
func (patternHandle) Upsert(c *web.Context) error {
	var p pattern.Pattern
	if err := json.NewDecoder(c.Request.Body).Decode(&p); err != nil {
		return err
	}

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

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

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

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

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

			var filteredPats []pattern.Pattern
			for _, pat := range patternsBack {
				if pat.Type[0:len(prefix)] == prefix {
					filteredPats = append(filteredPats, pat)
				}
			}

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