blob: 8d8f6b73e65be16765ebdd5964d357dbfa11cc71 [file] [log] [blame]
package example
import grails.test.hibernate.HibernateSpec
import org.grails.datastore.mapping.config.Settings
import org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException
import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver
/**
* Created by graemerocher on 06/04/2017.
*/
class PartitionedMultiTenancySpec extends HibernateSpec {
BookService bookDataService = hibernateDatastore.getService(BookService)
@Override
Map getConfiguration() {
Collections.unmodifiableMap(
(Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver(),
(Settings.SETTING_DB_CREATE): "create-drop"
)
}
def cleanup() {
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "")
}
void "Test should rollback changes in a previous test"() {
when: "When there is no tenant"
Book.count()
then: "You still get an exception"
thrown(TenantNotFoundException)
when: "You can save a book"
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "moreBooks")
bookDataService.saveBook("The Stand")
then: "And the changes will be rolled back for the next test"
bookDataService.countBooks() == 1
}
void 'Test database per tenant'() {
when: "When there is no tenant"
Book.count()
then: "You still get an exception"
thrown(TenantNotFoundException)
when: "But look you can add a new Schema at runtime!"
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "moreBooks")
AnotherBookService bookService = new AnotherBookService()
then:
bookService.countBooks() == 0
bookDataService.countBooks() == 0
when: "And the new @CurrentTenant transformation deals with the details for you!"
bookService.saveBook("The Stand")
bookService.saveBook("The Shining")
bookService.saveBook("It")
then:
bookService.countBooks() == 3
bookDataService.countBooks() == 3
when: "Swapping to another schema and we get the right results!"
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "evenMoreBooks")
bookService.saveBook("Along Came a Spider")
bookDataService.saveBook("Whatever")
then:
bookService.countBooks() == 2
bookDataService.countBooks() == 2
}
}