Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Converts the existing test suite over to RSpec.

  • Loading branch information...
commit 6bcf360f3ec8abb6eaa46c280a40fd8992537010 1 parent ffdddb7
@damncabbage damncabbage authored
View
1  .rspec
@@ -0,0 +1 @@
+--colour --format d
View
0  test/fixtures/order.json → spec/fixtures/order.json
File renamed without changes
View
0  test/fixtures/time.json → spec/fixtures/time.json
File renamed without changes
View
86 spec/models/api_spec.rb
@@ -0,0 +1,86 @@
+require 'spec_helper'
+
+describe BigCommerce::Api do
+ before do
+ FakeWeb.allow_net_connect = false
+ FakeWeb.register_uri(:get, %r|https://test:12345@store-12345.mybigcommerce.com/api/v2/orders|, :body => load_json_fixture('order'), :status => 200, :content_type => "text/json")
+ FakeWeb.register_uri(:get, %r|https://test:12345@store-12345.mybigcommerce.com/api/v2/time|, :body => load_json_fixture('time'), :status => 200, :content_type => "text/json")
+ end
+ after do
+ FakeWeb.clean_registry
+ end
+
+ let(:api) do
+ BigCommerce::Api.new({
+ :store_url => "https://store-12345.mybigcommerce.com",
+ :username => "test", :api_key => "12345"
+ })
+ end
+
+ let(:rfc2822_datetime) { "Tue, 13 Mar 2012 12:45:26 +0000" }
+ let(:rfc2822_date) { "Mon, 12 Mar 2012 00:00:00 +0000" }
+
+ describe "Time" do
+ it "converts times to RFC 2822 format" do
+ api.to_rfc2822(DateTime.parse('2012-03-13 12:45:26 +0000')).should eql rfc2822_datetime
+ end
+ it "converts dates to RFC 2822 format" do
+ api.to_rfc2822(DateTime.parse('2012-03-12 00:00:00 +0000')).should eql rfc2822_date
+ end
+ it "retrieves the current time from the store" do
+ api.connection.should_receive(:get).once.with("/time")
+ api.get_time
+ end
+ end
+
+ describe "Orders" do
+ it "retrieves an order by ID" do
+ api.connection.should_receive(:get).once.with("/orders/100")
+ api.get_order(100)
+ end
+
+ it "retrieves orders" do
+ api.connection.should_receive(:get).once.with("/orders", {})
+ api.get_orders
+ end
+
+ it "retrieves orders with pagination" do
+ api.connection.should_receive(:get).once.with("/orders", {:page=>2})
+ api.get_orders(:page => 2)
+ end
+
+ it "retrieves orders with limit" do
+ api.connection.should_receive(:get).once.with("/orders", {:limit => 10})
+ api.get_orders(:limit => 10)
+ end
+
+ it "retrieves orders with pagination and limit" do
+ api.connection.should_receive(:get).once.with("/orders", {:limit=>10, :page=>2})
+ api.get_orders(:limit => 10, :page => 2)
+ end
+
+ it "converts from DateTime when retrieving orders by date" do
+ api.connection.should_receive(:get).once.with("/orders", {:min_date_created => CGI::escape(rfc2822_datetime)})
+ api.get_orders_by_date(DateTime.parse('2012-03-13 12:45:26 GMT'))
+ end
+
+ it "converts from DateTime when retrieving orders by date with pagination" do
+ api.connection.should_receive(:get).once.with("/orders", {:min_date_created => CGI::escape(rfc2822_datetime), :page => 2})
+ api.get_orders_by_date(DateTime.parse('2012-03-13 12:45:26 GMT'), :page => 2)
+ end
+
+ it "converts from Date when retrieving orders by date" do
+ api.connection.should_receive(:get).once.with("/orders", {:min_date_created => CGI::escape(rfc2822_date)})
+ api.get_orders_by_date(Date.parse("2012-03-12"))
+ end
+
+ it "converts from a date-time string when retrieving orders by date" do
+ api.connection.should_receive(:get).once.with("/orders", {:min_date_created=> CGI::escape(rfc2822_datetime)})
+ api.connection.should_receive(:get).once.with("/orders", {:min_date_created=> CGI::escape(rfc2822_date)})
+
+ api.get_orders_by_date('2012-03-13 12:45:26 GMT')
+ api.get_orders_by_date('2012-03-12')
+ end
+ end
+
+end
View
45 spec/models/connection_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+describe BigCommerce::Connection do
+ let(:connection) do
+ BigCommerce::Connection.new({
+ :store_url => "https://store-12345.mybigcommerce.com",
+ :username => "test",
+ :api_key => "12345"
+ })
+ end
+
+ describe "#get" do
+ before do
+ FakeWeb.allow_net_connect = false
+ FakeWeb.register_uri(:get, %r|https://test:12345@store-12345.mybigcommerce.com/api/v2/orders|, :body => load_json_fixture('order'), :status => 200, :content_type => "text/json")
+ FakeWeb.register_uri(:get, %r|https://test:12345@store-12345.mybigcommerce.com/api/v2/time|, :body => load_json_fixture('time'), :status => 200, :content_type => "text/json")
+ end
+ after do
+ FakeWeb.clean_registry
+ end
+
+ it "sends requests with no params" do
+ connection.should_receive(:request).once.with(:get, "/orders", nil, nil)
+ connection.get("/orders")
+ end
+
+ it "sends requests with with params" do
+ connection.should_receive(:request).once.with(:get, "/orders", nil, {:page => 3})
+ connection.get("/orders", {:page => 3})
+ end
+ end
+
+ describe "#hash_to_params" do
+ # TODO: Consider using addressable gem; see <http://stackoverflow.com/a/803067/3528> for examples.
+ it "converts flat hashes to query strings" do
+ connection.hash_to_params({:a => 1, :b => 2}).should eql "a=1&b=2"
+ end
+ it "converts empty hashes to nil" do
+ connection.hash_to_params({}).should be_nil
+ end
+ it "leaves nil as nil" do
+ connection.hash_to_params(nil).should be_nil
+ end
+ end
+end
View
8 spec/spec_helper.rb
@@ -0,0 +1,8 @@
+require 'rubygems'
+require 'bigcommerce'
+require 'fakeweb'
+
+# Requires supporting ruby files with custom matchers and macros, etc,
+# in spec/support/ and its subdirectories.
+Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f }
+
View
9 spec/support/fixture_helpers.rb
@@ -0,0 +1,9 @@
+module FixtureHelpers
+ def load_json_fixture(name)
+ File.read(File.expand_path("../fixtures/#{name}.json", File.dirname(__FILE__)))
+ end
+end
+
+RSpec.configure do |c|
+ c.include FixtureHelpers
+end
Please sign in to comment.
Something went wrong with that request. Please try again.