// loadItems adds items to run tests. func loadItems(context interface{}, db *db.DB, store *cayley.Handle) error { items, err := itemfix.Get() if err != nil { return err } for _, itm := range items { if err := sponge.Import(context, db, store, &itm); err != nil { return err } } return nil }
// loadItems adds items to run tests. func loadItems(context interface{}, db *db.DB) error { store, err := db.GraphHandle(context) if err != nil { return err } items, err := itemfix.Get() if err != nil { return err } for _, itm := range items { if err := sponge.Import(context, db, store, &itm); err != nil { return err } } return nil }
// Import receives POSTed data, itemizes it then imports it via the item API. // 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal. func (dataHandle) Import(c *web.Context) error { // Unmarshall the data packet from the Request Body. var dat map[string]interface{} if err := json.NewDecoder(c.Request.Body).Decode(&dat); err != nil { return err } // Create a new item with known Type, Version and Data. itm := item.Item{ Type: c.Params["type"], Version: defaultVersion, Data: dat, } // Item.ID must be inferred from the source_id in the data. if err := itm.InferIDFromData(); err != nil { return err } db := c.Ctx["DB"].(*db.DB) graphHandle, err := db.GraphHandle(c.SessionID) if err != nil { return err } // Upsert the item into the items collection and add/remove necessary // quads to/from the graph. if err := sponge.Import(c.SessionID, db, graphHandle, &itm); err != nil { return err } // Respond with no content success. c.Respond(itm, http.StatusOK) return nil }
// Import inserts or updates the posted Item document into the items collection // and adds/removes any necessary quads to/from the relationship graph. // 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal func (itemHandle) Import(c *web.Context) error { // Decode the item. var itm item.Item if err := json.NewDecoder(c.Request.Body).Decode(&itm); err != nil { return err } db := c.Ctx["DB"].(*db.DB) graphHandle, err := db.GraphHandle(c.SessionID) if err != nil { return err } // Upsert the item into the items collection and add/remove necessary // quads to/from the graph. if err := sponge.Import(c.SessionID, db, graphHandle, &itm); err != nil { return err } c.Respond(itm, http.StatusOK) return nil }
// TestImportRemoveItem tests the insert and update of an item. func TestImportRemoveItem(t *testing.T) { db, store := setup(t) defer teardown(t, db, store) t.Log("Given the need to import an item.") { //---------------------------------------------------------------------- // Get the fixture. items, err := itemfix.Get() if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the fixture.", tests.Success) //---------------------------------------------------------------------- // Import the Item. if err := sponge.Import(tests.Context, db, store, &items[0]); err != nil { t.Fatalf("\t%s\tShould be able to import an item : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to import an item", tests.Success) //---------------------------------------------------------------------- // Check the inferred relationship. p := cayley.StartPath(store, quad.String("ITEST_80aa936a-f618-4234-a7be-df59a14cf8de")).Out(quad.String("authored")) it, _ := p.BuildIterator().Optimize() defer it.Close() for it.Next() { token := it.Result() value := store.NameOf(token) if quad.NativeOf(value) != "ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82" { t.Fatalf("\t%s\tShould be able to get the inferred relationships from the graph", tests.Failed) } } if err := it.Err(); err != nil { t.Fatalf("\t%s\tShould be able to get the inferred relationships from the graph : %s", tests.Failed, err) } it.Close() t.Logf("\t%s\tShould be able to get the inferred relationships from the graph.", tests.Success) //---------------------------------------------------------------------- // Import the Item again to test for duplicate imports. if err := sponge.Import(tests.Context, db, store, &items[0]); err != nil { t.Fatalf("\t%s\tShould be able to import a duplicate item : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to import a duplicate item", tests.Success) //---------------------------------------------------------------------- // Remove the item. if err := sponge.Remove(tests.Context, db, store, items[0].ID); err != nil { t.Fatalf("\t%s\tShould be able to remove the item : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to remove the item", tests.Success) //---------------------------------------------------------------------- // Check the inferred relationships. p = cayley.StartPath(store, quad.String("ITEST_80aa936a-f618-4234-a7be-df59a14cf8de")).Out(quad.String("authored")) it, _ = p.BuildIterator().Optimize() defer it.Close() var count int for it.Next() { count++ } if err := it.Err(); err != nil { t.Fatalf("\t%s\tShould be able to confirm removed relationships : %s", tests.Failed, err) } if count > 0 { t.Fatalf("\t%s\tShould be able to confirm removed relationships.", tests.Failed) } t.Logf("\t%s\tShould be able to confirm removed relationships.", tests.Success) } }