hashr
hashr
Hashr is a tiny class makes test on JSON response easier.
The name of hashr
come from the awesome ruby gem hashr, though, AFAIK, use original code is not possible because Crystal very different with Ruby in some aspect.
This shard should only be used in spec.
Installation
-
Add the dependency to your
shard.yml
:dependencies: hashr: github: crystal-china/hashr
-
Run
shards install
Usage
Following is a example use graphql + spec-kemal
require "hashr"
require "../spec_helper"
describe "daily reports" do
it "query daily report" do
report = ReportFactory.create
post_json "/graphql", body: {query: "query { ... }"}
p! typeof(response.body) # => String
p! response.body # => "{\"data\":{\"reportQuery\":{\"target\":{\"targetTotalCount\":47,\"processedTotalCount\":44,\"qualifiedTotalCount\":40}}}}"
response_hash = Hash(String, JSON::Any).from_json(response.body) # => Get a hash like this:
# {"data" => {
# "reportQuery" => {
# "target" => {
# "targetTotalCount" => report.target_total_count,
# "processedTotalCount" => report.processed_total_count,
# "qualifiedTotalCount" => report.qualified_total_count
# }
# }
# }
# }
# Instead, verify on the entire response result, we can verify on specified field only.
parsed_response = Hashr.new(response)
# Use nice dot method call.
target = parsed_response.data.reportQuery.target
target.processedTotalCount.should eq report.processed_total_count
target.qualifiedTotalCount.should eq report.qualified_total_count
end
end
You can use Hashr as model object, like this:
it "use hashr as model object" do
contacts = [
{
id: 1,
first: "billy",
last: "zheng",
phone: "18612385678",
email: "billy@gmail.com",
},
{
id: 2,
first: "xuan",
last: "zheng",
phone: "18512345678",
email: "retired@qq.com",
},
]
models = contacts.map { |e| Hashr.new(e) }
models.each do |contact|
puts "#{contact.first}.#{contact.last}: phone: #{contact.phone}, email: #{contact.email}"
end
billy = models.first
billy.phone = "13012345678"
p billy.name # => 13012345678
Limit
For verify a value is nil, you have to use eq
, be_nil
not work because Crystal don't allow us redefine #nil? method on any object.
h = {"nilValue" => nil}.to_json
value = Hashr.new({"foo" => JSON.parse(h)})
value.foo.nilValue.should eq nil # => true
value.foo.nilValue.should be_nil # => false
Development
TODO: Write development instructions here
Contributing
- Fork it (https://github.com/crystal-china/hashr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Billy.Zheng - creator and maintainer
Repository
hashr
Owner
Statistic
- 0
- 0
- 0
- 0
- 1
- over 1 year ago
- August 16, 2022
License
MIT License
Links
Synced at
Wed, 25 Dec 2024 19:31:32 GMT
Languages