Skip to content

Instantly share code, notes, and snippets.

@uklance
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uklance/1dba84e5ee358beee3e9 to your computer and use it in GitHub Desktop.
Save uklance/1dba84e5ee358beee3e9 to your computer and use it in GitHub Desktop.
sproc-unit thoughts
@Events({Event.SCENARIO_THEN, Event.SCENARIO_AFTER})
public class AssertCommand {
@Parameter(requiredGroup="value")
private String sql;
@Parameter(name="attribute", requiredGroup="value")
private String attributeName;
@Parameter(name="expected", required=true)
private String expectedString;
@Inject
private CommandContext commandContext;
@Inject
private ConnectionFactory connectionFactory;
@Command
public void doAssert() {
if (sql != null) {
...
} else if (attributeName != null) {
Object attribute = commandContext.getAttribute(attributeName);
}
}
}
public interface CommandBuilder<T extends Command> {
CommandBuilder<T> withField(String name, Object value);
T build();
}
public interface CommandBuilderSource {
<T extends Command> CommandBuilder<T> get(Class<T> type);
}
public class CommandJunitClassRunner extends ParentRunner<ScenarioGroup> {
public CommandJunitClassRunner(Class... moduleClasses) {
...
}
}
@Events({Event.SCENARIO_GROUP_BEFORE, Event.SCENARIO_GIVEN, Event.SCENARIO_WHEN})
public class InsertCommand {
@Parameter(required=true)
private String table;
// <insert randomize="firstName,birthDate" ... />
@Parameter(defaultPrefix="csv")
private List<String> randomize;
// nested <insert><row /><insert>
@Parameter
private List<Row> rows;
@Inject
private ConnectionFactory connectionFactory;
@Command
public void doInsert() {
for (Row row : rows) {
...
}
}
}
public class JdbcCommandJunitClassRunner extends CommandJunitClassRunner<ScenarioGroup> {
public JdbcCommandJunitClassRunner() {
super(JdbcCommandModule.class)
}
}
public class Row {
@WildParameters(pattern="*")
private Map<String, String> rows;
}
<scenarios>
<import path="some/other/scenarios.xml" />
<before>
<!-- h2 sproc definitions -->
<execute>CREATE ALIAS DO_ADD FOR &quot;foo.bar.TestUtils.doAdd&quot;;</execute>
<execute>CREATE ALIAS DO_MULTIPLY FOR &quot;foo.bar.TestUtils.doMultiply&quot;;</execute>
<execute>CREATE ALIAS DO_CONCAT FOR &quot;foo.bar.TestUtils.doConcat&quot;;</execute>
</before>
<after>
<execute>DROP ALIAS DO_ADD;</execute>
<execute>DROP ALIAS DO_MULTIPLY;</execute>
<execute>DROP ALIAS DO_CONCAT;</execute>
</after>
<scenario name="Testing DO_ADD">
<when>
<attribute name="a1" sql="CALL DO_ADD(1, 2)" />
<attribute name="a2" sql="CALL DO_ADD(2, 4)" />
</when>
<then>
<assertEquals expected="3" sql="select ${a1}" />
<assertEquals expected="6" sql="select ${a2}" />
</then>
</scenario>
<scenario name="Testing DO_MULTIPLY">
<when>
<attribute name="a1" sql="CALL DO_MULTIPLY(2, 4)" />
<attribute name="a2" sql="CALL DO_MULTIPLY(3, 6)" />
</when>
<then>
<assertEquals expected="8" sql="select ${a1}" />
<assertEquals expected="18" sql="select ${a2}" />
<assertEquals expected="20" sql="CALL DO_MULTIPLY(4, 5)" />
<assertEquals expected="8" attribute="a1" />
</then>
</scenario>
<scenario name="Testing DO_CONCAT">
<when>
<attribute name="a1" sql="CALL DO_CONCAT('a', 'b')" />
</when>
<then>
<assertEquals expected="ab" sql="select '${a1}'" />
</then>
</scenario>
<scenario name="Wish List">
<given>
<attribute name="empId1" sql="select EMPLOYEE_SEQ.nextval from dual;" />
<attribute name="empId2" sql="select EMPLOYEE_SEQ.nextval from dual;" />
<insert table="employees" randomize="birthDate,startDate">
<row employeeId="${empId1}" firstName="Barney" surname="Rubble" />
<row employeeId="${empId2}" firstName="Fred" surname="Flinstone" />
</insert>
</given>
<when>
<resultSet name="employees" sql="select * from employees where employeeId = ${empId1}" />
</when>
<then>
<assertEquals expected="1" attribute="employees.size()" />
<assertEquals expected="Rubble" attribute="employees[0].surname" />
</then>
<after>
<execute>delete from employees where employeeId in (${empId1}, ${empId2})</execute>
</after>
</scenario>
</scenarios>
public class SprocUnitModule {
public void contributeInjectionProviderSource(MappedConfiguration<String, InjectionProvider> config) {
config.addInstance("connectionFactory", ConnectionFactoryInjectionProvider.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment