Skip to content

Commit

Permalink
Adding new has_key_for? method to params. Fixes Avram #72
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoertink committed Oct 23, 2020
1 parent dd06b41 commit 15e7f5f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
74 changes: 74 additions & 0 deletions spec/lucky/params_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@ require "../spec_helper"
include ContextHelper
include MultipartHelper

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 User < Avram::Model
table do
column name : String
end
end

private class SaveUser < User::SaveOperation
end

describe Lucky::Params do
describe "#from_query" do
it "returns the HTTP::Params for the query params" do
Expand Down Expand Up @@ -598,4 +622,54 @@ describe Lucky::Params do
params.should eq({"filter" => {"name" => "euphonium"}, "page" => "1", "per" => "50"})
end
end

describe "#has_key_for?" do
it "returns true for the Operation with the proper key" do
request = build_request body: "", content_type: ""
request.query = "test_operation_with_default_param_key:title=Test"
params = Lucky::Params.new(request)

params.has_key_for?(TestOperationWithDefaultParamKey).should be_true
end

it "returns true for the Operation with a custom key" do
request = build_request body: "", content_type: ""
request.query = "test_op:title=Test"
params = Lucky::Params.new(request)

params.has_key_for?(TestOperationWithCustomParamKey).should be_true
end

it "returns false for the Operation with the improper key" do
request = build_request body: "", content_type: ""
request.query = "bad_key:title=Test"
params = Lucky::Params.new(request)

params.has_key_for?(TestOperationWithDefaultParamKey).should be_false
end

it "returns false for the Operation with no key" do
request = build_request body: "", content_type: ""
request.query = "title=Test"
params = Lucky::Params.new(request)

params.has_key_for?(TestOperationWithDefaultParamKey).should be_false
end

it "returns true for the SaveOperation with the proper key" do
request = build_request body: "", content_type: ""
request.query = "user:name=Test"
params = Lucky::Params.new(request)

params.has_key_for?(SaveUser).should be_true
end

it "returns false for the SaveOperation with the improper key" do
request = build_request body: "", content_type: ""
request.query = "author:name=Test"
params = Lucky::Params.new(request)

params.has_key_for?(SaveUser).should be_false
end
end
end
4 changes: 4 additions & 0 deletions src/lucky/params.cr
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ class Lucky::Params
end
end

def has_key_for?(operation : Avram::Operation.class | Avram::SaveOperation.class) : Bool
nested?(operation.param_key).any?
end

private def nested_json_params(nested_key : String) : Hash(String, String)
nested_params = {} of String => String
nested_key_json = parsed_json[nested_key]? || JSON.parse("{}")
Expand Down

0 comments on commit 15e7f5f

Please sign in to comment.