Пример #1
0
func Test_Pagination(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		for _, name := range []string{"Mark", "Joe", "Jane"} {
			user := User{Name: nulls.NewString(name)}
			err := tx.Create(&user)
			a.NoError(err)
		}

		u := Users{}
		q := tx.Paginate(1, 2)
		err := q.All(&u)
		a.NoError(err)
		a.Equal(len(u), 2)

		p := q.Paginator
		a.Equal(p.CurrentEntriesSize, 2)
		a.Equal(p.TotalEntriesSize, 3)
		a.Equal(p.TotalPages, 2)

		u = Users{}
		err = tx.Where("name = 'Mark'").All(&u)
		a.NoError(err)
		a.Equal(reflect.ValueOf(&u).Elem().Len(), 1)
	})
}
Пример #2
0
func Benchmark_Create_Pop(b *testing.B) {
	transaction(func(tx *pop.Connection) {
		for n := 0; n < b.N; n++ {
			u := &User{
				Name: nulls.NewString("Mark Bates"),
			}
			tx.Create(u)
		}
	})
}
Пример #3
0
func Test_Last(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		first := User{Name: nulls.NewString("Mark")}
		err := tx.Create(&first)
		a.NoError(err)

		last := User{Name: nulls.NewString("Mark")}
		err = tx.Create(&last)
		a.NoError(err)

		u := User{}
		err = tx.Where("name = 'Mark'").Last(&u)
		a.NoError(err)

		a.Equal(last.ID, u.ID)
	})
}
Пример #4
0
func Benchmark_Create_Raw(b *testing.B) {
	transaction(func(tx *pop.Connection) {
		for n := 0; n < b.N; n++ {
			u := &User{
				Name: nulls.NewString("Mark Bates"),
			}
			q := "INSERT INTO users (alive, bio, birth_date, created_at, name, price, updated_at) VALUES (:alive, :bio, :birth_date, :created_at, :name, :price, :updated_at)"
			tx.Store.NamedExec(q, u)
		}
	})
}
Пример #5
0
func Benchmark_Find_Raw(b *testing.B) {
	transaction(func(tx *pop.Connection) {
		u := &User{
			Name: nulls.NewString("Mark Bates"),
		}
		tx.Create(u)
		for n := 0; n < b.N; n++ {
			tx.Store.Get(u, "select * from users where id = ?", u.ID)
		}
	})
}
Пример #6
0
func Test_Count(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		user := User{Name: nulls.NewString("Mark")}
		err := tx.Create(&user)
		a.NoError(err)
		c, err := tx.Count(&user)
		a.NoError(err)
		a.Equal(c, 1)
	})
}
Пример #7
0
func Test_Save(t *testing.T) {
	r := require.New(t)
	transaction(func(tx *pop.Connection) {
		u := &User{Name: nulls.NewString("Mark")}
		r.Zero(u.ID)
		tx.Save(u)
		r.NotZero(u.ID)

		uat := u.UpdatedAt.UnixNano()

		tx.Save(u)
		r.NotEqual(uat, u.UpdatedAt.UnixNano())
	})
}
Пример #8
0
func Test_Exists(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		t, _ := tx.Where("id = ?", 0).Exists("users")
		a.False(t)

		user := User{Name: nulls.NewString("Mark")}
		err := tx.Create(&user)
		a.NoError(err)

		t, _ = tx.Where("id = ?", user.ID).Exists("users")
		a.True(t)
	})
}
Пример #9
0
func Test_Find(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		user := User{Name: nulls.NewString("Mark")}
		err := tx.Create(&user)
		a.NoError(err)

		u := User{}
		err = tx.Find(&u, user.ID)
		a.NoError(err)

		a.NotEqual(u.ID, 0)
		a.Equal(u.Name.String, "Mark")
	})
}
Пример #10
0
func Test_Count_RawQuery(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		user := User{Name: nulls.NewString("Mark")}
		err := tx.Create(&user)
		a.NoError(err)

		c, err := tx.RawQuery("select count(*) as row_count from users as users").Count(nil)
		a.NoError(err)
		a.Equal(c, 1)

		c, err = tx.RawQuery("select count(*) as row_count from users as users where id = -1").Count(nil)
		a.NoError(err)
		a.Equal(c, 0)
	})
}
Пример #11
0
func Test_Exec(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		user := User{Name: nulls.NewString("Mark 'Awesome' Bates")}
		tx.Create(&user)

		ctx, _ := tx.Count(user)
		a.Equal(1, ctx)

		q := tx.RawQuery("delete from users where id = ?", user.ID)
		err := q.Exec()
		a.NoError(err)

		ctx, _ = tx.Count(user)
		a.Equal(0, ctx)
	})
}
Пример #12
0
func Test_Update(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		user := User{Name: nulls.NewString("Mark")}
		tx.Create(&user)

		a.NotZero(user.CreatedAt)
		a.NotZero(user.UpdatedAt)

		user.Name.String = "Marky"
		err := tx.Update(&user)
		a.NoError(err)

		tx.Reload(&user)
		a.Equal(user.Name.String, "Marky")
	})
}
Пример #13
0
func Test_Create_Timestamps(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		user := User{Name: nulls.NewString("Mark 'Awesome' Bates")}
		a.Zero(user.CreatedAt)
		a.Zero(user.UpdatedAt)

		err := tx.Create(&user)
		a.NoError(err)

		a.NotZero(user.CreatedAt)
		a.NotZero(user.UpdatedAt)

		friend := Friend{FirstName: "Ross", LastName: "Gellar"}
		err = tx.Create(&friend)
		a.NoError(err)
	})
}
Пример #14
0
func Test_Count_Disregards_Pagination(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		names := []string{
			"Jack",
			"Hurley",
			"Charlie",
			"Desmond",
			"Juliet",
			"Locke",
			"Sawyer",
			"Kate",
			"Benjamin Linus",
		}

		for _, name := range names {
			user := User{Name: nulls.NewString(name)}
			err := tx.Create(&user)
			a.NoError(err)
		}

		first_users := Users{}
		second_users := Users{}

		q := tx.Paginate(1, 3)
		q.All(&first_users)

		a.Equal(3, len(first_users))
		totalFirstPage := q.Paginator.TotalPages

		q = tx.Paginate(2, 3)
		q.All(&second_users)

		a.Equal(3, len(second_users))
		totalSecondPage := q.Paginator.TotalPages

		a.NotEqual(0, totalFirstPage)
		a.NotEqual(0, totalSecondPage)
		a.Equal(totalFirstPage, totalSecondPage)
	})
}
Пример #15
0
func Test_Destroy(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		count, err := tx.Count("users")
		user := User{Name: nulls.NewString("Mark")}
		err = tx.Create(&user)
		a.NoError(err)
		a.NotEqual(user.ID, 0)

		ctx, err := tx.Count("users")
		a.Equal(count+1, ctx)

		err = tx.Destroy(&user)
		a.NoError(err)

		ctx, _ = tx.Count("users")
		a.Equal(count, ctx)
	})
}
Пример #16
0
func Test_Create(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		count, _ := tx.Count(&User{})
		user := User{Name: nulls.NewString("Mark 'Awesome' Bates")}
		err := tx.Create(&user)
		a.NoError(err)
		a.NotEqual(user.ID, 0)

		ctx, _ := tx.Count(&User{})
		a.Equal(count+1, ctx)

		u := User{}
		q := tx.Where("name = ?", "Mark 'Awesome' Bates")
		err = q.First(&u)
		a.NoError(err)
		a.Equal(user.Name.String, "Mark 'Awesome' Bates")
	})
}
Пример #17
0
func Test_All(t *testing.T) {
	transaction(func(tx *pop.Connection) {
		a := require.New(t)

		for _, name := range []string{"Mark", "Joe", "Jane"} {
			user := User{Name: nulls.NewString(name)}
			err := tx.Create(&user)
			a.NoError(err)
		}

		u := Users{}
		err := tx.All(&u)
		a.NoError(err)
		a.Equal(len(u), 3)

		u = Users{}
		err = tx.Where("name = 'Mark'").All(&u)
		a.NoError(err)
		a.Equal(len(u), 1)
	})
}