Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Make count assertions case-insensitive regarding the count column name. Add missing license headers. Support DatabaseContainer without a database name.

See #230
  • Loading branch information
mp911de committed Mar 15, 2021
1 parent b7a7c2c commit 055baaf
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
*
* @author Mark Paluch
*/
public class SingleConnectionConnectionFactoryUnitTests {
class SingleConnectionConnectionFactoryUnitTests {

@Test // gh-204
public void shouldAllocateSameConnection() {
void shouldAllocateSameConnection() {

SingleConnectionConnectionFactory factory = new SingleConnectionConnectionFactory("r2dbc:h2:mem:///foo", false);

Expand All @@ -52,7 +52,7 @@ public void shouldAllocateSameConnection() {
}

@Test // gh-204
public void shouldApplyAutoCommit() {
void shouldApplyAutoCommit() {

SingleConnectionConnectionFactory factory = new SingleConnectionConnectionFactory("r2dbc:h2:mem:///foo", false);
factory.setAutoCommit(false);
Expand All @@ -71,7 +71,7 @@ public void shouldApplyAutoCommit() {
}

@Test // gh-204
public void shouldSuppressClose() {
void shouldSuppressClose() {

SingleConnectionConnectionFactory factory = new SingleConnectionConnectionFactory("r2dbc:h2:mem:///foo", true);

Expand All @@ -87,7 +87,7 @@ public void shouldSuppressClose() {
}

@Test // gh-204
public void shouldNotSuppressClose() {
void shouldNotSuppressClose() {

SingleConnectionConnectionFactory factory = new SingleConnectionConnectionFactory("r2dbc:h2:mem:///foo", false);

Expand All @@ -101,7 +101,7 @@ public void shouldNotSuppressClose() {
}

@Test // gh-204
public void releaseConnectionShouldCloseUnrelatedConnection() {
void releaseConnectionShouldCloseUnrelatedConnection() {

Connection connectionMock = mock(Connection.class);
Connection otherConnection = mock(Connection.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.r2dbc.dialect;

import static org.assertj.core.api.Assertions.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.r2dbc.dialect;

import static org.assertj.core.api.Assertions.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import reactor.test.StepVerifier;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -214,7 +213,7 @@ void shouldDeleteUsingQueryMethod() {
.verifyComplete();

Map<String, Object> count = jdbc.queryForMap("SELECT count(*) AS count FROM legoset");
assertThat(count).hasEntrySatisfying("count", numberOf(1));
assertThat(getCount(count)).satisfies(numberOf(1));
}

@Test // gh-335
Expand Down Expand Up @@ -293,11 +292,13 @@ public void shouldInsertItemsTransactional() {
Mono<Map<String, Object>> nonTransactional = repository.save(legoSet2) //
.map(it -> jdbc.queryForMap("SELECT count(*) AS count FROM legoset"));

transactional.as(StepVerifier::create).expectNext(Collections.singletonMap("count", 0L)).verifyComplete();
nonTransactional.as(StepVerifier::create).expectNext(Collections.singletonMap("count", 2L)).verifyComplete();
transactional.as(StepVerifier::create).assertNext(actual -> assertThat(getCount(actual)).satisfies(numberOf(0)))
.verifyComplete();
nonTransactional.as(StepVerifier::create).assertNext(actual -> assertThat(getCount(actual)).satisfies(numberOf(2)))
.verifyComplete();

Map<String, Object> count = jdbc.queryForMap("SELECT count(*) AS count FROM legoset");
assertThat(count).hasEntrySatisfying("count", numberOf(2));
Map<String, Object> map = jdbc.queryForMap("SELECT count(*) AS count FROM legoset");
assertThat(getCount(map)).satisfies(numberOf(2));
}

@Test // gh-363
Expand Down Expand Up @@ -353,6 +354,10 @@ void shouldDeleteAllAndReturnCount() {
.verifyComplete();
}

private static Object getCount(Map<String, Object> map) {
return map.getOrDefault("count", map.get("COUNT"));
}

private Condition<? super Object> numberOf(int expected) {
return new Condition<>(it -> {
return it instanceof Number && ((Number) it).intValue() == expected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public static ProvidedDatabaseBuilder builder(JdbcDatabaseContainer container) {
.port(container.getFirstMappedPort()) //
.username(container.getUsername()) //
.password(container.getPassword()) //
.database(container.getDatabaseName()) //
.jdbcUrl(container.getJdbcUrl());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static DataSource createDataSource() {
dataSource.setUsername("sa");
dataSource.setPassword("");
dataSource.setUrl("jdbc:h2:mem:r2dbc;DB_CLOSE_DELAY=-1");
dataSource.setDriverClassName("org.h2.Driver");

return dataSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabas
}

/**
* Returns a locally provided database at {@code postgres:@localhost:5432/postgres}.
* Returns a locally provided database .
*/
private static ExternalDatabase local() {

Expand All @@ -112,6 +112,7 @@ private static ExternalDatabase testContainer() {

testContainerDatabase = ProvidedDatabase.builder(container) //
.username("root") //
.database(container.getDatabaseName()) //
.build();
} catch (IllegalStateException ise) {
// docker not available.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class MySqlTestSupport {
+ ") ENGINE=InnoDB;";

/**
* Returns a database either hosted locally at {@code postgres:@localhost:5432/postgres} or running inside Docker.
* Returns a database either hosted locally or running inside Docker.
*
* @return information about the database. Guaranteed to be not {@literal null}.
*/
Expand Down Expand Up @@ -88,7 +88,7 @@ private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabas
}

/**
* Returns a locally provided database at {@code postgres:@localhost:5432/postgres}.
* Returns a locally provided database.
*/
private static ExternalDatabase local() {

Expand All @@ -114,6 +114,7 @@ private static ExternalDatabase testContainer() {
container.start();

testContainerDatabase = ProvidedDatabase.builder(container) //
.database(container.getDatabaseName()) //
.username("root") //
.build();
} catch (IllegalStateException ise) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private static ExternalDatabase testContainer() {
PostgreSQLContainer.IMAGE + ":" + PostgreSQLContainer.DEFAULT_TAG);
container.start();

testContainerDatabase = ProvidedDatabase.from(container);
testContainerDatabase = ProvidedDatabase.builder(container).database(container.getDatabaseName()).build();

} catch (IllegalStateException ise) {
// docker not available.
Expand Down

0 comments on commit 055baaf

Please sign in to comment.