Skip to content

Instantly share code, notes, and snippets.

@krizo
Created April 10, 2014 12:53
Show Gist options
  • Save krizo/10378955 to your computer and use it in GitHub Desktop.
Save krizo/10378955 to your computer and use it in GitHub Desktop.
require "spec_helper"
class SalesValues
attr_accessor :total_sales_value, :account_sales_value, :account_total_value_in_timeframe
def initialize
@total_sales_value = @account_sales_value = @account_total_value_in_timeframe = 0
end
end
ALL_TIMEFRAMES = [:today, :yesterday, :this_month, :this_week, :last_month, :last_week]
ENV['ACCOUNTS_NUMBER'] ? ACCOUNTS_NUMBER = ENV['ACCOUNTS_NUMBER'].to_i : ACCOUNTS_NUMBER = 5
shared_examples "sales_by_account" do
let(:actual_total_deals_value) do
@current_page.deals_worth_value.split(DEFAULT_CURRENCY)[1].gsub(',','').to_i
end
let(:expected_account_value) do
total_value(@deals[@contact_name][@timeframe], forecasted=true)
end
ALL_TIMEFRAMES.each do |timeframe|
describe "##{timeframe.upcase}" do
before(:all) do
@start_date = get_dates_for_timeframe(timeframe)[:start_date]
@end_date = get_dates_for_timeframe(timeframe)[:end_date]
@contact_name = @contact.full_name
visit_page(DealsWonPage)
@timeframe = timeframe
end
it "creates deals for #{timeframe.upcase}" do
@deals[@contact_name][timeframe] = \
create_deals_tagged( { :start_date => @start_date,
:end_date => @end_date,
:entity_id => @contact.id,
:stage => 'won',
:tags => [timeframe.to_s.upcase, @contact_name]} )
sleep 60
end
describe "Data verification" do
context "via API" do
it "compare mocks to actual deals" do
account_total_value_in_timeframe = 0
@expected_sales.account_total_value_in_timeframe = 0
@deals[@contact_name][timeframe].each do |mocked_deal|
actual_deal = get_deal_by_id(mocked_deal.id)
mocked_deal.scope.should == actual_deal.scope
account_total_value_in_timeframe += actual_deal.scope
end
exp_total_value = total_value(@deals[@contact_name][timeframe])
exp_total_value.should == account_total_value_in_timeframe
@expected_sales.total_sales_value += account_total_value_in_timeframe
@expected_sales.account_total_value_in_timeframe += account_total_value_in_timeframe
@expected_sales.account_sales_value += account_total_value_in_timeframe
# $total_sales_value += account_total_value_in_timeframe
# $account_total_value_in_timeframe = account_total_value_in_timeframe
# $account_sales_value += account_total_value_in_timeframe
end
end
context "on Deals Page" do
after(:all) do
on(DealsWonPage).tags_filter_heading_element.when_visible(20).click
@current_page.wait_for_ajax
if @current_page.clear_filter_element.visible?
@current_page.clear_filter_element.when_visible.click
end
@current_page.wait_for_ajax
end
before(:all) do
on(NavigationPage).sales_element.when_visible.click
on(DealsPage).wait_for_ajax
on(DealsPage).tags_filter_heading_element.when_visible.click
@current_page.clear_filters_element.click if @current_page.clear_filters_element.visible?
@current_page.wait_for_ajax
end
it "applies filters" do
on(DealsPage).wait_for_ajax
on(FilterPage).filter_by_tag(@timeframe.to_s.upcase).should be_true
@current_page.wait_for_ajax
on(FilterPage).filter_by_tag(@contact_name.split(',')[0]).should be_true
on(DealsPage).wait_for_ajax
end
it "checks total sales value" do
actual_total_deals_value.should == expected_account_value
end
end
context "on Reports (backend)" do
let (:actual_total_value) do
tag_ids = "#{get_tag_id(@contact_name)},#{get_tag_id(@timeframe.to_s.upcase)}"
retries = 0
begin
report_data = FS::API::Reports::Base.user_context(@token).get(
"/api/v2/reports/sales_by_account.json",
:group_by => "days",
:start_date => @start_date,
:end_date => @end_date,
:tag_ids => tag_ids
)
rescue ApiClient::Errors::ServerError
if (retries <= 10)
puts "Server Error while requesting json report, retrying"
retries += 1
retry
else
puts "Giving up after #{retries} retires"
end
end
report_data["data"].first.to_i
end
before(:all) do
@start_date = get_dates_for_timeframe(timeframe)[:start_date]
@end_date = get_dates_for_timeframe(timeframe)[:end_date]
@tag = @contact_name.upcase
end
it "checks total sales value for account" do
actual_total_value.should == expected_account_value
end
end
describe "on Reports UI (data table)" do
let(:actual_account_value) do
@current_page.get_table_cell_text(@contact_name, "Deals Value").gsub(' ','')
end
before(:all) do
on(NavigationPage).reports_element.when_visible.click
on(ReportsPage).wait_for_ajax
end
it "goes to report page" do
@current_page.change_menu_context("sales-performance")
@current_page.sales_performance_menu_icon_element
@current_page.sales_by_account_link_element.when_visible.click
on(ReportsPage).wait_for_ajax
end
it "switches to data view" do
on(ReportsPage).switch_data_view_element.when_visible(20).click
end
context "UNFILTERED" do
before(:all) do
if on(ReportsPage).clear_filter_element.visible?
on(ReportsPage).clear_filter_element.click
end
on(ReportsPage).wait_for_ajax
end
it "checks TOTAL deals value" do
actual_total_value = @current_page.get_table_cell_text("Total", "Deals Value").gsub(' ','')
actual_total_value.should eq format_value(@expected_sales.total_sales_value)
end
it "checks value percentage for account" do
actual_perc_value = @current_page.get_table_cell_text(@contact_name, "% of total deals value")
account_sales_value = actual_account_value.split(DEFAULT_CURRENCY)[1].gsub(',','').to_i
expected_percentage = (account_sales_value.to_f / @expected_sales.total_sales_value.to_f) * 100
actual_perc_value.should eq format_percentage(expected_percentage.round.to_i)
end
it "checks deals value for account" do
actual_account_value.should eq format_value(@expected_sales.account_sales_value)
end
end
context "FILTERED" do
before(:all) do
@current_page.navigate_to(FutureKiss.url_for("app", @report_url))
@current_page.switch_data_view_element.when_visible(20).click
@current_page.wait_for_ajax
@current_page.change_date_to_predefined_time_period(timeframe.to_s)
@current_page.wait_for_ajax
@current_page.filters_element.when_visible.click
@current_page.wait_until { @current_page.tag_filter_element.visible? }
@current_page.tag_filter_element.when_visible.click
end
it "applies filters" do
on(ReportsPage).wait_for_ajax
on(FilterPage).filter_by_tag(@timeframe.to_s.upcase).should be_true
@current_page.wait_for_ajax
on(FilterPage).filter_by_tag(@contact_name.split(',')[0]).should be_true
end
it "checks deals value for account" do
actual_account_value.should eq format_value(expected_account_value)
end
it "checks value percentage for account" do
actual_perc_value = @current_page.get_table_cell_text(
@contact_name, "% of total deals value")
expected_perc = expected_account_value.to_f / @expected_sales.account_total_value_in_timeframe.to_f
actual_perc_value.should eq \
format_percentage((expected_perc * 100).round.to_i)
end
it "clears filters" do
@current_page.wait_for_ajax
@current_page.tag_filter_element.when_visible.click
@current_page.wait_for_ajax
@current_page.clear_filter_element.when_visible.click
@current_page.switch_data_view_element.when_visible.click
end
end
end
end
end
end
end
describe "SALES BY ACCOUNT" do
include_examples 'initialize_data_quality_test'
include DataQualityHelper
before(:all) do
deals_destroy
contacts_destroy
tags_destroy('deals')
tags_destroy('contacts')
@deals = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc) }
# $total_sales_value = 0
@contacts = []
on(ReportsPage).back
@report_url = 'reports/sales_by_account'
@expected_sales = SalesValues.new
end
ACCOUNTS_NUMBER.times do |iter|
describe "Account ##{iter+1}" do
before(:all) do
$account_sales_value = 0
@contact = create(:contact_person)
@contacts << @contact
end
include_examples "sales_by_account"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment