Fixed / tested Steps::Create

pull/118/head
Rick Carlino 2015-01-11 08:47:35 -06:00
parent 5d912f55e7
commit 43ee0daeda
5 changed files with 54 additions and 11 deletions

View File

@ -7,6 +7,9 @@ class Step
embedded_in :sequence embedded_in :sequence
field :message_type field :message_type
field :time_stamp, type: Time validates :message_type, presence: true
field :command, type: Hash field :command, type: Hash
validates :command, presence: true
end end

View File

@ -3,19 +3,35 @@ module Steps
required do required do
string :message_type, in: Step::MESSAGE_TYPES string :message_type, in: Step::MESSAGE_TYPES
date :time_stamp hash(:command) { model :*, class: Object }
hash(:command) { :* }
model :sequence model :sequence
end end
def validate def execute
create(Step, inputs)
end end
def execute # TODO: Move this into a refinement
# STILL NEED TO: # This method provides a uniform way of
# * Make the steps mutation create a step # 1. Creating models inside of Mutations
# * Make the sequence mutation create one, too. # 2. presenting API model validation errors unifromly.
# This is important for keeping API msgs sane.
# Returns an instance of Klass -OR- a hash of model validation err. messages
# You may optionally pass in a block to transform the model / input
# before it gets saved.
# Ex: create(User, name: 'Rick', email: "r@mailinator.com") do |user, inputs|
# inputs.email.downcase!
# user.password = "SomethingThatYouPassIn"
# end
def create(klass, inputs = {})
binding.pry binding.pry
model = klass.new(inputs)
yield(model, inputs) if block_given?
if (model.valid? && model.save)
model
else
model.errors.messages
end
end end
end end
end end

View File

@ -9,6 +9,7 @@ describe Api::SequencesController do
let(:user) { FactoryGirl.create(:user) } let(:user) { FactoryGirl.create(:user) }
it 'creates a new sequences for a user' do it 'creates a new sequences for a user' do
pending "Going to create mutation level tests first."
sign_in user sign_in user
input = {name: "Scare Birds", input = {name: "Scare Birds",
steps: [{ steps: [{
@ -21,7 +22,6 @@ describe Api::SequencesController do
speed: 100, speed: 100,
delay: 0}}]} delay: 0}}]}
post :create, input post :create, input
binding.pry
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
end end

View File

@ -2,7 +2,8 @@
FactoryGirl.define do FactoryGirl.define do
factory :sequence do factory :sequence do
name name { Faker::Company.catch_phrase }
color color { Step::MESSAGE_TYPES.sample }
user
end end
end end

View File

@ -0,0 +1,23 @@
require 'spec_helper'
describe Steps::Create do
let(:user) { FactoryGirl.create(:user) }
let(:mutation) { Steps::Create }
let(:valid_params) { {message_type: 'move_rel',
sequence: FactoryGirl.create(:sequence),
command: {action: 'MOVE RELATIVE',
x: 1,
y: 2,
z: 3,
speed: 100,
delay: 0}} }
it 'Builds an instance of `step`' do
outcome = mutation.run(valid_params)
expect(outcome.success?).to be_truthy
expect(outcome.result.message_type).to eq(valid_params[:message_type])
expect(outcome.result.sequence).to eq(valid_params[:sequence])
cmd = outcome.result.command.deep_symbolize_keys
expect(cmd).to eq(valid_params[:command])
end
end