Search in sources :

Example 1 with MockResultSet

use of org.seasar.doma.internal.jdbc.mock.MockResultSet in project doma by domaframework.

the class BuiltinIdentityIdGeneratorTest method test_identityReservationSql.

@Test
public void test_identityReservationSql() {
    MockConfig config = new MockConfig();
    config.setDialect(new PostgresDialect());
    MockResultSet resultSet = config.dataSource.connection.preparedStatement.resultSet;
    resultSet.rows.add(new RowData(11L));
    resultSet.rows.add(new RowData(12L));
    resultSet.rows.add(new RowData(13L));
    EntityType<IdGeneratedEmp> entityType = _IdGeneratedEmp.getSingletonInternal();
    BuiltinIdentityIdGenerator identityIdGenerator = new BuiltinIdentityIdGenerator();
    IdGenerationConfig idGenerationConfig = new IdGenerationConfig(config, entityType, new ReservedIdProvider(config, entityType, 3));
    Long value = identityIdGenerator.generatePreInsert(idGenerationConfig);
    assertEquals(new Long(11), value);
    assertEquals("select nextval(pg_catalog.pg_get_serial_sequence('\"CATA\".\"EMP\"', 'id')) from generate_series(1, 3)", config.dataSource.connection.preparedStatement.sql);
    value = identityIdGenerator.generatePreInsert(idGenerationConfig);
    assertEquals(new Long(12), value);
    value = identityIdGenerator.generatePreInsert(idGenerationConfig);
    assertEquals(new Long(13), value);
    try {
        identityIdGenerator.generatePreInsert(idGenerationConfig);
        fail();
    } catch (IllegalStateException e) {
        System.out.println(e.getMessage());
    }
}
Also used : RowData(org.seasar.doma.internal.jdbc.mock.RowData) example.entity._IdGeneratedEmp(example.entity._IdGeneratedEmp) IdGeneratedEmp(example.entity.IdGeneratedEmp) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) PostgresDialect(org.seasar.doma.jdbc.dialect.PostgresDialect) Test(org.junit.jupiter.api.Test)

Example 2 with MockResultSet

use of org.seasar.doma.internal.jdbc.mock.MockResultSet in project doma by domaframework.

the class BuiltinTableIdGeneratorTest method test.

@Test
public void test() {
    MockConfig config = new MockConfig();
    config.setDialect(new PostgresDialect());
    MockConnection connection = new MockConnection();
    MockConnection connection2 = new MockConnection();
    MockResultSet resultSet2 = connection2.preparedStatement.resultSet;
    resultSet2.rows.add(new RowData(11L));
    final LinkedList<MockConnection> connections = new LinkedList<>();
    connections.add(connection);
    connections.add(connection2);
    config.dataSource = new MockDataSource() {

        @Override
        public Connection getConnection() {
            return connections.pop();
        }
    };
    BuiltinTableIdGenerator idGenerator = new BuiltinTableIdGenerator();
    idGenerator.setQualifiedTableName("aaa");
    idGenerator.setPkColumnName("PK");
    idGenerator.setPkColumnValue("EMP_ID");
    idGenerator.setValueColumnName("VALUE");
    idGenerator.setInitialValue(1);
    idGenerator.setAllocationSize(1);
    idGenerator.initialize();
    IdGenerationConfig idGenerationConfig = new IdGenerationConfig(config, _IdGeneratedEmp.getSingletonInternal());
    Long value = idGenerator.generatePreInsert(idGenerationConfig);
    assertEquals(new Long(10), value);
    assertEquals("update aaa set VALUE = VALUE + ? where PK = ?", connection.preparedStatement.sql);
    assertEquals(2, connection.preparedStatement.bindValues.size());
    assertEquals("select VALUE from aaa where PK = ?", connection2.preparedStatement.sql);
    assertEquals(1, connection2.preparedStatement.bindValues.size());
}
Also used : Connection(java.sql.Connection) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) LinkedList(java.util.LinkedList) RowData(org.seasar.doma.internal.jdbc.mock.RowData) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) PostgresDialect(org.seasar.doma.jdbc.dialect.PostgresDialect) Test(org.junit.jupiter.api.Test)

Example 3 with MockResultSet

use of org.seasar.doma.internal.jdbc.mock.MockResultSet in project doma by domaframework.

the class ScalarProviderTest method testBasic.

@Test
public void testBasic() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("aaa"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("hoge"));
    resultSet.next();
    ScalarProvider<String, String> provider = new ScalarProvider<>(() -> new org.seasar.doma.internal.jdbc.scalar.BasicScalar<>(StringWrapper::new), new MySelectQuery());
    String result = provider.get(resultSet);
    assertEquals("hoge", result);
}
Also used : RowData(org.seasar.doma.internal.jdbc.mock.RowData) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 4 with MockResultSet

use of org.seasar.doma.internal.jdbc.mock.MockResultSet in project doma by domaframework.

the class ScalarProviderTest method testOptionalBasic.

@Test
public void testOptionalBasic() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("aaa"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("hoge"));
    resultSet.next();
    ScalarProvider<String, Optional<String>> provider = new ScalarProvider<>(() -> new org.seasar.doma.internal.jdbc.scalar.OptionalBasicScalar<>(StringWrapper::new), new MySelectQuery());
    Optional<String> result = provider.get(resultSet);
    assertEquals("hoge", result.get());
}
Also used : Optional(java.util.Optional) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) RowData(org.seasar.doma.internal.jdbc.mock.RowData) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 5 with MockResultSet

use of org.seasar.doma.internal.jdbc.mock.MockResultSet in project doma by domaframework.

the class ScalarProviderTest method testDomain.

@Test
public void testDomain() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("aaa"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("hoge"));
    resultSet.next();
    DomainType<String, PhoneNumber> domainType = _PhoneNumber.getSingletonInternal();
    ScalarProvider<String, PhoneNumber> provider = new ScalarProvider<>(domainType::createScalar, new MySelectQuery());
    PhoneNumber result = provider.get(resultSet);
    assertEquals("hoge", result.getValue());
}
Also used : RowData(org.seasar.doma.internal.jdbc.mock.RowData) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) PhoneNumber(example.domain.PhoneNumber) example.domain._PhoneNumber(example.domain._PhoneNumber) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)28 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)28 RowData (org.seasar.doma.internal.jdbc.mock.RowData)27 ColumnMetaData (org.seasar.doma.internal.jdbc.mock.ColumnMetaData)24 MockResultSetMetaData (org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData)24 SqlFileSelectQuery (org.seasar.doma.jdbc.query.SqlFileSelectQuery)17 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)14 Emp (example.entity.Emp)12 example.entity._Emp (example.entity._Emp)12 Method (java.lang.reflect.Method)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 TestInfo (org.junit.jupiter.api.TestInfo)8 SqlFileUtil (org.seasar.doma.internal.jdbc.util.SqlFileUtil)8 SqlLogType (org.seasar.doma.jdbc.SqlLogType)8 BigDecimal (java.math.BigDecimal)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 NonUniqueResultException (org.seasar.doma.jdbc.NonUniqueResultException)6 PhoneNumber (example.domain.PhoneNumber)5 example.domain._PhoneNumber (example.domain._PhoneNumber)5 MockConnection (org.seasar.doma.internal.jdbc.mock.MockConnection)5