Search in sources :

Example 11 with RowData

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

the class EntitySingleResultHandlerTest method testHandle.

@Test
public void testHandle() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "aaa"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    EntitySingleResultHandler<Emp> handler = new EntitySingleResultHandler<>(_Emp.getSingletonInternal());
    Emp emp = handler.handle(resultSet, query, (i, next) -> {
    }).get();
    assertEquals(1, emp.getId());
    assertEquals("aaa", emp.getName());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) SqlFileUtil(org.seasar.doma.internal.jdbc.util.SqlFileUtil) BeforeEach(org.junit.jupiter.api.BeforeEach) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) AssertionUtil.assertEquals(org.seasar.doma.internal.util.AssertionUtil.assertEquals) Emp(example.entity.Emp) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) SqlLogType(org.seasar.doma.jdbc.SqlLogType) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) example.entity._Emp(example.entity._Emp) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) Method(java.lang.reflect.Method) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 12 with RowData

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

the class BasicResultListHandlerTest method testHandle_NonSingleColumnException.

@Test
public void testHandle_NonSingleColumnException() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("x"));
    metaData.columns.add(new ColumnMetaData("y"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("aaa", "bbb"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setResultEnsured(true);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    BasicResultListHandler<String> handler = new BasicResultListHandler<>(StringWrapper::new);
    try {
        handler.handle(resultSet, query, (i, next) -> {
        });
        fail();
    } catch (NonSingleColumnException expected) {
    }
}
Also used : StringWrapper(org.seasar.doma.wrapper.StringWrapper) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) NonSingleColumnException(org.seasar.doma.jdbc.NonSingleColumnException) 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 13 with RowData

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

the class BasicSingleResultHandlerTest method testHandle_NonUniqueResultException.

@Test
public void testHandle_NonUniqueResultException() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("x"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("aaa"));
    resultSet.rows.add(new RowData("bbb"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    BasicSingleResultHandler<String> handler = new BasicSingleResultHandler<>(StringWrapper::new);
    try {
        handler.handle(resultSet, query, (i, next) -> {
        });
        fail();
    } catch (NonUniqueResultException ignore) {
    }
}
Also used : NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) StringWrapper(org.seasar.doma.wrapper.StringWrapper) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) 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 14 with RowData

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

the class SelectCommandTest method testExecute_singleResult.

@Test
public void testExecute_singleResult() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    metaData.columns.add(new ColumnMetaData("salary"));
    metaData.columns.add(new ColumnMetaData("version"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "hoge", new BigDecimal(10000), 100));
    runtimeConfig.dataSource.connection = new MockConnection(new MockPreparedStatement(resultSet));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.addParameter("name", String.class, "hoge");
    query.addParameter("salary", BigDecimal.class, new BigDecimal(10000));
    query.setMethod(getClass().getMethod(method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    SelectCommand<Emp> command = new SelectCommand<>(query, new EntitySingleResultHandler<>(_Emp.getSingletonInternal()));
    Emp entity = command.execute();
    query.complete();
    assertNotNull(entity);
    assertEquals(new Integer(1), entity.getId());
    assertEquals("hoge", entity.getName());
    assertEquals(new BigDecimal(10000), entity.getSalary());
    assertEquals(new Integer(100), entity.getVersion());
    List<BindValue> bindValues = runtimeConfig.dataSource.connection.preparedStatement.bindValues;
    BindValue bindValue = bindValues.get(0);
    assertEquals("hoge", bindValue.getValue());
    assertEquals(1, bindValue.getIndex());
    bindValue = bindValues.get(1);
    assertEquals(new BigDecimal(10000), bindValue.getValue());
    assertEquals(2, bindValue.getIndex());
}
Also used : SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) MockPreparedStatement(org.seasar.doma.internal.jdbc.mock.MockPreparedStatement) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) BigDecimal(java.math.BigDecimal) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) RowData(org.seasar.doma.internal.jdbc.mock.RowData) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 15 with RowData

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

the class BuiltinIdentityIdGeneratorTest method test_identitySelectSql.

@Test
public void test_identitySelectSql() {
    MockConfig config = new MockConfig();
    config.setDialect(new PostgresDialect());
    MockResultSet resultSet = config.dataSource.connection.preparedStatement.resultSet;
    resultSet.rows.add(new RowData(11L));
    BuiltinIdentityIdGenerator identityIdGenerator = new BuiltinIdentityIdGenerator();
    IdGenerationConfig idGenerationConfig = new IdGenerationConfig(config, _IdGeneratedEmp.getSingletonInternal());
    Long value = identityIdGenerator.generatePostInsert(idGenerationConfig, config.dataSource.connection.preparedStatement);
    assertEquals(new Long(11), value);
    assertEquals("select currval(pg_catalog.pg_get_serial_sequence('\"CATA\".\"EMP\"', 'id'))", config.dataSource.connection.preparedStatement.sql);
}
Also used : RowData(org.seasar.doma.internal.jdbc.mock.RowData) 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)

Aggregations

Test (org.junit.jupiter.api.Test)27 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)27 RowData (org.seasar.doma.internal.jdbc.mock.RowData)27 ColumnMetaData (org.seasar.doma.internal.jdbc.mock.ColumnMetaData)23 MockResultSetMetaData (org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData)23 SqlFileSelectQuery (org.seasar.doma.jdbc.query.SqlFileSelectQuery)16 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)14 Emp (example.entity.Emp)11 example.entity._Emp (example.entity._Emp)11 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 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 BigDecimal (java.math.BigDecimal)6 NonUniqueResultException (org.seasar.doma.jdbc.NonUniqueResultException)6 PhoneNumber (example.domain.PhoneNumber)5 example.domain._PhoneNumber (example.domain._PhoneNumber)5 StringWrapper (org.seasar.doma.wrapper.StringWrapper)5