From f9bf2aaac6e6c8aab42fea31c74ff8d3ef93d0fa Mon Sep 17 00:00:00 2001 From: Sudhir Nimavat Date: Thu, 5 Jun 2025 11:42:02 +0530 Subject: [PATCH 1/3] Do not create empty locations /9ci/domain9#3200 --- .../yakworks/rest/OrgRestApiSpec.groovy | 16 +++++++++++-- .../yakworks/rally/orgs/OrgRepoTests.groovy | 23 ++++++++++++++++++- .../rally/orgs/repo/AbstractOrgRepo.groovy | 1 + 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/examples/rally-api/src/integration-test/groovy/yakworks/rest/OrgRestApiSpec.groovy b/examples/rally-api/src/integration-test/groovy/yakworks/rest/OrgRestApiSpec.groovy index bacaedce3..c962f8f70 100644 --- a/examples/rally-api/src/integration-test/groovy/yakworks/rest/OrgRestApiSpec.groovy +++ b/examples/rally-api/src/integration-test/groovy/yakworks/rest/OrgRestApiSpec.groovy @@ -2,6 +2,7 @@ package yakworks.rest import gorm.tools.transaction.WithTrx import grails.gorm.transactions.Rollback +import grails.gorm.transactions.Transactional import okhttp3.Request import okhttp3.RequestBody import org.apache.poi.xssf.usermodel.XSSFWorkbook @@ -9,6 +10,7 @@ import org.springframework.http.HttpStatus import spock.lang.Ignore import spock.lang.IgnoreRest +import yakworks.rally.orgs.model.Location import yakworks.rest.client.OkAuth import yakworks.rest.client.OkHttpRestTrait import grails.testing.mixin.integration.Integration @@ -362,17 +364,27 @@ class OrgRestApiSpec extends Specification implements OkHttpRestTrait, WithTrx { body.errors[0].message == 'must not be null' } + @Transactional void "testing put"() { - when: - Response resp = put(path, [name: "9Galt"], 67) + setup: + int countBefore = Location.count() + Location existing = Org.get(67).location + when: + Response resp = put(path, [name: "9Galt", location:[city:"test"], locations:[[:]]], 67) Map body = bodyToMap(resp) + Location updated = Org.get(67).location + int countAfter = Location.count() then: resp.code() == HttpStatus.OK.value() body.id body.name == '9Galt' + and: + countBefore = countAfter + existing.id = updated.id + updated.city == "test" } void "test post with tags"() { diff --git a/rally-domain/src/integration-test/groovy/yakworks/rally/orgs/OrgRepoTests.groovy b/rally-domain/src/integration-test/groovy/yakworks/rally/orgs/OrgRepoTests.groovy index 71bf5d0f7..7a4cda858 100644 --- a/rally-domain/src/integration-test/groovy/yakworks/rally/orgs/OrgRepoTests.groovy +++ b/rally-domain/src/integration-test/groovy/yakworks/rally/orgs/OrgRepoTests.groovy @@ -261,7 +261,7 @@ class OrgRepoTests extends Specification implements DomainIntTest { location: [ city: 'Denver', street1: '1st' - ] + ], ] org = orgRepo.update(data) flush() @@ -278,6 +278,27 @@ class OrgRepoTests extends Specification implements DomainIntTest { countBefore == countAfter } + void "should not create empty locations"() { + setup: + Org org = Org.get(11) + int countBefore = Location.count() + + when: + Map data = [ + id: org.id, + locations:[ + [:] + ] + ] + orgRepo.update(data) + flushAndClear() + int countAfter = Location.count() + + then: "no new location should be added" + noExceptionThrown() + countBefore == countAfter + } + void "test insert with orgmembers"() { given: OrgDimensionTesting.setDimensions(['Branch', 'Division', 'Business']) diff --git a/rally-domain/src/main/groovy/yakworks/rally/orgs/repo/AbstractOrgRepo.groovy b/rally-domain/src/main/groovy/yakworks/rally/orgs/repo/AbstractOrgRepo.groovy index 5e61ee20e..7435eb354 100644 --- a/rally-domain/src/main/groovy/yakworks/rally/orgs/repo/AbstractOrgRepo.groovy +++ b/rally-domain/src/main/groovy/yakworks/rally/orgs/repo/AbstractOrgRepo.groovy @@ -151,6 +151,7 @@ abstract class AbstractOrgRepo extends LongIdGormRepo { */ void persistToManyWithOrgId(Org org, GormRepo assocRepo, List assocList){ if(!assocList) return + assocList = assocList.findAll { Map it -> it.size() > 0} assocList.each { it['orgId'] = org.getId()} assocRepo.createOrUpdate(assocList) } From 9f5ad84c1665d355b39ce010f3d10e299867d4a7 Mon Sep 17 00:00:00 2001 From: Sudhir Nimavat Date: Thu, 5 Jun 2025 11:47:31 +0530 Subject: [PATCH 2/3] typo --- .../groovy/yakworks/rest/OrgRestApiSpec.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/rally-api/src/integration-test/groovy/yakworks/rest/OrgRestApiSpec.groovy b/examples/rally-api/src/integration-test/groovy/yakworks/rest/OrgRestApiSpec.groovy index c962f8f70..6852e9cf6 100644 --- a/examples/rally-api/src/integration-test/groovy/yakworks/rest/OrgRestApiSpec.groovy +++ b/examples/rally-api/src/integration-test/groovy/yakworks/rest/OrgRestApiSpec.groovy @@ -382,8 +382,8 @@ class OrgRestApiSpec extends Specification implements OkHttpRestTrait, WithTrx { body.name == '9Galt' and: - countBefore = countAfter - existing.id = updated.id + countBefore == countAfter + existing.id == updated.id updated.city == "test" } From 131449e92000a91ae0a85608f674e5bb20ca12df Mon Sep 17 00:00:00 2001 From: Sudhir Nimavat Date: Thu, 5 Jun 2025 13:49:32 +0530 Subject: [PATCH 3/3] Add comment --- .../main/groovy/yakworks/rally/orgs/repo/AbstractOrgRepo.groovy | 1 + 1 file changed, 1 insertion(+) diff --git a/rally-domain/src/main/groovy/yakworks/rally/orgs/repo/AbstractOrgRepo.groovy b/rally-domain/src/main/groovy/yakworks/rally/orgs/repo/AbstractOrgRepo.groovy index 7435eb354..c7934a38e 100644 --- a/rally-domain/src/main/groovy/yakworks/rally/orgs/repo/AbstractOrgRepo.groovy +++ b/rally-domain/src/main/groovy/yakworks/rally/orgs/repo/AbstractOrgRepo.groovy @@ -151,6 +151,7 @@ abstract class AbstractOrgRepo extends LongIdGormRepo { */ void persistToManyWithOrgId(Org org, GormRepo assocRepo, List assocList){ if(!assocList) return + //remove, if there are any empty maps. assocList = assocList.findAll { Map it -> it.size() > 0} assocList.each { it['orgId'] = org.getId()} assocRepo.createOrUpdate(assocList)