trydofor / professional-wings

WingsBoot=BKB+BoT+SpringBoot: ①quickly achieve business goals; ②timely repay technical debt; ③safely refactor programs and business. We advocate defensive programming, May The `false` Be With You !
https://wings.fessional.pro
Apache License 2.0
87 stars 10 forks source link

mock jooq dsl #152

Closed trydofor closed 11 months ago

trydofor commented 12 months ago

Mocking Connection

import static org.jooq.SQLDialect.*;
import static org.jooq.impl.DSL.*;
import static com.example.generated.Tables.*;
import org.jooq.*;
import org.jooq.impl.*;

// The "create" reference is an instance of [DSLContext](https://www.jooq.org/javadoc/latest/org.jooq/org/jooq/DSLContext.html)

// Initialise your data provider (implementation further down):
MockDataProvider provider = new MyProvider();
MockConnection connection = new MockConnection(provider);

// Pass the mock connection to a jOOQ DSLContext:
DSLContext create = DSL.using(connection, SQLDialect.ORACLE);

// Execute queries transparently, with the above DSLContext:
Result<BookRecord> result = create.selectFrom(BOOK).where(BOOK.ID.eq(5)).fetch();
// For more info on imports and data types, click on the "Help" icon
// These imports, and possibly others, are implied:
import java.sql.*;
import javax.sql.*;
import static org.jooq.SQLDialect.*;
import static org.jooq.impl.DSL.*;
import static com.example.generated.Tables.*;
import org.jooq.*;
import org.jooq.Record; // Starting with Java 14, Record can not be imported on demand anymore
import org.jooq.impl.*;

// The "create" reference is an instance of [DSLContext](https://www.jooq.org/javadoc/latest/org.jooq/org/jooq/DSLContext.html)

public class MyProvider implements MockDataProvider {

    @Override
    public MockResult[] execute(MockExecuteContext ctx) throws SQLException {

        // You might need a DSLContext to create org.jooq.Result and org.jooq.Record objects
        DSLContext create = DSL.using(SQLDialect.ORACLE);
        MockResult[] mock = new MockResult[1];

        // The execute context contains SQL string(s), bind values, and other meta-data
        String sql = ctx.sql();

        // Exceptions are propagated through the JDBC and jOOQ APIs
        if (sql.toUpperCase().startsWith("DROP")) {
            throw new SQLException("Statement not supported: " + sql);
        }

        // You decide, whether any given statement returns results, and how many
        else if (sql.toUpperCase().startsWith("SELECT")) {

            // Always return one record
            Result<Record2<Integer, String>> result = create.newResult(AUTHOR.ID, AUTHOR.LAST_NAME);
            result.add(create
                .newRecord(AUTHOR.ID, AUTHOR.LAST_NAME)
                .values(1, "Orwell"));
            mock[0] = new MockResult(1, result);
        }

        // You can detect batch statements easily
        else if (ctx.batch()) {
            // [...]
        }

        return mock;
    }
}
trydofor commented 12 months ago

in spring auto configuration, declare ConnectionProvider global

@Bean
public ConnectionProvider mockConnectionProvider(){
     return new MockConnectionProvider();//
}

in wings, can set DSLContext via *Dao to impl mocking, instance

myDao.setDslContext(() -> myDsl);