Exemplo n.º 1
0
// Midware handles the Cayley session management.
func Midware(mongoURI *url.URL) web.Middleware {

	// Create the middleware that we can use to create Cayley sessions with.
	m := func(next web.Handler) web.Handler {

		// Create the handler that will be attached in the middleware chain.
		h := func(c *web.Context) error {

			// Load the mongo db from the request context.
			db := c.Ctx["DB"].(*db.DB)

			// Create the new cayley session based on the mongo connection credentials
			// which will add it to the db object itself.
			if err := db.NewCayley(c.SessionID, mongoURI.Path); err != nil {
				return web.ErrDBNotConfigured
			}

			log.Dev(c.SessionID, "cayley : Midware", "Capture Cayley Session")

			// Close the Cayley session when the handler returns.
			defer func() {
				log.Dev(c.SessionID, "cayley : Midware", "Release Cayley Session")
				db.CloseCayley(c.SessionID)
			}()

			return next(c)
		}

		return h
	}

	return m
}
Exemplo n.º 2
0
// runTest initializes the environment for the tests and allows for
// the proper return code if the test fails or succeeds.
func runTest(m *testing.M) int {

	// Create stub server for Sponged.
	server := setup()
	cfg.SetString("SPONGED_URL", server)

	mongoURI := cfg.MustURL("MONGO_URI")

	// Initialize MongoDB using the `tests.TestSession` as the name of the
	// master session.
	if err := db.RegMasterSession(tests.Context, tests.TestSession, mongoURI.String(), 0); err != nil {
		fmt.Println("Can't register master session: " + err.Error())
		return 1
	}

	a = routes.API()

	// Snatch the mongo session so we can create some test data.
	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		fmt.Println("Unable to get Mongo session")
		return 1
	}
	defer db.CloseMGO(tests.Context)

	if err = db.NewCayley(tests.Context, tests.TestSession); err != nil {
		fmt.Println("Unable to get Cayley support")
	}

	store, err := db.GraphHandle(tests.Context)
	if err != nil {
		fmt.Println("Unable to get Cayley handle")
		return 1
	}
	defer store.Close()

	if err := tstdata.Generate(db); err != nil {
		fmt.Println("Could not generate test data.")
		return 1
	}
	defer tstdata.Drop(db)

	if err := loadItems("context", db, store); err != nil {
		fmt.Println("Could not import items")
		return 1
	}
	defer unloadItems("context", db, store)

	return m.Run()
}
Exemplo n.º 3
0
// setup initializes for each indivdual test.
func setup(t *testing.T) (*db.DB, *cayley.Handle) {
	tests.ResetLog()

	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)
	}

	if err := db.NewCayley(tests.Context, tests.TestSession); err != nil {
		t.Fatalf("%s\tShould be able to get Cayley support : %v", tests.Failed, err)
	}

	store, err := db.GraphHandle(tests.Context)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Cayley handle : %v", tests.Failed, err)
	}

	return db, store
}
Exemplo n.º 4
0
// setup initializes for each indivdual test.
func setup(t *testing.T) *db.DB {
	tests.ResetLog()

	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)
	}

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

	loadTestData(t, db)

	if err := loadRegex(db, "number.json"); err != nil {
		t.Fatalf("\t%s\tShould be able to load regex fixture : %v", tests.Failed, err)
	}
	if err := loadRegex(db, "email.json"); err != nil {
		t.Fatalf("\t%s\tShould be able to load regex fixture : %v", tests.Failed, err)
	}

	if err := loadRelationships("context", db); err != nil {
		t.Fatalf("\t%s\tShould be able to load relationship fixture : %v", tests.Failed, err)
	}

	if err := loadPatterns("context", db); err != nil {
		t.Fatalf("\t%s\tShould be able to load pattern fixture : %v", tests.Failed, err)
	}

	if err := loadViews("context", db); err != nil {
		t.Fatalf("\t%s\tShould be able to load view fixture : %v", tests.Failed, err)
	}

	if err := loadItems("context", db); err != nil {
		t.Fatalf("\t%s\tShould be able to load items : %v", tests.Failed, err)
	}

	return db
}
Exemplo n.º 5
0
// runTest initializes the environment for the tests and allows for
// the proper return code if the test fails or succeeds.
func runTest(m *testing.M) int {
	mongoURI := cfg.MustURL("MONGO_URI")

	// Initialize MongoDB using the `tests.TestSession` as the name of the
	// master session.
	if err := db.RegMasterSession(tests.Context, tests.TestSession, mongoURI.String(), 0); err != nil {
		fmt.Println("Can't register master session: " + err.Error())
		return 1
	}

	// Setup the app for performing tests.
	a = routes.API()

	// Snatch the mongo session so we can create some test data.
	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		fmt.Println("Unable to get Mongo session")
		return 1
	}
	defer db.CloseMGO(tests.Context)

	if err := db.NewCayley(tests.Context, tests.TestSession); err != nil {
		fmt.Println("Unable to get Cayley support")
	}

	store, err := db.GraphHandle(tests.Context)
	if err != nil {
		fmt.Println("Unable to get Cayley handle")
		return 1
	}
	defer store.Close()

	if err := tstdata.Generate(db); err != nil {
		fmt.Println("Could not generate test data.")
		return 1
	}
	defer tstdata.Drop(db)

	// Load the queries.
	if err := loadQuery(db, "basic.json"); err != nil {
		fmt.Println("Could not load queries in basic.json")
		return 1
	}

	if err := loadQuery(db, "basic_view.json"); err != nil {
		fmt.Println("Could not load queries in basic.json")
		return 1
	}
	if err := loadQuery(db, "basic_var.json"); err != nil {
		fmt.Println("Could not load queries in basic_var.json")
		return 1
	}
	defer qfix.Remove(db, "QTEST_O")

	if err := loadScript(db, "basic_script_pre.json"); err != nil {
		fmt.Println("Could not load scripts in basic_script_pre.json")
		return 1
	}
	if err := loadScript(db, "basic_script_pst.json"); err != nil {
		fmt.Println("Could not load scripts in basic_script_pst.json")
		return 1
	}
	defer sfix.Remove(db, "STEST_O")

	if err := loadMasks(db, "basic.json"); err != nil {
		fmt.Println("Could not load masks.")
		return 1
	}
	defer mfix.Remove(db, "test_xenia_data")

	if err := loadRelationships("context", db); err != nil {
		fmt.Println("Could not load relationships.")
		return 1
	}
	defer relationshipfix.Remove("context", db, relPrefix)

	if err := loadViews("context", db); err != nil {
		fmt.Println("Could not load views.")
		return 1
	}
	defer viewfix.Remove("context", db, viewPrefix)

	if err := loadPatterns("context", db); err != nil {
		fmt.Println("Could not load patterns")
		return 1
	}
	defer patternfix.Remove("context", db, "PTEST_")

	if err := loadItems("context", db, store); err != nil {
		fmt.Println("Could not import items")
		return 1
	}
	defer unloadItems("context", db, store)

	return m.Run()
}