Commit 5c9ff2b5 by John Doe

refactored tests

parent 52247d85
...@@ -46,11 +46,12 @@ class NilmsController < ApplicationController ...@@ -46,11 +46,12 @@ class NilmsController < ApplicationController
@service = StubService.new @service = StubService.new
# redundant since the user must be an owner... # redundant since the user must be an owner...
@role = current_user.get_nilm_permission(@nilm) @role = current_user.get_nilm_permission(@nilm)
if @nilm.update(nilm_params) and @nilm.db.update(db_params) if @nilm.update(nilm_params) && @db.update(db_params)
@service.add_notice('Installation Updated') @service.add_notice('Installation Updated')
render :show, status: :ok render :show, status: :ok
else else
@service.errors = @nilm.errors.full_messages @service.errors = @nilm.errors.full_messages +
@db.errors.full_messages
render :show, status: :unprocessable_entity render :show, status: :unprocessable_entity
end end
end end
......
...@@ -2,12 +2,16 @@ ...@@ -2,12 +2,16 @@
json.data do json.data do
json.extract! @nilm, *Nilm.json_keys json.extract! @nilm, *Nilm.json_keys
json.role @role json.role @role
if @nilm.db != nil
json.max_points_per_plot @nilm.db.max_points_per_plot json.max_points_per_plot @nilm.db.max_points_per_plot
json.root_folder do json.root_folder do
if @nilm.db.root_folder != nil
json.partial! 'db_folders/db_folder', json.partial! 'db_folders/db_folder',
db_folder: @nilm.db.root_folder, db_folder: @nilm.db.root_folder,
nilm: @nilm nilm: @nilm
end end
end
end
json.jouleModules(@nilm.joule_modules) do |m| json.jouleModules(@nilm.joule_modules) do |m|
json.extract! m, *JouleModule.json_keys json.extract! m, *JouleModule.json_keys
json.nilm_id @nilm.id json.nilm_id @nilm.id
......
...@@ -67,22 +67,22 @@ RSpec.describe NilmsController, type: :request do ...@@ -67,22 +67,22 @@ RSpec.describe NilmsController, type: :request do
it 'returns 422 on invalid db parameters' do it 'returns 422 on invalid db parameters' do
# max points must be a positive number # max points must be a positive number
put "/nilms/#{john_nilm.id}.json", put "/nilms/#{john_nilm.id}.json",
params: {db: {max_points_per_plot: 'invalid'}}, params: {max_points_per_plot: 'invalid'},
headers: john.create_new_auth_token headers: john.create_new_auth_token
expect(response.status).to eq(422) expect(response.status).to eq(422)
expect(response).to have_error_message(/not a number/) expect(response).to have_error_message(/not a number/)
end end
it 'only allows configurable db parameters to be changed' do it 'only allows configurable db parameters to be changed' do
# should ignore size and accept max_points # should ignore url and accept max_points
url = john_nilm.db.url size_db = john_nilm.db.size_db
num_points = john_nilm.db.max_points_per_plot num_points = john_nilm.db.max_points_per_plot
put "/nilms/#{john_nilm.id}.json", put "/nilms/#{john_nilm.id}.json",
params: {db: {max_points_per_plot: num_points+10, url: 'different'}}, params: {max_points_per_plot: num_points+10, size: 'different'},
headers: john.create_new_auth_token headers: john.create_new_auth_token
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(response).to have_notice_message() expect(response).to have_notice_message()
expect(john_nilm.db.reload.url).to eq(url) expect(john_nilm.db.reload.size_db).to eq(size_db)
expect(john_nilm.db.max_points_per_plot).to eq(num_points+10) expect(john_nilm.db.max_points_per_plot).to eq(num_points+10)
end end
end end
...@@ -113,7 +113,7 @@ RSpec.describe NilmsController, type: :request do ...@@ -113,7 +113,7 @@ RSpec.describe NilmsController, type: :request do
describe 'GET show' do describe 'GET show' do
context 'with any permissions' do context 'with any permissions' do
it 'returns nilm and nested db as json' do it 'returns nilm and nested root folder as json' do
# john has some permission on all 3 nilms # john has some permission on all 3 nilms
[pete_nilm, lab_nilm, john_nilm].each do |nilm| [pete_nilm, lab_nilm, john_nilm].each do |nilm|
get "/nilms/#{nilm.id}.json", get "/nilms/#{nilm.id}.json",
...@@ -122,7 +122,7 @@ RSpec.describe NilmsController, type: :request do ...@@ -122,7 +122,7 @@ RSpec.describe NilmsController, type: :request do
expect(response.header['Content-Type']).to include('application/json') expect(response.header['Content-Type']).to include('application/json')
body = JSON.parse(response.body) body = JSON.parse(response.body)
expect(body['data']['name']).to eq(nilm.name) expect(body['data']['name']).to eq(nilm.name)
expect(body['data']['db']['url']).to eq(nilm.db.url) expect(body['data']['root_folder']['name']).to_not be_empty
end end
end end
it 'returns joule modules as json' do it 'returns joule modules as json' do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment