Skip to content

Commit

Permalink
Adding in specs for params has_key_for? method (#505)
Browse files Browse the repository at this point in the history
* Adding in specs for params has_key_for? method. Fixes #503

* Adding in FakeParams for testing. This cleans up some specs using the fake params
  • Loading branch information
jwoertink authored Oct 30, 2020
1 parent b7a915e commit 255d4f1
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 23 deletions.
24 changes: 1 addition & 23 deletions spec/operations/nested_save_operation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ private class SaveBusiness < Business::SaveOperation
end
end

private class NestedParams
include Avram::Paramable

private class NestedParams < Avram::FakeParams
@business : Hash(String, String) = {} of String => String
@email_address : Hash(String, String) = {} of String => String
@tax_id : Hash(String, String) = {} of String => String
Expand All @@ -35,26 +33,6 @@ private class NestedParams
raise "What is this key!? #{key}"
end
end

def nested(key : String) : Hash(String, String)
nested?(key)
end

def get?(key)
raise "Not implemented"
end

def get(key)
raise "Not implemented"
end

def nested_file?(key)
raise "Not implemented"
end

def nested_file(key)
raise "Not implemented"
end
end

describe "Avram::SaveOperation with nested operation" do
Expand Down
74 changes: 74 additions & 0 deletions spec/params_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require "./spec_helper"

private class TestOperationWithDefaultParamKey < Avram::Operation
attribute title : String

def run
end
end

private class TestOperationWithCustomParamKey < Avram::Operation
param_key :test_op
attribute title : String

def run
end
end

private class SaveUser < User::SaveOperation
end

private class NestedParams < Avram::FakeParams
@data : Hash(String, String) = {} of String => String

def initialize(@data)
end

def nested?(key : String) : Hash(String, String)
if @data.keys.map(&.split(':').first).includes?(key)
@data
else
{} of String => String
end
end
end

describe Avram::Paramable do
describe "#has_key_for?" do
it "returns true for the Operation with the proper key" do
params = NestedParams.new({"test_operation_with_default_param_key:title" => "Test"})

params.has_key_for?(TestOperationWithDefaultParamKey).should be_true
end

it "returns true for the Operation with a custom key" do
params = NestedParams.new({"test_op:title" => "Test"})

params.has_key_for?(TestOperationWithCustomParamKey).should be_true
end

it "returns false for the Operation with the improper key" do
params = NestedParams.new({"bad_key:title" => "Test"})

params.has_key_for?(TestOperationWithDefaultParamKey).should be_false
end

it "returns false for the Operation with no key" do
params = NestedParams.new({"title" => "Test"})

params.has_key_for?(TestOperationWithDefaultParamKey).should be_false
end

it "returns true for the SaveOperation with the proper key" do
params = NestedParams.new({"user:name" => "Test"})

params.has_key_for?(SaveUser).should be_true
end

it "returns false for the SaveOperation with the improper key" do
params = NestedParams.new({"author:name" => "Test"})

params.has_key_for?(SaveUser).should be_false
end
end
end
25 changes: 25 additions & 0 deletions spec/support/fake_params.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
abstract class Avram::FakeParams
include Avram::Paramable

abstract def nested?(key : String) : Hash(String, String)

def nested(key : String) : Hash(String, String)
nested?(key)
end

def get?(key)
raise "Not implemented"
end

def get(key)
raise "Not implemented"
end

def nested_file?(key)
raise "Not implemented"
end

def nested_file(key)
raise "Not implemented"
end
end

0 comments on commit 255d4f1

Please sign in to comment.