Example #1
0
// loadTestData adds all the test data into the database.
func loadTestData(context interface{}, db *db.DB) error {

	// Make sure the old data is clear.
	if err := unloadTestData(context, db); err != nil {
		if !graph.IsQuadNotExist(err) {
			return err
		}
	}

	// -----------------------------------------------------------
	// Load example items, relationships, views, and patterns.

	items, rels, vs, pats, err := wirefix.Get()
	if err != nil {
		return err
	}

	if err := wirefix.Add(context, db, items, rels, vs, pats); err != nil {
		return err
	}

	// -----------------------------------------------------------
	// Build the example graph.

	mongoURI := cfg.MustURL("MONGO_URI")

	if err := cayleyshelf.InitQuadStore(mongoURI.String()); err != nil {
		return err
	}

	var quads []quad.Quad
	quads = append(quads, quad.Make(wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", ""))
	quads = append(quads, quad.Make(wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", ""))
	quads = append(quads, quad.Make(wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", ""))
	quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", ""))
	quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", ""))
	quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"authored", wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", ""))
	quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"flagged", wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", ""))

	tx := cayley.NewTransaction()
	for _, quad := range quads {
		tx.AddQuad(quad)
	}

	if err := db.NewCayley(tests.Context, tests.TestSession); err != nil {
		return err
	}

	store, err := db.GraphHandle(tests.Context)
	if err != nil {
		return err
	}
	defer store.Close()

	if err := store.ApplyTransaction(tx); err != nil {
		return err
	}

	return nil
}
Example #2
0
// setupGraph initializes an in-memory Cayley graph and logging for an individual test.
func setupGraph(t *testing.T) (*db.DB, *cayley.Handle, []map[string]interface{}) {
	tests.ResetLog()

	items, _, _, _, err := wirefix.Get()
	if err != nil {
		t.Fatalf("%s\tShould load item records from the fixture file : %v", tests.Failed, err)
	}
	t.Logf("%s\tShould load item records from the fixture file.", tests.Success)

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}

	store, err := cayley.NewMemoryGraph()
	if err != nil {
		t.Fatalf("\t%s\tShould be able to create a new Cayley graph : %v", tests.Failed, err)
	}
	t.Logf("\t%s\tShould be able to create a new Cayley graph.", tests.Success)

	// Convert the items to maps.
	var itemMaps []map[string]interface{}
	for _, itm := range items {
		itemMap := map[string]interface{}{
			"type":    itm.Type,
			"item_id": itm.ID,
			"version": itm.Version,
			"data":    itm.Data,
		}
		itemMaps = append(itemMaps, itemMap)
	}

	return db, store, itemMaps
}