// 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 }
func main() { // To see how most of this works, see hello_world -- this just add in a transaction store, err := cayley.NewMemoryGraph() if err != nil { log.Fatalln(err) } // Create a transaction of work to do // NOTE: the transaction is independent of the storage type, so comes from cayley rather than store t := cayley.NewTransaction() t.AddQuad(quad.Make("food", "is", "good", nil)) t.AddQuad(quad.Make("phrase of the day", "is of course", "Hello World!", nil)) t.AddQuad(quad.Make("cats", "are", "awesome", nil)) t.AddQuad(quad.Make("cats", "are", "scary", nil)) t.AddQuad(quad.Make("cats", "want to", "kill you", nil)) // Apply the transaction err = store.ApplyTransaction(t) if err != nil { log.Fatalln(err) } p := cayley.StartPath(store, quad.String("cats")).Out(quad.String("are")) err = p.Iterate(nil).EachValue(nil, func(v quad.Value) { fmt.Println("cats are", v.Native()) }) if err != nil { log.Fatalln(err) } }
// unloadTestData removes all the test data from the database. func unloadTestData(context interface{}, db *db.DB) error { // ------------------------------------------------------------ // Clear items, relationships, and views. wirefix.Remove("context", db, wirePrefix) // ------------------------------------------------------------ // Clear cayley graph. 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.RemoveQuad(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 }