Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
tests organizationUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Aug 1, 2018
1 parent ee3a627 commit e5ab4e6
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 1 deletion.
106 changes: 106 additions & 0 deletions server/graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,112 @@ func TestMutations(t *testing.T) {
return m
}(),
},
"organizationUpdate/without_complement": testCase{
token: dToken,
mutation: `mutation {
viewer {
organizationUpdate(input: {
name: "new name",
address: {
withoutComplement: true
}
}) {
organization {
name, address {complement}
}
}
}
}`,
response: `{"data":{"viewer":{"organizationUpdate":{"organization":{
"name": "new name", "address" : {"complement": null}
}}}}}`,
orgRepoMock: func() *orgRepoMock {
m := &orgRepoMock{}
m.On("Get", int64(1)).Once().
Return(
&model.Organization{
User: model.User{
ID: 1,
Email: "[email protected]",
},
Name: "old name",
About: "old about",
Phone: "",
Video: "http://video.com/wv",
Address: model.Address{
Street: "Rua Algum Lugar",
Number: "500",
Neighborhood: "Algum Bairro Antigo",
City: "Curitiba",
State: "PR",
Zipcode: "90230000",
Complement: nulls.NewString("Perto do Obelisco"),
},
},
nil,
)

oUpdated := model.Organization{
User: model.User{
ID: 1,
Email: "[email protected]",
},
Name: "new name",
About: "old about",
Phone: "",
Video: "http://video.com/wv",
Address: model.Address{
Street: "Rua Algum Lugar",
Number: "500",
Neighborhood: "Algum Bairro Antigo",
City: "Curitiba",
State: "PR",
Zipcode: "90230000",
Complement: nulls.String{Valid: false},
},
}

m.On("Update", oUpdated).Once().
Return(oUpdated, nil)

return m
}(),
},
"organizationUpdate/fails_when_withoutComplement_and_complement": testCase{
token: dToken,
mutation: `mutation {
viewer {
organizationUpdate(input: {
name: "new name",
address: {
withoutComplement: true,
complement: "Olhe para cima"
}
}) {
organization {
name, address {complement}
}
}
}
}`,
response: `{"data":{"viewer":{"organizationUpdate": null }}, "errors": [
{ "message": "parameters withoutComplement and complement can't be used together", "locations":[] }
]}`,
orgRepoMock: func() *orgRepoMock {
m := &orgRepoMock{}
m.On("Get", int64(1)).Once().
Return(
&model.Organization{
User: model.User{
ID: 1,
Email: "[email protected]",
},
},
nil,
)
return m
}(),
},
}

for name, test := range tests {
Expand Down
14 changes: 13 additions & 1 deletion server/graphql/organizationUpdateMutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ var addressInput = graphql.NewInputObject(graphql.InputObjectConfig{
"city": stringInput,
"state": stringInput,
"zipcode": stringInput,
"withoutComplement": &graphql.InputObjectFieldConfig{
Type: graphql.Boolean,
Description: "When set to true, will make the address without a complement",
DefaultValue: false,
},
},
})

Expand Down Expand Up @@ -109,10 +114,17 @@ func newOrganizationUpdateMutation(update updateOrgFn) *graphql.Field {
o.Address.Zipcode = zipcode
}

withoutComplement := address["withoutComplement"].(bool)
if withoutComplement {
o.Address.Complement = nulls.String{Valid: false}
}

if complement, ok := address["complement"].(string); ok {
if withoutComplement {
return nil, errors.New("parameters withoutComplement and complement can't be used together")
}
o.Address.Complement = nulls.NewString(complement)
}

}

var err error
Expand Down

0 comments on commit e5ab4e6

Please sign in to comment.