-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for more generic json/jsonb criteria
- Loading branch information
Showing
12 changed files
with
370 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
grails-app/services/test/criteria/json/PgJsonbTestSearchService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package test.criteria.json | ||
|
||
import test.json.TestMapJsonb | ||
|
||
class PgJsonbTestSearchService { | ||
static transactional = false | ||
|
||
List<TestMapJsonb> search(String criteriaName, String field, String jsonAttribute, value) { | ||
TestMapJsonb.withCriteria { | ||
"${criteriaName}" field, jsonAttribute, value.toString() | ||
} | ||
} | ||
|
||
List<TestMapJsonb> search(String criteriaName, String field, String jsonOp, String jsonAttribute, String sqlOp, value) { | ||
TestMapJsonb.withCriteria { | ||
"${criteriaName}" field, jsonOp, jsonAttribute, sqlOp, value.toString() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
test/integration/net/kaleidos/hibernate/json/PgJsonPathsIntegrationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package net.kaleidos.hibernate.json | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
import test.json.TestMapJson | ||
|
||
class PgJsonPathsIntegrationSpec extends Specification { | ||
|
||
def pgJsonTestSearchService | ||
|
||
@Unroll | ||
void 'Test equals finding nested values (json)'() { | ||
setup: | ||
new TestMapJson(data: [name: 'Iván', lastName: 'López', nested: [a: 1, b: 2]]).save(flush: true) | ||
new TestMapJson(data: [name: 'Alonso', lastName: 'Torres', nested: [a: 2, b: 3]]).save(flush: true) | ||
new TestMapJson(data: [name: 'Iván', lastName: 'Pérez', nested: [a: 1, b: 5]]).save(flush: true) | ||
when: | ||
def result = pgJsonTestSearchService.search('pgJson', 'data', '#>>', '{nested, a}', '=', value) | ||
then: | ||
result.size() == size | ||
result.every { it.data.nested.a == value } | ||
where: | ||
value || size | ||
1 || 2 // there are 2 items with nested.a equal to 1 | ||
2 || 1 | ||
3 || 0 | ||
} | ||
@Unroll | ||
void 'Test equals finding nested values (json)'() { | ||
setup: | ||
new TestMapJson(data: [name: 'Iván', lastName: 'López', nested: [a: 1, b: 2]]).save(flush: true) | ||
new TestMapJson(data: [name: 'Alonso', lastName: 'Torres', nested: [a: 2, b: 3]]).save(flush: true) | ||
new TestMapJson(data: [name: 'Iván', lastName: 'Pérez', nested: [a: 1, b: 5]]).save(flush: true) | ||
when: | ||
def result = pgJsonTestSearchService.search('pgJson', 'data', '#>>', '{nested, b}', '>', value) | ||
then: | ||
result.size() == size | ||
result.every { it.data.nested.b > value.toInteger() } | ||
where: | ||
value || size | ||
1 || 3 // There are 3 items with nested.b > 1 | ||
3 || 1 | ||
6 || 0 | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
test/integration/net/kaleidos/hibernate/json/PgJsonValuesIntegrationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package net.kaleidos.hibernate.json | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
import test.json.TestMapJson | ||
|
||
class PgJsonValuesIntegrationSpec extends Specification { | ||
|
||
def pgJsonTestSearchService | ||
|
||
@Unroll | ||
void 'Test equals finding value: #value with condition is ilike (json)'() { | ||
setup: | ||
new TestMapJson(data: [name: 'Iván', lastName: 'López']).save(flush: true) | ||
new TestMapJson(data: [name: 'Alonso', lastName: 'Torres']).save(flush: true) | ||
new TestMapJson(data: [name: 'Iván', lastName: 'Pérez']).save(flush: true) | ||
when: | ||
def result = pgJsonTestSearchService.search('pgJson', 'data', '->>', 'name', 'ilike', value) | ||
then: | ||
result.size() == size | ||
result.every { it.data.name.matches "^(?i)${value.replace('%', '.*')}\$" } | ||
where: | ||
value || size | ||
'%iv%' || 2 | ||
'John' || 0 | ||
} | ||
@Unroll | ||
void 'Test equals finding value: #value with condition equals (json)'() { | ||
setup: | ||
new TestMapJson(data: [name: 'Iván', lastName: 'López']).save(flush: true) | ||
new TestMapJson(data: [name: 'Alonso', lastName: 'Torres']).save(flush: true) | ||
new TestMapJson(data: [name: 'Iván', lastName: 'Pérez']).save(flush: true) | ||
when: | ||
def result = pgJsonTestSearchService.search('pgJson', 'data', '->>', 'name', '=', value) | ||
then: | ||
result.size() == size | ||
result.every { it.data.name == value } | ||
where: | ||
value || size | ||
'Iván' || 2 | ||
'John' || 0 | ||
} | ||
@Unroll | ||
void 'Test equals finding value: #value with condition does not equal (json)'() { | ||
setup: | ||
new TestMapJson(data: [name: 'Iván', lastName: 'López']).save(flush: true) | ||
new TestMapJson(data: [name: 'Alonso', lastName: 'Torres']).save(flush: true) | ||
new TestMapJson(data: [name: 'Iván', lastName: 'Pérez']).save(flush: true) | ||
when: | ||
def result = pgJsonTestSearchService.search('pgJson', 'data', '->>', 'name', '<>', value) | ||
then: | ||
result.size() == size | ||
result.every { it.data.name != value } | ||
where: | ||
value || size | ||
'Iván' || 1 | ||
'John' || 3 | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
test/integration/net/kaleidos/hibernate/json/PgJsonbEqualsIntegrationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.kaleidos.hibernate.json | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
import test.json.TestMapJsonb | ||
|
||
class PgJsonbEqualsIntegrationSpec extends Specification { | ||
|
||
def pgJsonbTestSearchService | ||
|
||
@Unroll | ||
void 'Test equals finding value: #value (jsonb)'() { | ||
setup: | ||
new TestMapJsonb(data: [name: 'Iván', lastName: 'López']).save(flush: true) | ||
new TestMapJsonb(data: [name: 'Alonso', lastName: 'Torres']).save(flush: true) | ||
new TestMapJsonb(data: [name: 'Iván', lastName: 'Pérez']).save(flush: true) | ||
|
||
when: | ||
def result = pgJsonbTestSearchService.search('pgJsonHasFieldValue', 'data', 'name', value) | ||
|
||
then: | ||
result.size() == size | ||
result.every { it.data.name == value } | ||
|
||
where: | ||
value || size | ||
'Iván' || 2 | ||
'John' || 0 | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
test/integration/net/kaleidos/hibernate/json/PgJsonbPathsIntegrationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package net.kaleidos.hibernate.json | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
import test.json.TestMapJsonb | ||
|
||
class PgJsonbPathsIntegrationSpec extends Specification { | ||
|
||
def pgJsonbTestSearchService | ||
|
||
@Unroll | ||
void 'Test equals finding nested values (jsonb)'() { | ||
setup: | ||
new TestMapJsonb(data: [name: 'Iván', lastName: 'López', nested: [a: 1, b: 2]]).save(flush: true) | ||
new TestMapJsonb(data: [name: 'Alonso', lastName: 'Torres', nested: [a: 2, b: 3]]).save(flush: true) | ||
new TestMapJsonb(data: [name: 'Iván', lastName: 'Pérez', nested: [a: 1, b: 5]]).save(flush: true) | ||
when: | ||
def result = pgJsonbTestSearchService.search('pgJson', 'data', '#>>', '{nested, a}', '=', value) | ||
then: | ||
result.size() == size | ||
result.every { it.data.nested.a == value } | ||
where: | ||
value || size | ||
1 || 2 // there are 2 items with nested.a equal to 1 | ||
2 || 1 | ||
3 || 0 | ||
} | ||
@Unroll | ||
void 'Test equals finding nested values (jsonb)'() { | ||
setup: | ||
new TestMapJsonb(data: [name: 'Iván', lastName: 'López', nested: [a: 1, b: 2]]).save(flush: true) | ||
new TestMapJsonb(data: [name: 'Alonso', lastName: 'Torres', nested: [a: 2, b: 3]]).save(flush: true) | ||
new TestMapJsonb(data: [name: 'Iván', lastName: 'Pérez', nested: [a: 1, b: 5]]).save(flush: true) | ||
when: | ||
def result = pgJsonbTestSearchService.search('pgJson', 'data', '#>>', '{nested, b}', '>', value) | ||
then: | ||
result.size() == size | ||
result.every { it.data.nested.b > value.toInteger() } | ||
where: | ||
value || size | ||
1 || 3 // There are 3 items with nested.b > 1 | ||
3 || 1 | ||
6 || 0 | ||
} | ||
} |
Oops, something went wrong.