Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9852510
commit 1c9631a
Showing
10 changed files
with
245 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'json' | ||
|
||
module Alke | ||
class JsonParser < Faraday::Middleware | ||
CONTENT_TYPE = 'Content-Type'.freeze | ||
MIME_TYPE = 'application/json'.freeze | ||
|
||
def call(env) | ||
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE | ||
env[:body] = (env[:body] || {}).to_json | ||
@app.call(env).on_complete do | ||
env[:body] = JSON.parse(env[:body]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
require "test/unit" | ||
require "alke" | ||
require 'test_helper' | ||
|
||
class TestAlke < Test::Unit::TestCase | ||
def test_sanity | ||
flunk "write tests or I will kneecap you" | ||
end | ||
class TestAlke < MiniTest::Unit::TestCase | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'test_helper' | ||
|
||
class TestClient < MiniTest::Unit::TestCase | ||
|
||
class Widget | ||
include Alke::Client | ||
|
||
host 'http://localhost:4567' | ||
path '/widgets' | ||
end | ||
|
||
def setup | ||
@client = Object.new | ||
@class = @client.singleton_class | ||
@class.send :include, Alke::Client | ||
end | ||
|
||
def test_default_adapter | ||
assert_equal Faraday::Adapter::NetHttp, @class.adapter | ||
end | ||
|
||
def test_default_host | ||
assert_equal nil, @class.host | ||
end | ||
|
||
def test_default_prefix | ||
assert_equal nil, @class.prefix | ||
end | ||
|
||
def test_default_path | ||
assert_equal nil, @class.path | ||
end | ||
|
||
def test_default_url | ||
assert_equal '', @class.url | ||
assert_equal '/1', @class.url(1) | ||
end | ||
|
||
def test_set_host | ||
@class.host 'http://localhost:4567' | ||
assert_equal 'http://localhost:4567', @class.host | ||
end | ||
|
||
def test_connection | ||
assert @class.connection | ||
assert @class.connection != @class.connection, | ||
"should return a dup'd copy of the connection" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "alke" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'test_helper' | ||
|
||
class TestIntegration < MiniTest::Unit::TestCase | ||
|
||
class Widget | ||
include Alke::Client | ||
|
||
host 'http://localhost:4567' | ||
path '/widgets' | ||
|
||
schema do | ||
id :primary_key | ||
name :string | ||
price :integer | ||
stock :boolean | ||
updated_at :datetime, readonly: true | ||
end | ||
end | ||
|
||
def setup | ||
$live_warning ||= false | ||
unless ENV['LIVE'] == '1' | ||
unless $live_warning | ||
msg = "\nStart the server `ruby test/sinatra_app.rb` and set " | ||
msg += "the LIVE=1 env variable." | ||
puts msg | ||
$live_warning = true | ||
end | ||
skip | ||
end | ||
end | ||
|
||
def test_find | ||
assert w = Widget[1] | ||
assert_equal Widget, w.class | ||
assert_equal 1, w.id | ||
end | ||
|
||
def test_responds_to | ||
w = Widget.new | ||
assert w.respond_to? :id | ||
assert w.respond_to? :name | ||
assert w.respond_to? :price | ||
assert w.respond_to? :stock | ||
assert w.respond_to? :updated_at | ||
assert !w.respond_to?(:id=) | ||
assert w.respond_to? :name= | ||
assert w.respond_to? :price= | ||
assert w.respond_to? :stock= | ||
assert !w.respond_to?(:updated_at=) | ||
end | ||
|
||
def test_responds_to_schemaless | ||
w = Widget.new quantity: 6 | ||
assert w.respond_to? :quantity | ||
assert !w.respond_to?(:quantity=) | ||
w2 = Widget.new | ||
assert !w2.respond_to?(:quantity) | ||
end | ||
|
||
def test_create | ||
w = Widget.create( | ||
name: 'WonderRug', | ||
price: 2000, | ||
stock: true | ||
) | ||
assert w.id | ||
assert w.persisted? | ||
end | ||
end |