Exemplo n.º 1
0
func TestDeleteWithoutAlias(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Delete("users")
	test.Fatal(t, db.Delete, qb.GetType())
	test.Fatal(t, "DELETE FROM users", qb.String())
}
Exemplo n.º 2
0
func TestDocumentManager_Register_StoreIdAnnotation(t *testing.T) {
	type Person struct {
		ID   bson.ObjectId `bson:"_id"`
		Name string        `bson:"Name"`
	}
	type Family struct {
		ID        bson.ObjectId   `bson:"_id"`
		Name      string          `bson:"Name"`
		Members   []*Person       `odm:"referenceMany(targetDocument:Person,storeId:MemberIDs,cascade:all)"`
		MemberIDs []bson.ObjectId `bson:"MemberIDs"`
	}
	dm, done := getDocumentManager(t)
	defer done()
	err := dm.RegisterMany(map[string]interface{}{
		"Person": new(Person),
		"Family": new(Family),
	})
	test.Fatal(t, err, nil)
	family := &Family{Name: "Doe", Members: []*Person{{Name: "John"}, {Name: "Jane"}, {Name: "Jack"}}}
	dm.Persist(family)
	err = dm.Flush()
	test.Fatal(t, err, nil)
	family = new(Family)
	err = dm.FindOne(bson.M{"Name": "Doe"}, family)
	test.Fatal(t, err, nil)
	test.Fatal(t, len(family.MemberIDs), 3)
}
Exemplo n.º 3
0
func TestSetFirstResult(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.SetFirstResult(10)
	test.Fatal(t, qb.GetState(), db.Dirty)
	test.Fatal(t, qb.GetFirstResult(), 10)
}
Exemplo n.º 4
0
func TestParser(t *testing.T) {
	testLogger := logger.NewTestLogger(t)

	for _, fixture := range []struct {
		String      string
		Length      int
		Definitions []*tag.Definition
	}{
		{`field:foo;complex_field(name:param,name2:param2,name_3:3);field:1;last_field`,
			4,
			[]*tag.Definition{
				{Name: "field", Value: "foo"},
				{Name: "complex_field", Parameters: []tag.Parameter{{Key: "name", Value: "param"}, {Key: "name2", Value: "param2"}, {Key: "name_3", Value: "3"}}},
				{Name: "field", Value: "1"},
				{Name: "last_field"},
			},
		},
	} {
		parser := tag.NewParser(strings.NewReader(fixture.String))
		parser.SetLogger(testLogger)
		definitions, err := parser.Parse()
		test.Fatal(t, err, nil)
		test.Fatal(t, len(definitions), fixture.Length)
		test.Fatal(t, reflect.DeepEqual(definitions, fixture.Definitions), true)
	}

}
Exemplo n.º 5
0
func TestMongo(t *testing.T) {

	type Test struct {
		ID bson.ObjectId `bson:"_id,omitempty"`
		Name,
		Description string
	}

	session, err := mgo.Dial(os.Getenv("MONGODB_TEST_SERVER"))
	test.Fatal(t, err, nil)
	defer session.Close()
	defer session.DB(os.Getenv("MONGODB_TEST_DB")).C("mongo_tests").DropCollection()

	// Optional. Switch the session to a monotonic behavior.
	session.SetMode(mgo.Monotonic, true)
	collection := session.DB(os.Getenv("MONGODB_TEST_DB")).C("mongo_tests")
	test1 := &Test{Name: "Initial", Description: "A simple test"}
	err = collection.Insert(test1)
	test.Fatal(t, err, nil)
	result := new(Test)
	err = collection.Find(bson.M{"name": test1.Name}).One(result)
	test.Error(t, err, nil)
	test.Error(t, result.Description, test1.Description)
	result1 := new(Test)
	err = collection.FindId(result.ID).One(result1)
	test.Error(t, err, nil)
	test.Error(t, result1.ID, result.ID)
}
Exemplo n.º 6
0
func TestConnectionPrepare(t *testing.T) {
	connection := GetConnection(t)
	defer connection.Close()
	result, err := connection.Prepare("SELECT * from users").Exec()
	test.Fatal(t, err, nil)
	test.Fatal(t, result != nil, true)
}
Exemplo n.º 7
0
func TestConnectionArray(t *testing.T) {
	connection := GetConnection(t)
	LoadFixtures(connection)
	var users []*AppUser
	err := connection.Query("SELECT * FROM users WHERE name IN (?,?)", []interface{}{"John Doe", "Jane Doe"}...).GetResults(&users)
	test.Fatal(t, err, nil)
	test.Fatal(t, len(users), 2)
}
Exemplo n.º 8
0
func SubTestDocumentManager_FindOne(dm mongo.DocumentManager, t *testing.T) bson.ObjectId {
	user := new(User)
	err := dm.FindOne(bson.M{"Name": "John"}, user)
	test.Fatal(t, err, nil)
	test.Fatal(t, user.Role != nil, true)
	test.Fatal(t, user.Role.Title, "Editor")
	return user.ID
}
Exemplo n.º 9
0
func SubTestDocumentManager_FindAll(dm mongo.DocumentManager, t *testing.T) {
	users := []*User{}
	err := dm.FindAll(&users)
	test.Fatal(t, err, nil)
	test.Fatal(t, len(users), 1)
	test.Fatal(t, len(users[0].Posts), 1)
	test.Fatal(t, users[0].Posts[0].Title, "First Post Title")
	test.Fatal(t, users[0].Role != nil, true)
}
Exemplo n.º 10
0
func TestUpdate(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Update("users", "u").
		Set("u.foo", "?").
		Set("u.bar", "?")
	test.Fatal(t, qb.GetType(), db.Update)
	test.Fatal(t, qb.String(), "UPDATE users u SET u.foo = ?, u.bar = ?")
}
Exemplo n.º 11
0
func TestGetState(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	test.Fatal(t, qb.GetState(), db.Clean)
	qb.Select("u.*").From("users", "u")
	test.Fatal(t, qb.GetState(), db.Dirty)
	qb.String()
	test.Fatal(t, qb.GetState(), db.Clean)
}
Exemplo n.º 12
0
func TestSQLTagBuilder(t *testing.T) {
	tag := "column:nick_name"
	sqlTag := db.SQLStructTagBuilder{logger.NewTestLogger(t)}.BuildFromString(tag)
	test.Fatal(t, sqlTag.ColumnName, "nick_name")
	test.Fatal(t, sqlTag.PersistZeroValue, false)
	tag = "column:email_address,persistzerovalue"
	sqlTag = db.SQLStructTagBuilder{logger.NewTestLogger(t)}.BuildFromString(tag)
	test.Fatal(t, sqlTag.ColumnName, "email_address")
	test.Fatal(t, sqlTag.PersistZeroValue, true)
}
Exemplo n.º 13
0
func TestInsertValuesSetValue(t *testing.T) {
	connection := NewTestConnection(t)

	qb := db.NewQueryBuilder(connection)
	qb.Insert("users").
		SetValue("foo", "?").
		SetValue("bar", "?")
	test.Fatal(t, db.Insert, qb.GetType())
	test.Fatal(t, "INSERT INTO users (foo, bar) VALUES(?, ?)", qb.String())
}
Exemplo n.º 14
0
func TestDocumentManager_CreateQuery_Select(t *testing.T) {
	t.Log("QueryBuilder.Select should also affect which relations are resolved")
	dm, done := getDocumentManager(t)
	defer done()
	err := dm.RegisterMany(map[string]interface{}{
		"Client":   new(Client),
		"Employee": new(Employee),
		"Project":  new(Project),
	})
	test.Fatal(t, err, nil)
	client := &Client{Name: "Example"}
	employee := &Employee{Name: "John Doe"}
	project := &Project{Title: "Project", Employee: employee}
	client.Projects = append(client.Projects, project)
	dm.Persist(client)
	dm.Persist(employee)
	dm.Persist(project)
	err = dm.Flush()
	test.Fatal(t, err, nil)
	projects := []*Project{}
	err = dm.CreateQuery().Select(bson.M{"Client": 1}).All(&projects)
	test.Fatal(t, err, nil)
	test.Fatal(t, projects[0].Employee == nil, true)
	test.Fatal(t, projects[0].Client.Name, "Example")
	client = new(Client)
	err = dm.CreateQuery().Select(bson.M{"Name": 1}).One(client)
	test.Fatal(t, err, nil)
	test.Fatal(t, len(client.Projects), 0)
	err = dm.FindID(client.ID, client)
	test.Fatal(t, err, nil)
	test.Fatal(t, len(client.Projects), 1)
}
Exemplo n.º 15
0
func TestInjector_Resolve_CircularDependency(t *testing.T) {
	container := injector.NewInjector()
	container.SetLogger(test.NewTestLogger(t))
	err := container.RegisterFactory(func(i int) (int, error) {
		return i, nil
	})
	test.Fatal(t, err, nil)
	var i int
	err = container.Resolve(&i)
	test.Fatal(t, err, injector.ErrorCircularDependency)
}
Exemplo n.º 16
0
func TestInjector_Call(t *testing.T) {
	container := injector.NewInjector()
	container.SetLogger(test.NewTestLogger(t))
	container.RegisterValue(10)
	container.RegisterValue("foo")
	var result1 string
	err := container.Call(func(a int, b string) string {
		return fmt.Sprint(a, b)
	}, &result1)
	test.Fatal(t, err, nil)
	test.Fatal(t, result1, "10foo")
}
Exemplo n.º 17
0
func TestConnectionCreateQueryBuilderPrepareExec(t *testing.T) {
	connection := GetConnection(t)
	user := &AppUser{Name: "robert", Email: "*****@*****.**"}
	result, err := connection.CreateQueryBuilder().
		Insert("users").
		SetValue("name", "?").
		SetValue("email", "?").
		Prepare().Exec(user.Name, user.Email)
	test.Fatal(t, err, nil)
	lastInsertedID, err := result.LastInsertId()
	test.Fatal(t, err, nil)
	test.Fatal(t, lastInsertedID, int64(1))
}
Exemplo n.º 18
0
func TestConnectionGet(t *testing.T) {
	connection := db.NewConnection(GetDB(t))
	err := LoadFixtures(connection)
	test.Fatal(t, err, nil)
	user := new(AppUser)
	err = connection.QueryRow("SELECT name as Name,email as Email from users ;").GetResult(user)
	test.Fatal(t, err, nil)
	test.Fatal(t, user.Name, "John Doe")
	user2 := AppUser{}
	err = connection.QueryRow("SELECT * from users ;").GetResult(user2)
	test.Fatal(t, err, db.ErrNotAPointer)

}
Exemplo n.º 19
0
func TestRowGetResult(t *testing.T) {
	connection := db.NewConnection(GetDB(t))
	err := LoadFixtures(connection)
	test.Fatal(t, err, nil)
	result := map[string]interface{}{}
	err = connection.CreateQueryBuilder().
		Select("u.*").
		From("users", "u").
		Where("u.name = ?").
		QueryRow("John Doe").
		GetResult(&result)
	test.Fatal(t, err, nil)
	test.Fatal(t, result["email"], "*****@*****.**")
}
Exemplo n.º 20
0
func TestInjector_Resolve_Factory(t *testing.T) {
	container := injector.NewInjector()
	container.SetLogger(test.NewTestLogger(t))
	err := container.RegisterFactory(func(a int, b string) (string, error) {
		return fmt.Sprint(a, b), nil
	}, "service")
	test.Fatal(t, err, nil)
	container.RegisterValue(5)
	container.RegisterValue("bar")
	var result string
	err = container.Resolve(&result, "service")
	test.Fatal(t, err, nil)
	test.Fatal(t, result, "5bar")

}
Exemplo n.º 21
0
func TestInjector_Resolve_Parent(t *testing.T) {
	type Person struct{ Name string }
	container := injector.NewInjector()
	container.SetLogger(test.NewTestLogger(t))
	container.RegisterValue("foo")
	childContainer := container.CreateChild()
	err := childContainer.RegisterFactory(func(name string) (Person, error) {
		return Person{name}, nil
	})
	test.Fatal(t, err, nil)
	person := &Person{}
	err = childContainer.Resolve(person)
	test.Fatal(t, err, nil)
	test.Fatal(t, person.Name, "foo")
}
Exemplo n.º 22
0
func TestSelectAllWithoutTableAlias(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("*").
		From("users")
	test.Fatal(t, qb.String(), "SELECT * FROM users")
}
Exemplo n.º 23
0
func TestBuilderSelect(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.id").
		From("users", "u")
	test.Fatal(t, qb.String(), "SELECT u.id FROM users u")
}
Exemplo n.º 24
0
func TestConnectionSelect(t *testing.T) {
	connection := db.NewConnection(GetDB(t))
	result, err := connection.Exec("INSERT INTO users(name,email) values('john doe','*****@*****.**'),('jane doe','*****@*****.**');")
	test.Fatal(t, err, nil)
	r, err := result.RowsAffected()
	test.Fatal(t, err, nil)
	test.Fatal(t, r, int64(2), "2 records should have been created")
	t.Log(result.LastInsertId())

	// test query
	users := []*AppUser{}
	err = connection.Query("SELECT users.name as Name, users.email as Email from users ORDER BY users.id ASC ;").GetResults(&users)
	test.Fatal(t, err, nil)
	t.Logf("%#v", users)
	test.Fatal(t, users[0].Name, "john doe")
}
Exemplo n.º 25
0
func TestMakeIndexOfErrors(t *testing.T) {
	var IndexOfInts func([]int, int) int
	err := funcs.MakeIndexOf(IndexOfInts)
	test.Fatal(t, err, funcs.ErrNotAPointer)
	err = funcs.MakeIndexOf(&struct{}{})
	test.Fatal(t, err, funcs.ErrNotAFunction)
	var IndexOfString func([]string, int) int
	err = funcs.MakeIndexOf(&IndexOfString)
	test.Fatal(t, err, funcs.ErrUnexpectedType)
	var IndexOfByte func([]byte, byte) string
	err = funcs.MakeIndexOf(&IndexOfByte)
	test.Fatal(t, err, funcs.ErrUnexpectedType)
	var IndexOfArray func([][]string, []string) int
	err = funcs.MakeIndexOf(&IndexOfArray)
	test.Fatal(t, err, funcs.ErrNoComparableType)
}
Exemplo n.º 26
0
func TestBuilderSelectWithJoin(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.*", "p.*").From("users", "u").
		Join("u", "phones", "p", Eq("p.user_id", "u.id"))
	test.Fatal(t, qb.String(), "SELECT u.*, p.* FROM users u JOIN phones p ON p.user_id = u.id")

}
Exemplo n.º 27
0
func TestBuilderSelectWithWhere(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.id").
		From("users", "u").
		Where(And(Eq("u.nickname", "?")))
	test.Fatal(t, qb.String(), "SELECT u.id FROM users u WHERE u.nickname = ?")
}
Exemplo n.º 28
0
func TestDeleteWhere(t *testing.T) {
	connection := NewTestConnection(t)

	qb := db.NewQueryBuilder(connection)
	qb.Delete("users", "u").
		Where("u.foo = ?")
	test.Fatal(t, "DELETE FROM users u WHERE u.foo = ?", qb.String())
}
Exemplo n.º 29
0
func TestUpdateWhere(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Update("users", "u").
		Set("u.foo", "?").
		Where("u.foo = ?")
	test.Fatal(t, qb.String(), "UPDATE users u SET u.foo = ? WHERE u.foo = ?")
}
Exemplo n.º 30
0
func TestUpdateWithoutAlias(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Update("users").
		Set("foo", "?").
		Set("bar", "?")
	test.Fatal(t, qb.String(), "UPDATE users SET foo = ?, bar = ?")
}