Stub out Devices#seed endpoint

pull/1145/head
Rick Carlino 2019-04-10 15:40:31 -05:00
parent b42a0aee10
commit 42e5d85171
5 changed files with 62 additions and 13 deletions

View File

@ -34,7 +34,12 @@ module Api
mutate Devices::Sync.run(device: current_device)
end
private
def seed
Devices::SeedData.delay.run!(params.as_json, device: current_device)
render json: { done: "Loading resources now." }
end
private
# Store the JSON on the local filesystem for self hosted users that don't
# have email set up.

View File

@ -0,0 +1,24 @@
module Devices
class SeedData < Mutations::Command
PRODUCT_LINES = ["genesis"]
required do
model :device
string :product_line, in: PRODUCT_LINES
end
def execute
add_plants
end
private
def add_plants
Plant.create!(device: device,
x: rand(40...1500),
y: rand(40...800),
radius: rand(30...60),
name: "Celery",
openfarm_slug: "celery")
end
end
end

View File

@ -58,8 +58,9 @@ FarmBot::Application.routes.draw do
patch :apply, on: :member
end
get "/global_config" => "global_config#show", as: :global_config
get "/device/sync" => "devices#sync", as: :device_sync
get "/global_config" => "global_config#show", as: :global_config
get "/device/sync" => "devices#sync", as: :device_sync
post "/device/seed" => "devices#seed", as: :device_seed
# Make life easier on API users by not adding special rules for singular
# resources.

View File

@ -6,11 +6,10 @@ if Rails.env == "development"
ENV["MQTT_HOST"] = "blooper.io"
ENV["OS_UPDATE_SERVER"] = "http://example-server.com"
# CREDIT: Faker Ruby Gem
VEGGIES = %w(artichoke arugula asparagus broccoli
cabbage carrot cauliflower celery chive cucumber
eggplant endive garlic jicama kale kohlrabi leek lettuce okra onion
parsnip pepper potato pumpkin radicchio radish raspberry rhubarb spinach
squash tomato turnip zucchini)
VEGGIES = %w(artichoke arugula asparagus broccoli cabbage carrot cauliflower
celery cucumber eggplant garlic kale kohlrabi leek lettuce okra
parsnip potato pumpkin radicchio radish raspberry spinach squash
tomato turnip zucchini)
[
Sensor,
@ -60,12 +59,11 @@ if Rails.env == "development"
z: rand(1...300) })
end
PLANT_COUNT.times do
veggie = VEGGIES.sample
VEGGIES.each do |veggie|
Plant.create(device: u.device,
x: rand(40...970),
y: rand(40...470),
radius: rand(10...50),
x: rand(40...1500),
y: rand(40...800),
radius: rand(30...60),
name: veggie,
openfarm_slug: veggie.downcase.gsub(" ", "-"))
end

View File

@ -0,0 +1,21 @@
require 'spec_helper'
describe Api::DevicesController do
include Devise::Test::ControllerHelpers
let(:user) { FactoryBot.create(:user) }
let(:device) { user.device }
describe '#seed' do
it "seeds accounts with default data" do
sign_in user
device = user.device
expect(device.plants.count).to eq(0)
run_jobs_now do
post :seed, params: { product_line: "genesis" }
end
expect(response.status).to eq(200)
expect(device.reload.plants.count).to eq(1)
end
end
end