Exemple #1
0
func TestMysqlLikeSearch(t *testing.T) {
	initORM()
	o := orm.NewOrm()
	user1 := models.User{Uid: utils.GenUid(), Token: utils.GenToken(),
		CreateAt: utils.GetTimeMillis(), Nickname: "张三李四", UpdateAt: utils.GetTimeMillis() + 5}
	_, err := o.Insert(&user1)
	assert.Nil(t, err)
	user2 := models.User{Uid: utils.GenUid(), Token: utils.GenToken(),
		CreateAt: utils.GetTimeMillis() + 10, Nickname: "张李三四", UpdateAt: utils.GetTimeMillis() + 15}
	_, err = o.Insert(&user2)
	assert.Nil(t, err)

	var users *[]models.User
	users = new([]models.User)

	num, err := o.QueryTable(new(models.User)).Filter("Nickname__contains", "张三").All(users)

	assert.Nil(t, err)
	assert.Equal(t, int64(1), num)
	assertUserEquals(t, &user1, &((*users)[0]))

	deleteUser(t, user1.Id)
	deleteUser(t, user2.Id)
}
Exemple #2
0
func CreateUserByPhone(phone *string, secret string) (*User, int) {
	utils.AssertNotEmptyString(phone)
	o := orm.NewOrm()
	password := utils.Secret2Password("phone:"+*phone, secret)
	user := User{Phone: phone, Password: password}

	var err error
	for i := 0; i < DB_UNIQUE_CONFLICT_TRY; i++ {
		user.Uid = utils.GenUid()
		user.Token = utils.GenToken()
		now := utils.GetTimeMillis()
		user.CreateAt = now
		user.UpdateAt = now
		_, err = o.Insert(&user)
		if err == nil {
			return &user, 0
		}
		time.Sleep(1 * time.Millisecond)
	}

	beego.Warning("CreateUser fail: ", err)
	return nil, utils.ERROR_CODE_SYSTEM_ERROR
}
Exemple #3
0
// callee's duty to commit & rollback
func createOrUpdateUserInternal(o *orm.Ormer, user *User, authUser *utils.AuthUserInfo, logTag string, platform int) (*User, int) {
	if user != nil {
		if utils.IsLegalRestrictedStringWithLength(authUser.Nickname, utils.USER_NICKNAME_MEX_LEN) {
			user.Nickname = authUser.Nickname
		}
		user.Avatar = authUser.Avatar
		user.Gender = authUser.Gender
		switch platform {
		case utils.SNS_PLATFORM_WEIXIN:
			user.WeiXinNickName = authUser.Nickname
		case utils.SNS_PLATFORM_WEIBO:
			user.WeiBoNickName = authUser.Nickname
		case utils.SNS_PLATFORM_QQ:
			user.QQNickName = authUser.Nickname
		}

		var err int
		for i := 0; i < DB_UNIQUE_CONFLICT_TRY; i++ {
			user.Token = utils.GenToken()
			user.UpdateAt = utils.GetTimeMillis()
			err = updateUserInternal(o, user)
			if err == 0 {
				return user, 0
			}
			time.Sleep(1 * time.Millisecond)
		}

		beego.Warning(logTag, err)
		return nil, utils.ERROR_CODE_SYSTEM_ERROR
	}

	user = &User{Gender: authUser.Gender, Avatar: authUser.Avatar}
	if utils.IsLegalRestrictedStringWithLength(authUser.Nickname, utils.USER_NICKNAME_MEX_LEN) {
		user.Nickname = authUser.Nickname
	}
	switch platform {
	case utils.SNS_PLATFORM_WEIXIN:
		user.WeiXin = &authUser.Openid
		user.WeiXinNickName = authUser.Nickname
	case utils.SNS_PLATFORM_WEIBO:
		user.WeiBo = &authUser.Openid
		user.WeiBoNickName = authUser.Nickname
	case utils.SNS_PLATFORM_QQ:
		user.QQ = &authUser.Openid
		user.QQNickName = authUser.Nickname
	}

	var err error
	for i := 0; i < DB_UNIQUE_CONFLICT_TRY; i++ {
		user.Uid = utils.GenUid()
		user.Token = utils.GenToken()
		now := utils.GetTimeMillis()
		user.CreateAt = now
		user.UpdateAt = now
		_, err = (*o).Insert(user)
		if err == nil {
			return user, 0
		}
		time.Sleep(1 * time.Millisecond)
	}

	beego.Warning(logTag, err)
	return nil, utils.ERROR_CODE_SYSTEM_ERROR
}
func TestGenUid(t *testing.T) {
	uid := utils.GenUid()

	assert.True(t, uid >= utils.USER_MIN_UID)
}