Skip to content

Instantly share code, notes, and snippets.

@stp-che
Last active October 8, 2019 09:13
Show Gist options
  • Save stp-che/6b79178acb25fdd8df7b28b59737ea2d to your computer and use it in GitHub Desktop.
Save stp-che/6b79178acb25fdd8df7b28b59737ea2d to your computer and use it in GitHub Desktop.
Role scopes manipulation testing strategy
describe SaveScoping do
let(:role){ create :role }
let(:cities){ (0..2).map{ create :city } }
let(:orgs){ (0..2).map{ create :legl_organization } }
context 'creating initial role scoping' do
it '...' do
expect {
SaveScoping.(role, RoleScope::CITY, [cities[0].id, cities[1].id])
}.to change{ role.reload.divided }.to true
expect(role.role_scopes.count).to eq 3
expect(role.root_scope.div_param).to eq RoleScope::CITY
expect(role.root_scope.children).to contain_exactly(
have_attributes(div_param: nil, city: city[0], legal_organization: nil),
have_attributes(div_param: nil, city: city[1], legal_organization: nil)
)
end
end
context 'creating role scoping for existing scope' do
before {
SaveScoping.(role, RoleScope::CITY, [cities[0].id, cities[1].id])
}
it '...' do
scope = role.role_scopes.where(city: city[1]).first
expect {
SaveScoping.(role, RoleScope::LEGAL_ORG, [orgs[0].id, orgs[1].id], scope)
}.to change{ role.role_scopes.count }.by 2
expect(role.root_scope.children).to contain_exactly(
have_attributes(div_param: nil, city: city[0], legal_organization: nil),
have_attributes(
div_param: RoleScope::LEGAL_ORG,
city: city[1],
legal_organization: nil,
children: contain_exactly(
have_attributes(div_param: nil, city: city[1], legal_organization: orgs[0]),
have_attributes(div_param: nil, city: city[1], legal_organization: orgs[1])
)
)
)
end
end
context 'changing existing scoping' do
before {
SaveScoping.(role, RoleScope::CITY, [cities[0].id, cities[1].id])
scope = role.role_scopes.where(city: city[1]).first
SaveScoping.(role, RoleScope::LEGAL_ORG, [orgs[0].id, orgs[1].id], scope)
}
context 'changing set of scopes' do
it '...' do
scope = role.role_scopes.where(city: city[1]).first
expect {
SaveScoping.(role, RoleScope::LEGAL_ORG, [orgs[0].id, orgs[2].id], scope)
}.to_not change{ role.role_scopes.count }
expect(role.root_scope.children).to contain_exactly(
have_attributes(div_param: nil, city: city[0], legal_organization: nil),
have_attributes(
div_param: RoleScope::LEGAL_ORG,
city: city[1],
legal_organization: nil,
children: contain_exactly(
have_attributes(div_param: nil, city: city[1], legal_organization: orgs[0]),
have_attributes(div_param: nil, city: city[1], legal_organization: orgs[2])
)
)
)
expect {
SaveScoping.(role, RoleScope::CITY, [cities[2].id])
}.to_not change{ role.role_scopes.count }.by -3
expect(role.root_scope.children).to contain_exactly(
have_attributes(div_param: nil, city: city[2], legal_organization: nil),
)
end
context 'when new set is empty' do
it '...' do
scope = role.role_scopes.where(city: city[1]).first
expect {
SaveScoping.(role, RoleScope::LEGAL_ORG, [], scope)
}.to change{ role.role_scopes.count }.by -2
expect(role.root_scope.children).to contain_exactly(
have_attributes(div_param: nil, city: city[0], legal_organization: nil),
have_attributes(div_param: nil, city: city[1], legal_organization: nil)
)
end
context 'when scope is root' do
it '...' do
expect {
SaveScoping.(role, RoleScope::CITY, [], role.root_scope)
}.to change{ role.reload.divided }.to false
expect(role.role_scopes.count).to eq 0
end
end
end
end
context 'changing div_param' do
before {
SaveScoping.(role, RoleScope::CITY, [cities[0].id, cities[1].id])
scope = role.role_scopes.where(city: city[1]).first
SaveScoping.(role, RoleScope::LEGAL_ORG, [orgs[0].id, orgs[1].id], scope)
}
it '...' do
expect {
SaveScoping.(role, RoleScope::LEGAL_ORG, [orgs[0].id, orgs[1].id])
}.to chnge{ role.role_scopes.count }.by -2
expect(role.root_scope.div_param).to eq RoleScope::LEGAL_ORG
expect(role.root_scope.children).to contain_exactly(
have_attributes(div_param: nil, city: nil, legal_organization: orgs[0])
have_attributes(div_param: nil, city: nil, legal_organization: orgs[1]),
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment