Skip to content

Commit

Permalink
Merge pull request grails-samples#22 from caseyscarborough/master
Browse files Browse the repository at this point in the history
Fix adding/editing pets and add functional tests
  • Loading branch information
graemerocher committed Aug 29, 2014
2 parents 5d8b835 + 36dfa36 commit 2568b73
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class PetController {
return [pet: new Pet(owner: Owner.get(params.owner?.id)), types: PetType.list()]
}

def pet = petclinicService.createPet(params.pet?.name, params.pet?.birthDate,
(params.pet?.type?.id ?: 0) as Long, (params.pet?.owner?.id ?: 0) as Long)
def pet = petclinicService.createPet(params.pet_name, params.pet?.birthDate,
(params.pet?.type?.id ?: 0) as Long, (params.pet_owner_id ?: 0) as Long)

if (pet.hasErrors()) {
return [pet: pet, types: PetType.list()]
Expand All @@ -27,8 +27,8 @@ class PetController {

def pet = Pet.get(params.id)

petclinicService.updatePet(pet, params.pet?.name, params.pet?.birthDate,
(params.pet?.type?.id ?: 0) as Long, (params.pet?.owner?.id ?: 0) as Long)
petclinicService.updatePet(pet, params.pet_name, params.pet?.birthDate,
(params.pet?.type?.id ?: 0) as Long, (params.pet_owner_id ?: 0) as Long)

if (pet.hasErrors()) {
render view: 'add', model: [pet: pet, types: PetType.list()]
Expand Down
2 changes: 1 addition & 1 deletion grails-app/views/pet/add.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<g:if test="${pet.id}">
<g:hiddenField name="id" value="${pet.id}" />
</g:if>
<g:hiddenField name="pet.owner.id" value="${pet.owner.id}"></g:hiddenField>
<g:hiddenField name="pet_owner_id" value="${pet.owner.id}"></g:hiddenField>
<table>
<tr>
<th>
Expand Down
52 changes: 52 additions & 0 deletions test/functional/org/grails/samples/AddPetSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.grails.samples

import org.grails.samples.pages.AddPetPage
import org.grails.samples.pages.AddOwnerPage
import org.grails.samples.pages.ShowOwnerPage
import spock.lang.Shared

class AddPetSpec extends PetclinicSpecs {

@Shared
def ownerId

def setupSpec() {
addOwner()
}

def setup() {
to AddPetPage, "?owner.id=${ownerId}"
}

def 'can NOT add an invalid Pet'() {
when:
addPet.click()

then:
at AddPetPage
errors.size() == 3
}

def 'can add a valid Pet'() {
given:
name.value 'Fido'
id.value ownerId

when:
addPet.click()

then:
at ShowOwnerPage
}

protected void addOwner() {
to AddOwnerPage
firstName.value 'Sally'
lastName.value 'Jones'
address.value '987 State St.'
city.value 'MSN'
telephone.value '1234567890'
addOwner.click()
ownerId = $("form").find("input", name: "owner.id").value()
}
}
62 changes: 62 additions & 0 deletions test/functional/org/grails/samples/EditPetSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.grails.samples

import org.grails.samples.pages.AddOwnerPage
import org.grails.samples.pages.AddPetPage
import org.grails.samples.pages.EditPetPage
import org.grails.samples.pages.ShowOwnerPage
import spock.lang.Shared

class EditPetSpec extends PetclinicSpecs {

@Shared
def ownerId

def setupSpec() {
addOwner()
addPetToOwner(ownerId)
}

def setup() {
to EditPetPage, 1
}

def 'can NOT edit an invalid Pet'() {
when:
name.value ''
editPet.click()

then:
at EditPetPage
errors.size() == 3
}

def 'can edit a valid Pet'() {
given:
name.value 'Fido'
id.value ownerId

when:
editPet.click()

then:
at ShowOwnerPage
}

protected void addOwner() {
to AddOwnerPage
firstName.value 'Sally'
lastName.value 'Jones'
address.value '987 State St.'
city.value 'MSN'
telephone.value '1234567890'
addOwner.click()
ownerId = $("form").find("input", name: "owner.id").value()
}

protected void addPetToOwner(ownerId) {
to AddPetPage, "?owner.id=${ownerId}"
name.value 'Fido'
id.value ownerId
addPet.click()
}
}
17 changes: 17 additions & 0 deletions test/functional/org/grails/samples/pages/AddPetPage.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.grails.samples.pages

class AddPetPage extends PageWithFooter {

static url = '/petclinic/pet/add'

static at = {
title == 'Add Pet'
}

static content = {
name { $('#pet_name') }
id { $('#pet_owner_id') }
addPet { $('input', type: 'submit') }
errors(required: false) { $('.errors') }
}
}
17 changes: 17 additions & 0 deletions test/functional/org/grails/samples/pages/EditPetPage.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.grails.samples.pages

class EditPetPage extends PageWithFooter {

static url = '/petclinic/pet/edit'

static at = {
title == 'Update Pet'
}

static content = {
name { $('#pet_name') }
id { $('#pet_owner_id') }
editPet { $('input', type: 'submit') }
errors(required: false) { $('.errors') }
}
}

0 comments on commit 2568b73

Please sign in to comment.