Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amanteaux committed Sep 19, 2024
1 parent a6d6830 commit ff67d6d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,62 +1,75 @@
package com.coreoz.plume.db.querydsl.pagination;

import com.carlosbecker.guice.GuiceModules;
import com.carlosbecker.guice.GuiceTestRunner;
import com.coreoz.plume.db.pagination.Page;
import com.coreoz.plume.db.pagination.Slice;
import com.coreoz.plume.db.querydsl.DbQuerydslTestModule;
import com.coreoz.plume.db.querydsl.db.QUser;
import com.coreoz.plume.db.querydsl.db.User;
import com.coreoz.plume.db.querydsl.db.UserDao;
import com.coreoz.plume.db.querydsl.transaction.TransactionManagerQuerydsl;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.querydsl.core.types.Order;
import org.junit.Test;
import org.junit.runner.RunWith;

import jakarta.inject.Inject;

import static org.assertj.core.api.Assertions.assertThat;

/**
* file V2__add_users.sql
* 10 users were added
*/
@RunWith(GuiceTestRunner.class)
@GuiceModules(DbQuerydslTestModule.class)
public class SqlPaginatedQueryTest {

@Inject
TransactionManagerQuerydsl transactionManagerQuerydsl;
static final int USER_COUNT = 100;

static TransactionManagerQuerydsl transactionManagerQuerydsl;

static {
Injector injector = Guice.createInjector(new DbQuerydslTestModule());
transactionManagerQuerydsl = injector.getInstance(TransactionManagerQuerydsl.class);
UserDao userDao = injector.getInstance(UserDao.class);
long userCountToInsert = transactionManagerQuerydsl.selectQuery().select(QUser.user).from(QUser.user).fetchCount();
if (userCountToInsert > USER_COUNT) {
throw new IllegalStateException("There is already "+ userCountToInsert +" users, which is more than " + USER_COUNT + ", USER_COUNT should be increased");
}
for (int i = 0; i < (USER_COUNT - userCountToInsert); i++) {
User user = new User();
user.setName("Page user");
userDao.save(user);
}
}

@Test
public void fetch_page_with_correct_pagination_should_paginate_users() {
int pageSize = 10;
Page<User> page = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
.withSort(QUser.user.name, Order.DESC)
.fetchPage(1, 10);
.fetchPage(1, pageSize);

assertThat(page.pagesCount()).isEqualTo(1);
assertThat(page.totalCount()).isEqualTo(10);
assertThat(page.items()).hasSize(10);
assertThat(page.hasMore()).isFalse();
assertThat(page.pagesCount()).isEqualTo(USER_COUNT / pageSize);
assertThat(page.totalCount()).isEqualTo(USER_COUNT);
assertThat(page.items()).hasSize(pageSize);
assertThat(page.hasMore()).isTrue();
}

@Test
public void fetch_page_with_wrong_pagination_should_return_empty_items() {
int pageSize = 10;
Page<User> page = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
.withSort(QUser.user.name, Order.DESC)
.fetchPage(2, 10);
.fetchPage(11, pageSize);

assertThat(page.pagesCount()).isEqualTo(1);
assertThat(page.totalCount()).isEqualTo(10);
assertThat(page.pagesCount()).isEqualTo(USER_COUNT / pageSize);
assertThat(page.totalCount()).isEqualTo(USER_COUNT);
assertThat(page.items()).isEmpty();
assertThat(page.hasMore()).isFalse();
}
Expand All @@ -65,15 +78,15 @@ public void fetch_page_with_wrong_pagination_should_return_empty_items() {
public void fetch_page_with_minimum_page_and_page_size_should_return_results() {
Page<User> page = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
.withSort(QUser.user.name, Order.ASC)
.fetchPage(1, 1); // Minimum page number and page size

assertThat(page.pagesCount()).isEqualTo(10);
assertThat(page.totalCount()).isEqualTo(10);
assertThat(page.pagesCount()).isEqualTo(USER_COUNT);
assertThat(page.totalCount()).isEqualTo(USER_COUNT);
assertThat(page.items()).hasSize(1); // Only one item expected due to page size of 1
assertThat(page.hasMore()).isTrue(); // Has more items since page size is small
}
Expand All @@ -82,23 +95,24 @@ public void fetch_page_with_minimum_page_and_page_size_should_return_results() {
public void fetch_page_with_page_size_larger_than_total_results_should_return_all() {
Page<User> page = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
.withSort(QUser.user.name, Order.ASC)
.fetchPage(1, 100); // Large page size compared to available results
.fetchPage(1, USER_COUNT + 1); // Large page size compared to available results

assertThat(page.totalCount()).isEqualTo(10);
assertThat(page.items()).hasSize(10); // Should return all available users
assertThat(page.pagesCount()).isEqualTo(1);
assertThat(page.totalCount()).isEqualTo(USER_COUNT);
assertThat(page.items()).hasSize(USER_COUNT); // Should return all available users
assertThat(page.hasMore()).isFalse(); // No more items because page size exceeds total results
}

@Test(expected = IllegalArgumentException.class)
public void fetch_page_with_invalid_negative_page_number_should_throw_exception() {
SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
Expand All @@ -110,7 +124,7 @@ public void fetch_page_with_invalid_negative_page_number_should_throw_exception(
public void fetch_page_with_invalid_negative_page_size_should_throw_exception() {
SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
Expand All @@ -120,25 +134,26 @@ public void fetch_page_with_invalid_negative_page_size_should_throw_exception()

@Test
public void fetch_page_without_sorting_should_paginate_users() {
int pageSize = 10;
Page<User> page = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
.fetchPage(1, 10); // No sorting applied
.fetchPage(1, pageSize); // No sorting applied

assertThat(page.pagesCount()).isEqualTo(1);
assertThat(page.totalCount()).isEqualTo(10);
assertThat(page.items()).hasSize(10);
assertThat(page.hasMore()).isFalse(); // Assuming there aren't more than 10 users in the test
assertThat(page.pagesCount()).isEqualTo(USER_COUNT / pageSize);
assertThat(page.totalCount()).isEqualTo(USER_COUNT);
assertThat(page.items()).hasSize(pageSize);
assertThat(page.hasMore()).isTrue();
}

@Test
public void fetch_page_with_empty_query_should_return_no_results() {
Page<User> page = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
.where(QUser.user.name.eq("Non-existent user")) // Query with no matches
Expand All @@ -154,7 +169,7 @@ public void fetch_page_with_empty_query_should_return_no_results() {
public void fetch_slice_with_minimum_page_and_page_size_should_return_results() {
Slice<User> slice = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
Expand All @@ -169,7 +184,7 @@ public void fetch_slice_with_minimum_page_and_page_size_should_return_results()
public void fetch_slice_with_empty_query_should_return_no_results() {
Slice<User> slice = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
.where(QUser.user.name.eq("Non-existent user")) // Query with no matches
Expand All @@ -184,27 +199,27 @@ public void fetch_slice_with_empty_query_should_return_no_results() {
public void fetch_slice_with_correct_pagination_should_slice_users() {
Slice<User> page = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
.withSort(QUser.user.name, Order.ASC)
.fetchSlice(1, 50);
.fetchSlice(1, USER_COUNT + 1);

assertThat(page.items()).isNotEmpty();
assertThat(page.items()).hasSize(USER_COUNT);
assertThat(page.hasMore()).isFalse();
}

@Test
public void fetch_slice_with_wrong_pagination_should_return_empty_items() {
Slice<User> page = SqlPaginatedQuery
.fromQuery(
this.transactionManagerQuerydsl.selectQuery()
transactionManagerQuerydsl.selectQuery()
.select(QUser.user)
.from(QUser.user)
)
.withSort(QUser.user.name, Order.ASC)
.fetchSlice(2, 50);
.fetchSlice(2, USER_COUNT);

assertThat(page.items()).isEmpty();
assertThat(page.hasMore()).isFalse();
Expand Down

This file was deleted.

0 comments on commit ff67d6d

Please sign in to comment.