Skip to content

Commit

Permalink
refs #1 - expected dataset bdd documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Jun 16, 2016
1 parent f9a3c27 commit 5de9137
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
/**
* Created by rafael-pestano on 16/06/2016.
*/
// tag::expectedCDIDeclaration[]
@RunWith(CdiTestRunner.class)
public class ExpectedDataSetCDIIt {

@Inject
EntityManager em;

// end::expectedCDIDeclaration[]

// tag::expectedCDI[]
@Test
@UsingDataSet //needed to activate interceptor (can be at class level)
@ExpectedDataSet(value = "yml/expectedUsers.yml",ignoreCols = "id")
Expand All @@ -32,4 +36,5 @@ public void shouldMatchExpectedDataSet() {
em.persist(u2);
em.getTransaction().commit();
}
// end::expectedCDI[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"src/test/resources/features/core/core-seed-database.feature",
"src/test/resources/features/cdi/cdi-seed-database.feature",
"src/test/resources/features/cucumber/cucumber-seed-database.feature",
"src/test/resources/features/general/dataset-replacements.feature"
"src/test/resources/features/general/dataset-replacements.feature",
"src/test/resources/features/general/expected-dataset.feature"
},
plugin = "json:target/dbunit-rules.json")
public class DBUnitRulesBdd {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.dbunit.rules.bdd.cdi;

import com.github.dbunit.rules.cdi.api.UsingDataSet;
import com.github.dbunit.rules.model.Tweet;
import com.github.dbunit.rules.model.User;
import cucumber.api.PendingException;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
Expand All @@ -13,8 +10,6 @@
import javax.persistence.EntityManager;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -49,17 +44,6 @@ public void The_database_should_be_seeded_with_the_dataset_content() throws Thro

}

@Given("^Groovy script engine is on test classpath$")
public void Groovy_script_engine_is_on_test_classpath(String docstring) throws Throwable {
// Express the Regexp above with the code you wish you had
assertNotNull(docstring);
}

@Then("^Dataset script should be interpreted when seeding the database$")
public void Dataset_script_should_be_interpreted_when_seeding_the_database() throws Throwable {
// Express the Regexp above with the code you wish you had
}

