Skip to content

Instantly share code, notes, and snippets.

@data-doge
Created February 26, 2015 03:22
Show Gist options
  • Save data-doge/3e56e1414c40f8b2ef1f to your computer and use it in GitHub Desktop.
Save data-doge/3e56e1414c40f8b2ef1f to your computer and use it in GitHub Desktop.
controller_test_ex
require 'spec_helper'
describe "account_controller" do
describe "GET /accounts" do
let(:first_account) { Account.create(dollars: 100, josh_coins: 100, user_id: "1234", email: "bob@bob.bo1b") }
let(:second_account) { Account.create(dollars: 200, josh_coins: 200, user_id: "3456", email: "bob@bob.b1ob") }
let(:third_account) { Account.create(dollars: 300, josh_coins: 300, user_id: "3156", email: "bob@bob1.bob") }
describe "returns correct user information" do
before(:each) do
first_account
get "/accounts"
@json_response = parse_json(last_response.body)
end
it "returns the josh coins of a user" do
josh_coins = first_account.josh_coins
expect(@json_response.first["josh_coins"]).to eq(josh_coins)
end
it "returns the dollars of a user" do
dollars = first_account.dollars
expect(@json_response.first["dollars"]).to eq(dollars)
end
it "returns the id of a user" do
id = first_account.id
expect(@json_response.first["id"]).to eq(id)
end
end
describe "returns user information in the order specified" do
before(:each) do
first_account
second_account
third_account
get "/accounts"
@json_response = parse_json(last_response.body)
end
it "returns richest user according to josh_coins first" do
third_account_json = third_account.as_json
expect(@json_response.first).to eq(third_account_json)
end
it "returns poorest user according to josh_coins last" do
first_account_json = first_account.as_json
expect(@json_response.last).to eq(first_account_json)
end
end
end
describe "POST /accounts/create/0-1-0" do
let(:valid_params) { { user_id: "3", email: "person@example.com" } }
it "passes the correct params to Account" do
valid_args = valid_params.merge(raw_josh_coins: 10000, raw_dollars: 10000)
expect(Account).to receive(:new).with(valid_args).and_call_original
post "/accounts/create/0-1-0", valid_params
end
it "returns the api key if the account is saved" do
account = double("account", save: true, api_key: 4)
allow(Account).to receive(:new).and_return(account)
post "/accounts/create/0-1-0", valid_params
json_result = parse_json(last_response.body)
expect(json_result["api_key"]).to eq(4)
end
it "return a 400 status if the account can not be saved" do
account = double("account", save: false)
allow(Account).to receive(:new).and_return(account)
post "/accounts/create/0-1-0", valid_params
expect(last_response.status).to eq(400)
end
end
describe "GET /orders?sort_by=method&limit=number" do
let(:account) { Account.create(dollars: 100, josh_coins: 100, user_id: "1234", email: "bob@bob.bo1b") }
let(:bid) { account.bids.create(price: 300, volume: 400) }
let(:ask) { account.asks.create(price: 500, volume: 600) }
describe "returns json hash with bids and asks" do
before(:each) do
account
bid
ask
get "/orders"
@json_response = parse_json(last_response.body)
end
it "returns bids" do
expect(@json_response["bids"]).to be_truthy
end
it "returns asks" do
expect(@json_response["asks"]).to be_truthy
end
end
describe "returns orders with correct format" do
describe "returns bids with correct format" do
before(:each) do
account
bid
get "/orders"
@json_response = parse_json(last_response.body)
end
it "returns correct id" do
expect(@json_response["bids"].first["id"]).to eq (bid.id)
end
it "returns correct price" do
expect(@json_response["bids"].first["price"]).to eq (bid.price)
end
it "returns correct volume" do
expect(@json_response["bids"].first["volume"]).to eq (bid.volume)
end
it "returns correct time_placed" do
expect(@json_response["bids"].first["time_placed"]).to eq (parse_json(bid.time_placed.to_json))
end
end
describe "returns asks with correct format" do
before(:each) do
account
ask
get "/orders"
@json_response = parse_json(last_response.body)
end
it "returns correct id" do
expect(@json_response["asks"].first["id"]).to eq (ask.id)
end
it "returns correct price" do
expect(@json_response["asks"].first["price"]).to eq (ask.price)
end
it "returns correct volume" do
expect(@json_response["asks"].first["volume"]).to eq (ask.volume)
end
it "returns correct time_placed" do
expect(@json_response["asks"].first["time_placed"]).to eq (parse_json(ask.time_placed.to_json))
end
end
end
describe "if sort method specified, returns orders in the correct order" do
describe "if method is time, returns in order of time" do
before(:each) do
@bids = []
@asks = []
3.times do
@asks << account.asks.create(price:300, volume:400)
@bids << account.bids.create(price:300, volume:400)
sleep(1)
end
get '/orders?sort_by=time'
@json_response = parse_json(last_response.body)
end
it "returns newest bid first" do
expect(@json_response["bids"].first["id"]).to eq(@bids.last.id)
end
it "returns oldest bid last" do
expect(@json_response["bids"].last["id"]).to eq(@bids.first.id)
end
it "returns newest ask first" do
expect(@json_response["asks"].first["id"]).to eq(@asks.last.id)
end
it "returns oldest ask last" do
expect(@json_response["asks"].last["id"]).to eq(@asks.first.id)
end
end
describe "if method is price, returns orders from highest to lowest" do
before do
@bids = []
@asks = []
3.times do |i|
@bids << account.bids.create(price: 10 * (i + 1), volume: 5)
@asks << account.asks.create(price: 10 * (i + 1), volume: 5)
end
get '/orders?sort_by=price'
@json_response = parse_json(last_response.body)
end
it "returns highest bid first" do
expect(@json_response["bids"].first["price"]).to eq(@bids.last.price)
end
it "returns lowest bid last" do
expect(@json_response["bids"].last["price"]).to eq(@bids.first.price)
end
it "returns highest ask first" do
expect(@json_response["asks"].first["price"]).to eq(@asks.last.price)
end
it "returns lowest ask last" do
expect(@json_response["asks"].last["price"]).to eq(@asks.first.price)
end
end
end
describe "when limit specified, returns proper amount of orders" do
describe "if limit is between 1 and the number of bids, returns specified limit of orders" do
before do
50.times do
account.bids.create(price: 300, volume: 400)
account.asks.create(price: 300, volume: 400)
end
@limit = rand(1..50)
get "/orders?limit=#{@limit}"
@json_response = parse_json(last_response.body)
end
it "returns the amount of asks specified: random number #{@limit}" do
expect(@json_response["bids"].length).to eq(@limit)
end
it "returns the amount of asks specified" do
expect(@json_response["asks"].length).to eq(@limit)
end
end
describe "when limit is less than 1, returns all orders" do
before do
10.times do
account.bids.create(price: 300, volume: 400)
account.asks.create(price: 300, volume: 400)
end
@limit = 0
get "/orders?limit=#{@limit}"
@json_response = parse_json(last_response.body)
end
it "returns all bids" do
expect(@json_response["bids"].length).to eq(10)
end
it "returns all asks" do
expect(@json_response["asks"].length).to eq(10)
end
end
describe "when limit is greater than number of orders, returns all orders" do
before do
10.times do
account.bids.create(price: 300, volume: 400)
account.asks.create(price: 300, volume: 400)
end
@limit = 1000
get "/orders?limit=#{@limit}"
@json_response = parse_json(last_response.body)
end
it "returns all bids" do
expect(@json_response["bids"].length).to eq(10)
end
it "returns all asks" do
expect(@json_response["asks"].length).to eq(10)
end
end
end
end
describe "GET /account/orders/bids?api_key=[API_KEY]" do
let(:account) { Account.create(dollars: 100, josh_coins: 100, user_id: "1234", email: "bob@bob.bo1b")}
describe "returns the accounts' bids with correct format" do
before do
@account = account
api_key = @account.api_key
@bid = @account.bids.create(price: rand(1..100), volume: rand(1..10))
get "/account/orders/bids?api_key=#{api_key}"
@json_response = parse_json(last_response.body)
end
it "returns correct id" do
expect(@json_response.first["id"]).to eq(@bid.id)
end
it "returns correct price" do
expect(@json_response.first["price"]).to eq(@bid.price)
end
it "returns correct volume" do
expect(@json_response.first["volume"]).to eq(@bid.volume)
end
it "returns correct time_place" do
expect(@json_response.first["time_placed"]).to eq(parse_json(@bid.time_placed.to_json))
end
end
describe "returns all of the accounts' bids" do
before do
@account = account
api_key = @account.api_key
50.times {@account.bids.create(price: 100, volume: 100)}
get "/account/orders/bids?api_key=#{api_key}"
@json_response = parse_json(last_response.body)
end
it "returns all of the accounts' bids" do
expect(@json_response.length).to eq(50)
end
end
end
describe "GET /account/orders/asks?api_key=[API_KEY]" do
let(:account) { Account.create(dollars: 100, josh_coins: 100, user_id: "1234", email: "bob@bob.bo1b")}
describe "returns the accounts' asks with correct format" do
before do
@account = account
api_key = @account.api_key
@ask = @account.asks.create(price: rand(1..100), volume: rand(1..10))
get "/account/orders/asks?api_key=#{api_key}"
@json_response = parse_json(last_response.body)
end
it "returns correct id" do
expect(@json_response.first["id"]).to eq(@ask.id)
end
it "returns correct price" do
expect(@json_response.first["price"]).to eq(@ask.price)
end
it "returns correct volume" do
expect(@json_response.first["volume"]).to eq(@ask.volume)
end
it "returns correct time_place" do
expect(@json_response.first["time_placed"]).to eq(parse_json(@ask.time_placed.to_json))
end
end
describe "returns all of the accounts' asks" do
before do
@account = account
api_key = @account.api_key
50.times do
@account.asks.create(price: 100, volume: 100)
end
get "/account/orders/asks?api_key=#{api_key}"
@json_response = parse_json(last_response.body)
end
it "returns all of the accounts' asks" do
expect(@json_response.length).to eq(50)
end
end
end
describe "GET /account/balance?api_key=[API_KEY]" do
let(:account) { Account.create(dollars: rand(0..100), josh_coins: rand(0..100), user_id: "1234", email: "bob@bob.bo1b")}
describe "returns the account's balance" do
before do
@account = account
api_key = @account.api_key
get "/account/balance?api_key=#{api_key}"
@json_response = parse_json(last_response.body)
end
it "returns the correct number of dollars" do
expect(@json_response["dollars"]).to eq(@account.dollars)
end
it "returns the correct number of JC" do
expect(@json_response["josh_coins"]).to eq(@account.josh_coins)
end
it "returns the accounts id" do
expect(@json_response["id"]).to eq(@account.id)
end
it "returns the accounts user_id" do
expect(@json_response["user_id"]).to eq(@account.user_id)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment