Пример #1
0
func TestThreadFindList(t *testing.T) {
	dbMap.TruncateTables()
	createdAt := bbstime.Now()
	updatedAt := bbstime.Now()
	threads := make(model.ThreadSlice, 10)
	for i := range threads {
		createdAt = createdAt.Add(time.Hour)
		updatedAt = updatedAt.Add(-time.Hour)
		threads[i].CreatedAt = createdAt
		threads[i].UpdatedAt = updatedAt
		if err := dbMap.Insert(&threads[i]); err != nil {
			t.Fatal(err)
		}
	}

	tests := []struct {
		paging   Paging
		expected model.ThreadSlice
	}{
		{
			paging: Paging{OrderBy: "created_at desc", Limit: 3, Offset: 0},
			expected: threads[7:].SortBy(func(t1, t2 model.Thread) bool {
				return t1.CreatedAt.After(t2.CreatedAt)
			})},
		{
			paging: Paging{OrderBy: "created_at desc", Limit: 3, Offset: 6},
			expected: threads[1:4].SortBy(func(t1, t2 model.Thread) bool {
				return t1.CreatedAt.After(t2.CreatedAt)
			})},
		{
			paging:   Paging{OrderBy: "updated_at desc", Limit: 3, Offset: 0},
			expected: threads[0:3],
		},
		{
			paging:   Paging{OrderBy: "updated_at desc", Limit: 3, Offset: 6},
			expected: threads[6:9],
		},
	}

	for _, test := range tests {
		threads, err := threadDao.FindList(test.paging)
		t.Logf("%+v", threads)
		if err != nil {
			t.Fatal(err)
		}
		assert.Equal(t, test.expected, threads, "")
	}
}
Пример #2
0
func TestThreadCreate(t *testing.T) {
	dbMap.TruncateTables()
	now := bbstime.Now()

	thread := model.Thread{
		UserId:    1,
		Title:     "thread_title",
		Body:      "thread_body",
		CreatedAt: now,
		UpdatedAt: now,
	}
	if err := threadDao.Create(&thread); err != nil {
		t.Fatal(err)
	}
	actualThread := model.Thread{}
	dbMap.SelectOne(&actualThread, "select * from threads where id = ?", thread.Id)
	assert.Equal(t, thread, actualThread, "")
}
Пример #3
0
func TestUserFindById(t *testing.T) {
	dbMap.TruncateTables()
	now := bbstime.Now()

	u1 := model.User{
		Username:    "******",
		Password:    "******",
		DisplayName: "displayName1",
		Status:      model.Member,
		CreatedAt:   now,
		UpdatedAt:   now,
	}
	if err := userDao.Create(&u1); err != nil {
		t.Fatal(err)
	}

	actualUser, err := userDao.FindById(u1.Id)
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, u1, *actualUser, "")
}
Пример #4
0
func TestUserFindByIds(t *testing.T) {
	dbMap.TruncateTables()
	now := bbstime.Now()

	u1 := model.User{
		Username:    "******",
		Password:    "******",
		DisplayName: "displayName1",
		Status:      model.Member,
		CreatedAt:   now,
		UpdatedAt:   now,
	}
	u2 := model.User{
		Username:    "******",
		Password:    "******",
		DisplayName: "displayName2",
		Status:      model.Member,
		CreatedAt:   now,
		UpdatedAt:   now,
	}
	if err := userDao.Create(&u1); err != nil {
		t.Fatal(err)
	}
	if err := userDao.Create(&u2); err != nil {
		t.Fatal(err)
	}

	actualUsers, err := userDao.FindByIds([]uint32{u1.Id, u2.Id})
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, 2, len(actualUsers), "")
	assert.Equal(t, u1, actualUsers[0], "")
	assert.Equal(t, u2, actualUsers[1], "")
}