Specs for backend
parent
fd5aea8013
commit
2e7db6760e
|
@ -2,33 +2,30 @@
|
|||
# configuration pairs.
|
||||
module Api
|
||||
class DeviceConfigsController < Api::AbstractController
|
||||
|
||||
def create
|
||||
mutate DeviceConfigs::Create.run(params.as_json, device: current_device)
|
||||
end
|
||||
|
||||
def index
|
||||
render json: configs
|
||||
end
|
||||
|
||||
def show
|
||||
render json: config
|
||||
render json: device_configs
|
||||
end
|
||||
|
||||
def update
|
||||
mutate DeviceConfigs::Update.run(params.as_json, config: config)
|
||||
mutate DeviceConfigs::Update.run(raw_json, config: device_config)
|
||||
end
|
||||
|
||||
def destroy
|
||||
render json: config.destroy! && ""
|
||||
render json: (device_config.destroy! && "")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def config
|
||||
configs.find(params[:id])
|
||||
def device_config
|
||||
device_configs.find(params[:id])
|
||||
end
|
||||
|
||||
def configs
|
||||
def device_configs
|
||||
current_device.device_configs
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,6 +19,7 @@ class Device < ApplicationRecord
|
|||
has_many :images, dependent: :destroy
|
||||
has_many :webcam_feeds, dependent: :destroy
|
||||
has_many :sensor_readings, dependent: :destroy
|
||||
has_many :device_configs, dependent: :destroy
|
||||
validates_presence_of :name
|
||||
validates :timezone,
|
||||
inclusion: { in: TIMEZONES, message: BAD_TZ, allow_nil: true }
|
||||
|
|
|
@ -15,7 +15,7 @@ FarmBot::Application.routes.draw do
|
|||
sequences: [:create, :destroy, :index, :show, :update],
|
||||
tools: [:create, :destroy, :index, :show, :update],
|
||||
webcam_feeds: [:create, :destroy, :index, :show, :update],
|
||||
device_configs: [:create, :destroy, :index, :show],
|
||||
device_configs: [:create, :destroy, :index, :update],
|
||||
}.to_a.map { |(name, only)| resources name, only: only }
|
||||
|
||||
# Singular API Resources:
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Api::DeviceConfigsController do
|
||||
let(:device) { FactoryBot.create(:device) }
|
||||
let(:user) { FactoryBot.create(:user, device: device) }
|
||||
|
||||
include Devise::Test::ControllerHelpers
|
||||
|
||||
it 'creates a device config' do
|
||||
sign_in user
|
||||
b4 = DeviceConfig.count
|
||||
input = { key: "Coffee Emoji", value: "☕" }
|
||||
post :create, params: input
|
||||
expect(response.status).to eq(200)
|
||||
expect(DeviceConfig.count).to be > b4
|
||||
input.keys.map { |key| expect(json[key]).to eq(input[key]) }
|
||||
end
|
||||
|
||||
it 'lists' do
|
||||
sign_in user
|
||||
FactoryBot.create_list(:device_config, 5, device: device)
|
||||
get :index
|
||||
expect(json.length).to eq(5)
|
||||
end
|
||||
|
||||
it 'updates' do
|
||||
sign_in user
|
||||
device_config = FactoryBot.create(:device_config, device: device)
|
||||
input = { key: device_config.key.reverse,
|
||||
value: device_config.value.reverse }
|
||||
put :update,
|
||||
body: input.to_json,
|
||||
params: { id: device_config.id }
|
||||
expect(response.status).to be(200)
|
||||
input.keys.map { |key| expect(json[key]).to eq(input[key]) }
|
||||
end
|
||||
|
||||
it 'deletes' do
|
||||
sign_in user
|
||||
device_config = FactoryBot.create(:device_config, device: device)
|
||||
id = device_config.id
|
||||
delete :destroy, params: { id: device_config.id }
|
||||
expect(response.status).to be(200)
|
||||
expect(DeviceConfig.exists?(id)).to be false
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue