Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,14 @@ class MangoTidyMap {
toMangoOperator(val as Map, result[key] as Map)
}
else {
if (key.toString().startsWith('$')) {
//if its a like then replace star
if ((key.toString() == '$ilike' || key.toString() == '$like') && val instanceof String ){
result[key] = val.endsWith("*") ? val.replace('*', '%') : val
}
else if (key.toString().startsWith('$')) {
result[key] = val
} //if we already have Mango method
}
//if we already have Mango method
else if (val instanceof List) {
List valAsList = val as List
// for handling case {customer: [{id:1}, {id:2}]}, transforms to {customer:{id:{'$in': [1,2]}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,74 @@ class MangoTidyMapSpec extends Specification {
]
}

void "test like with star *"() {
when: 'its already in $ilike'
def mmap = tidy([
name: ['$ilike': "Name*"]
])

then: 'it gets replaced with %'
mmap == [name: ['$ilike': "Name%"]]

when: 'its already in $like'
mmap = tidy([
name: ['$like': "*ame*"]
])

then: 'it gets replaced with %'
mmap == [name: ['$like': "%ame%"]]

when:
mmap = tidy([
'foo.name': "Name*"
])

then:
mmap == [foo: [name: ['$ilike': "Name%"]]]

when:
mmap = tidy(
[foo:
[name: "Name*"]
])

then:
mmap == [foo: [name: ['$ilike': "Name%"]]]

when: "wrapped in a not"
mmap = tidy([
'$not':[
'foo':[
name: "Name*"
]
]
])

then: "converts it to a list"
mmap == [
'$not': [ //list
[foo: [name: ['$ilike': "Name%"]]]
]
]

when: "wrapped in a not shortcut"
mmap = tidy([
'$not':[
'foo.name': "Name*",
'buzz.boo': "bar*"
]
])

then: "converts it to a list of maps"
mmap == [
'$not': [
[foo: [name: ['$ilike': "Name%"]]],
[buzz: [boo: ['$ilike': 'bar%']]]
]
]
}


void "test not simple"() {
when: 'the $not is a simple map'
def mmap = tidy([
Expand Down Expand Up @@ -452,11 +520,11 @@ class MangoTidyMapSpec extends Specification {

when:
mmap = tidy([
'foo.name.$like': "Name"
'foo.name.$like': "Name*"
])

then:
mmap == [foo: [name: ['$like': "Name"]]]
mmap == [foo: [name: ['$like': "Name%"]]]

when:
mmap = tidy([
Expand Down Expand Up @@ -535,7 +603,7 @@ class MangoTidyMapSpec extends Specification {
mmap = tidy([
'$not':[
'$exists': crit,
'name': 'org4%'
'name': 'org4*'
]
])

Expand Down