[STABLE] First batch of tests for PlantTemplates

pull/796/head
Rick Carlino 2018-04-19 09:43:55 -05:00
parent 7179e99759
commit 2e5f278f6a
9 changed files with 185 additions and 6 deletions

View File

@ -1,4 +1,31 @@
module Api
class PlantTemplatesController < Api::AbstractController
def index
render json: current_device.plant_templates
end
def create
mutate PlantTemplates::Create.run(raw_json, device: current_device)
end
def update
mutate PlantTemplates::Update.run(raw_json,
plant_template: plant_template,
device: current_device)
end
def destroy
render json: plant_template.destroy! && ""
end
private
def plant_templates
@plant_templates ||= current_device.plant_templates
end
def plant_template
@plant_template ||= plant_templates.find(params[:id])
end
end
end

View File

@ -15,6 +15,7 @@ class Device < ApplicationRecord
has_many :peripherals, dependent: :destroy
has_many :pin_bindings, dependent: :destroy
has_many :points, dependent: :destroy
has_many :plant_templates, dependent: :destroy
has_many :regimens, dependent: :destroy
has_many :sensor_readings, dependent: :destroy
has_many :sensors, dependent: :destroy
@ -22,6 +23,7 @@ class Device < ApplicationRecord
has_many :token_issuances, dependent: :destroy
has_many :tools, dependent: :destroy
has_many :webcam_feeds, dependent: :destroy
has_many :users
validates_presence_of :name
validates :timezone,

View File

@ -0,0 +1,4 @@
class PlantTemplate < ApplicationRecord
belongs_to :device
belongs_to :saved_garden
end

View File

@ -0,0 +1,29 @@
module PlantTemplates
class Create < Mutations::Command
required do
integer :saved_garden_id
model :device, class: Device
float :x
float :y
end
optional do
string :openfarm_slug # "null"
string :name # "untitled"
float :z # 0.0
float :radius # 25
end
def execute
PlantTemplate.create!(creation_params)
end
private
def creation_params
@creation_params ||= inputs.except(:saved_garden_id).merge({
saved_garden: device.saved_gardens.find(saved_garden_id)
})
end
end
end

View File

@ -0,0 +1,37 @@
module PlantTemplates
class Update < Mutations::Command
required do
model :device, class: Device
model :plant_template, class: PlantTemplate
end
optional do
float :radius
float :x
float :y
float :z
integer :saved_garden_id
string :name
string :openfarm_slug
end
def execute
plant_template.update_attributes!(update_params)
plant_template
end
def update_params
@update_params ||= inputs
.except(:saved_garden_id, :device, :plant_template)
.merge(saved_garden)
end
def saved_garden
if saved_garden_id
{ saved_garden: device.saved_gardens.find(:saved_garden_id) }
else
{}
end
end
end
end

View File

@ -16,7 +16,7 @@ FarmBot::Application.routes.draw do
webcam_feeds: [:create, :destroy, :index, :show, :update],
device_configs: [:create, :destroy, :index, :update],
saved_gardens: [:create, :destroy, :index, :update],
plant_templats: [],
plant_templates: [:create, :destroy, :index, :update],
pin_bindings: [:create, :destroy, :index, :show, :update]
}.to_a.map { |(name, only)| resources name, only: only }

View File

@ -0,0 +1,79 @@
require "spec_helper"
describe Api::PlantTemplatesController do
include Devise::Test::ControllerHelpers
let(:user) do
FactoryBot.create(:user)
end
let (:saved_garden) do
FactoryBot.create(:saved_garden, device: user.device)
end
let(:plant_templates) do
FactoryBot.create_list(:plant_template, 3, device: user.device)
end
describe "#index" do
it "shows all plant_templates" do
sign_in user
templates_count = plant_templates.count
get :index
expect(response.status).to be(200)
expect(json.length).to be(templates_count)
this = json.first
that = PlantTemplate.find(this[:id])
[:x, :y, :z, :radius, :openfarm_slug].map do |key|
expect(this[key]).to eq(that[key])
end
end
end
describe "#create" do
it "creates a plant template" do
sign_in user
b4 = user.device.plant_templates.count
params = {name: Faker::StarWars.call_sign,
saved_garden_id: saved_garden.id,
x: 1,
y: 2,
z: 3,
openfarm_slug: "tomato",
radius: 32}
post :create, params: {format: :json}, body: params.to_json
expect(response.status).to be(200)
params.map { |(key,value)| expect(json[key]).to eq(value) }
expect(user.device.plant_templates.count).to be > b4
end
end
describe "#update" do
it "updates attributes" do
sign_in user
plant_template = plant_templates.first
b4 = plant_template.name
params = {name: Faker::StarWars.call_sign,
x: 9,
y: 10,
z: 11,
openfarm_slug: "melon",
radius: 32}
put :update, params: { format: :json, id: plant_template.id },
body: params.to_json
expect(response.status).to be(200)
params.map { |(key,value)| expect(json[key]).to eq(value) }
end
end
describe "#destroy" do
it "destroys" do
sign_in user
garden = plant_templates.first
b4 = plant_templates.length
delete :destroy, params: { id: garden.id }
expect(response.status).to be(200)
expect(user.device.plant_templates.count).to be < b4
end
end
end

View File

@ -1,10 +1,11 @@
FactoryBot.define do
factory :plant_template do
radius 1.5
x { rand(1...550) }
y { rand(1...550) }
z { rand(1...550) }
device
openfarm_slug "lettuce"
radius 1.5
x { rand(1...550) }
y { rand(1...550) }
z { rand(1...550) }
saved_garden { FactoryBot.create(:saved_garden, device: device) }
device
end
end