/* Use database seeding in cucumber on cucumber feature
@Then("^The database should be seeded with the dataset content before test execution$")
Expand Down
2 changes: 2 additions & 0 deletions cdi/src/test/resources/features/cdi/cdi-seed-database.feature
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ include::../../src/test/java/com/github/dbunit/rules/DBUnitCDITest.java[tags=see
"""

Then The database should be seeded with the dataset content before test execution


Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ include::../../../core/src/test/resources/datasets/yml/groovy-with-date-replacem
include::../../../core/src/test/java/com/github/dbunit/rules/ScriptReplacementsIt.java[tags=groovy]
----
"""
Then Dataset script should be interpreted when seeding the database
Then Dataset script should be interpreted while seeding the database

Scenario: Seed database with javascript in dataset

Expand All @@ -70,4 +70,4 @@ include::../../../core/src/test/resources/datasets/yml/js-with-calc-replacements
include::../../../core/src/test/java/com/github/dbunit/rules/ScriptReplacementsIt.java[tags=javascript-likes]
----
"""
Then Dataset script should be interpreted when seeding the database
Then Dataset script should be interpreted while seeding the database
102 changes: 102 additions & 0 deletions cdi/src/test/resources/features/general/expected-dataset.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# order: 5
Feature: Database assertion using expected datasets
====
[quote]
____
In order to verify database state after test execution
As a developer
I want to assert database state with datasets.
____
====

Scenario: Database assertion with yml dataset

#cukedoctor-discrete
Given The following dataset
"""
----
include::../../../core/src/test/resources/datasets/yml/expectedUsers.yml[]
----
"""

#cukedoctor-discrete
When The following test is executed:
"""
[source,java,indent=0,linenums]
----
include::../../../core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java[tags=expectedDeclaration;expected]
----
"""
Then Test must pass because database state is as in expected dataset.

Scenario: Database assertion with regular expression in expected dataset

#cukedoctor-discrete
Given The following dataset
"""
----
include::../../../core/src/test/resources/datasets/yml/expectedUsersRegex.yml[]
----
"""

#cukedoctor-discrete
When The following test is executed:
"""
[source,java,indent=0,linenums]
----
include::../../../core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java[tags=expectedRegex]
----
"""
Then Test must pass because database state is as in expected dataset.

Scenario: Database assertion with seeding before test execution

#cukedoctor-discrete
Given The following dataset
"""
----
include::../../../core/src/test/resources/datasets/yml/user.yml[]
----
"""
#cukedoctor-discrete
And The following dataset
"""
----
include::../../../core/src/test/resources/datasets/yml/expectedUsers.yml[]
----
"""

#cukedoctor-discrete
When The following test is executed:
"""
[source,java,indent=0,linenums]
----
include::../../../core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java[tags=expectedWithSeeding]
----
"""
Then Test must pass because database state is as in expected dataset.

Scenario: Failling database assertion

#cukedoctor-discrete
Given The following dataset
"""
----
include::../../../core/src/test/resources/datasets/yml/expectedUsers.yml[]
----
"""

#cukedoctor-discrete
When The following test is executed:
"""
[source,java,indent=0,linenums]
----
include::../../../core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java[tags=faillingExpected]
----
"""

#cukedoctor-discrete
Then Test must fail showing what it was expecting as database state.
"""
NOTE: This step is failing purposely to show the error message.
"""
19 changes: 12 additions & 7 deletions core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/**
* Created by rmpestano on 6/15/16.
*/
// tag::expectedDeclaration[]
@RunWith(JUnit4.class)
public class ExpectedDataSetIt {

Expand All @@ -23,6 +24,9 @@ public class ExpectedDataSetIt {
public DBUnitRule dbUnitRule = DBUnitRule.instance(emProvider.getConnection());


// end::expectedDeclaration[]

// tag::expected[]
@Test
@ExpectedDataSet(value = "yml/expectedUsers.yml",ignoreCols = "id")
public void shouldMatchExpectedDataSet() {
Expand All @@ -34,16 +38,12 @@ public void shouldMatchExpectedDataSet() {
emProvider.em().persist(u);
emProvider.em().persist(u2);
emProvider.tx().commit();
/*
done by expected dataset
assertThat(u.getId()).isNotNull();
assertThat(u.getName()).isEqualTo("expected user1");
assertThat(u2.getId()).isNotNull();
assertThat(u2.getName()).isEqualTo("expected user2");*/
}
// end::expected[]

@Test
@Ignore(value = "How to test failled comparisons?")
// tag::faillingExpected[]
@Test
@ExpectedDataSet(value = "yml/expectedUsers.yml",ignoreCols = "id")
public void shouldNotMatchExpectedDataSet() {
User u = new User();
Expand All @@ -55,7 +55,9 @@ public void shouldNotMatchExpectedDataSet() {
emProvider.em().persist(u2);
emProvider.tx().commit();
}
// end::faillingExpected[]

// tag::expectedRegex[]
@Test
@ExpectedDataSet(value = "yml/expectedUsersRegex.yml")
public void shouldMatchExpectedDataSetUsingRegex() {
Expand All @@ -68,7 +70,9 @@ public void shouldMatchExpectedDataSetUsingRegex() {
emProvider.em().persist(u2);
emProvider.tx().commit();
}
// end::expectedRegex[]

// tag::expectedWithSeeding[]
@Test
@DataSet(value = "yml/user.yml", disableConstraints = true,cleanAfter = true)
@ExpectedDataSet(value = "yml/expectedUser.yml", ignoreCols = "id")
Expand All @@ -77,4 +81,5 @@ public void shouldMatchExpectedDataSetAfterSeedingDataBase() {
emProvider.em().remove(emProvider.em().find(User.class,1L));
emProvider.tx().commit();
}
// end::expectedWithSeeding[]
}

0 comments on commit 5de9137

Please sign in to comment.