Java Code Examples for com.vladmihalcea.sql.SQLStatementCountValidator#assertInsertCount()

The following examples show how to use com.vladmihalcea.sql.SQLStatementCountValidator#assertInsertCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomerControllerITTest.java    From POC with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method for testing that gives us the customer id based on test
 * customers name. Need this as IDs will increment as tests are rerun
 * @param firstName the customer firstName.
 * @return customer Id
 */
private Long getCustomerIdByFirstName(String firstName) {
	SQLStatementCountValidator.reset();
	long customerId = this.customerRepository.findOptionalIdByFirstName(firstName).orElse(0L);
	SQLStatementCountValidator.assertInsertCount(0);
	SQLStatementCountValidator.assertUpdateCount(0);
	SQLStatementCountValidator.assertDeleteCount(0);
	SQLStatementCountValidator.assertSelectCount(1);
	return customerId;
}
 
Example 2
Source File: HibernateSQLStatementCountTest.java    From vladmihalcea.wordpress.com with Apache License 2.0 4 votes vote down vote up
@Test
public void test() {

    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {

            Company company = new Company();
            company.setName("TV Company");
            entityManager.persist(company);

            Product product1 = new Product("tvCode");
            product1.setName("TV");
            product1.setCompany(company);

            WarehouseProductInfo warehouseProductInfo1 = new WarehouseProductInfo();
            warehouseProductInfo1.setQuantity(101);
            product1.addWarehouse(warehouseProductInfo1);

            Product product2 = new Product("cdCode");
            product2.setName("CD");
            product2.setCompany(company);

            WarehouseProductInfo warehouseProductInfo2 = new WarehouseProductInfo();
            warehouseProductInfo2.setQuantity(50);
            product2.addWarehouse(warehouseProductInfo2);

            entityManager.persist(product1);
            entityManager.persist(product2);
            entityManager.flush();

            final JdbcTemplate otherDataSourceJdbcTemplate = new JdbcTemplate(otherDataSource);
            List<Map<String, Object>> versions = otherDataSourceJdbcTemplate.queryForList(" select * from version ");
            assertTrue(versions.isEmpty());
            return null;
        }
    });

    try {
        SQLStatementCountValidator.reset();
        warehouseProductInfoService.findAllWithNPlusOne();
        SQLStatementCountValidator.assertSelectCount(1);
    } catch (SQLSelectCountMismatchException e) {
        assertEquals(3, e.getRecorded());
    }

    SQLStatementCountValidator.reset();
    warehouseProductInfoService.findAllWithFetch();
    SQLStatementCountValidator.assertSelectCount(1);

    SQLStatementCountValidator.reset();
    warehouseProductInfoService.newWarehouseProductInfo();
    SQLStatementCountValidator.assertSelectCount(1);
    SQLStatementCountValidator.assertInsertCount(2);
}