https://github.com/disc99/hogan
Hogan is the utility library which allows you to access DB intuitively.
https://github.com/disc99/hogan
dsl groovy java sql testing
Last synced: 11 months ago
JSON representation
Hogan is the utility library which allows you to access DB intuitively.
- Host: GitHub
- URL: https://github.com/disc99/hogan
- Owner: disc99
- License: mit
- Created: 2015-08-10T12:04:53.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-02-17T15:56:47.000Z (about 9 years ago)
- Last Synced: 2025-04-22T20:18:44.039Z (11 months ago)
- Topics: dsl, groovy, java, sql, testing
- Language: Groovy
- Homepage:
- Size: 132 KB
- Stars: 5
- Watchers: 2
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hogan

[](http://opensource.org/licenses/MIT)
[ ](https://bintray.com/disc99/maven/hogan/_latestVersion)
[](https://travis-ci.org/disc99/hogan)
[](https://coveralls.io/github/disc99/hogan?branch=master)
Hogan is the utility library which allows you to access DB intuitively.
## Description
As you know, DB processing needs boilerplate code.
Hogan expresses data structure with Spock like DSL (known as "data table DSL").
That's why you can access DB intuitively.
## Features
- Insert multiple tables
- Assert multiple tables (@Beta)
## Usage
### Add dependency
At first, please add following dependency and repository.
```groovy
dependencies {
testCompile "io.disc99:hogan:0.9.2"
}
repositories {
jcenter()
}
```
### Enable Hogan DSL
There are two ways to enable Hogan DSL.
One way is to create subclass of `Specification`.
The other way is to add `@EnableHogan` annotation to the target class.
### Create `Database` instance
Create `Database` class which processes DB access.
Constractor of `Database` is a deregater to create `groovy.sql.Sql`(new Sql or Sql.newInstance).
If you need more information, check the following link.
[`groovy.sql.Sql`](http://docs.groovy-lang.org/docs/latest/html/gapi/groovy/sql/Sql.html)
```groovy
Database db = new Database("jdbc:h2:mem:", "org.h2.Driver")
```
### Feature: insert
Describe table name whith label, and execute SQL each of it.
Execute 'Insert' according to the table definition.
And of cource you can deal with the number of labels.
```groovy
class HoganSpec extends Specification {
def test() {
setup:
db.insert {
item_master:
id | name | price
1 | 'Apple' | 500
2 | 'Orange' | 250
sales:
id | day | item_id | num
1 | '2015-04-01' | 1 | 3
2 | '2015-04-02' | 2 | 1
3 | '2015-04-02' | 1 | 2
}
expect:
// ...
}
}
```
Followings are acutual executed SQL.
```sql
INSERT INTO ITEM_MASTER (ID, NAME, PRICE) VALUES (1, 'Apple', 500)
INSERT INTO ITEM_MASTER (ID, NAME, PRICE) VALUES (2, 'Orange', 250)
INSERT INTO SALES (ID, DAY, ITEM_ID, NUM) VALUES (1, '2015-04-01', 1, 3)
INSERT INTO SALES (ID, DAY, ITEM_ID, NUM) VALUES (2, '2015-04-02', 2, 1)
INSERT INTO SALES (ID, DAY, ITEM_ID, NUM) VALUES (3, '2015-04-02', 1, 2)
```
### Feature: assert (@Beta)
Assert to the table according to the definition.
And undefined columns will be ignored.
```groovy
class HoganSpec extends Specification {
def test() {
when:
// ...
then:
db.assert {
item_master:
id | name
100 | 'Banana'
101 | 'Pine'
}
}
}
```
You will get following message when there's any discard.
```
assert actual == expected
| | |
| | [[ID:100, NAME:Banana], [ID:101, NAME:Pine]]
| false
[[ID:100, NAME:Banana], [ID:101, NAME:Pineapple]]
```
## License
[MIT License](https://github.com/disc99/hogan/blob/master/LICENSE)