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
field :message_type
field :time_stamp, type: Time
validates :message_type, presence: true
field :command, type: Hash
validates :command, presence: true
end

View File

@ -3,19 +3,35 @@ module Steps
required do
string :message_type, in: Step::MESSAGE_TYPES
date :time_stamp
hash(:command) { :* }
hash(:command) { model :*, class: Object }
model :sequence
end
def validate
def execute
create(Step, inputs)
end
def execute
# STILL NEED TO:
# * Make the steps mutation create a step
# * Make the sequence mutation create one, too.
# TODO: Move this into a refinement
# This method provides a uniform way of
# 1. Creating models inside of Mutations
# 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
model = klass.new(inputs)
yield(model, inputs) if block_given?
if (model.valid? && model.save)
model
else
model.errors.messages
end
end
end
end

View File

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

View File

@ -2,7 +2,8 @@
FactoryGirl.define do
factory :sequence do
name
color
name { Faker::Company.catch_phrase }
color { Step::MESSAGE_TYPES.sample }
user
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