test/cohortly_test.rb in cohortly-0.0.7 vs test/cohortly_test.rb in cohortly-0.0.8
- old
+ new
@@ -64,26 +64,27 @@
assert_equal metric.user_start_date.utc.to_s, payload[:user_start_date].utc.to_s
end
test "one day of data" do
- payload = { :user_start_date => Time.now,
+ payload = { :user_start_date => Time.now.utc,
:user_id => 5,
:user_email => "jordon@example.com",
:controller => "session",
- :action => "login"
+ :action => "login",
+ :created_at => Time.now.utc
}
ActiveSupport::Notifications.instrument("cohortly.event", payload)
Cohortly::Metric.weekly_cohort_chart_for_tag()
report = Cohortly::Report.new()
assert_equal report.report_totals, [[1]]
end
test "weekly" do
Cohortly::Metric.delete_all
- setup_weekly_data_to_report_on
+ weekly_data
Cohortly::Metric.weekly_cohort_chart_for_tag
report = Cohortly::Report.new()
assert report.weekly
@@ -93,13 +94,13 @@
assert_equal report.time_to_key(Time.utc(2011,8)), '2011-31'
assert_equal report.time_to_key(Time.utc(2011,1)), '2011-00'
assert_equal report.start_key, report.time_to_key(Time.now.utc - 15.weeks)
- assert_equal report.period_cohorts.length, 16
+ assert_equal report.period_cohorts.length, 15
- assert_equal report.report_totals, [[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
+ assert_equal report.report_totals, [
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
@@ -113,23 +114,29 @@
[3, 2, 1],
[2, 1],
[1]]
end
- test "report map reduce" do
- setup_data_to_report_on
- Cohortly::Metric.cohort_chart_for_tag
- assert_equal (Cohortly::Metric.all.collect &:user_id).uniq.length, 136
+ test "absolutely correct wwekly data chart" do
+ Cohortly::Metric.delete_all
+ weekly_data
+ Cohortly::Metric.weekly_cohort_chart_for_tag
- report = Cohortly::Report.new(nil,nil,false)
- assert_equal report.key_to_time('2011-08'), Time.utc(2011, 8)
- assert_equal report.time_to_key(Time.utc(2011,8)), '2011-08'
- assert_equal report.start_key, (Time.now - 15.months).year.to_s + '-0' + (Time.now - 15.months).month.to_s
- assert_equal report.period_cohorts.length, 16
+ report = Cohortly::Report.new()
+ assert report.weekly
+
+ time = DateTime.strptime('2011-08', '%Y-%W').utc
+ assert_equal report.key_to_time('2011-08'), time
+ assert_equal report.key_to_time(report.time_to_key(time)), time
- assert_equal report.report_totals, [[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
- [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
+
+ assert_equal report.time_to_key(Time.utc(2011,8)), '2011-31'
+ assert_equal report.time_to_key(Time.utc(2011,1)), '2011-00'
+ assert_equal report.start_key, report.time_to_key(Time.now.utc - 15.weeks)
+ assert_equal report.period_cohorts.length, 15
+
+ assert_equal report.report_totals, [[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
@@ -140,61 +147,123 @@
[5, 4, 3, 2, 1],
[4, 3, 2, 1],
[3, 2, 1],
[2, 1],
[1]]
+ end
+
+ test "report map reduce" do
+ setup_data_to_report_on
+ Cohortly::Metric.cohort_chart_for_tag
+ assert_equal (Cohortly::Metric.all.collect &:user_id).uniq.length, 136
+
+ report = Cohortly::Report.new(nil,nil,false)
+ assert_equal report.key_to_time('2011-08'), Time.utc(2011, 8)
+ assert_equal report.time_to_key(Time.utc(2011,8)), '2011-08'
+ assert_equal report.start_key, (Time.now - 15.months).year.to_s + '-0' + (Time.now - 15.months).month.to_s
+ assert_equal report.period_cohorts.length, 15
+
+ assert_equal report.report_totals, [[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2],
+ [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2],
+ [14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2],
+ [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2],
+ [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2],
+ [11, 10, 9, 8, 7, 6, 5, 4, 3, 2],
+ [10, 9, 8, 7, 6, 5, 4, 3, 2],
+ [9, 8, 7, 6, 5, 4, 3, 2],
+ [8, 7, 6, 5, 4, 3, 2],
+ [7, 6, 5, 4, 3, 2],
+ [6, 5, 4, 3, 2],
+ [5, 4, 3, 2],
+ [4, 3, 2],
+ [3, 2],
+ [2]]
+
end
test "counting uniq users in cohort" do
setup_weekly_data_to_report_on
Cohortly::Metric.weekly_cohort_chart_for_tag()
report = Cohortly::Report.new()
- start_week = report.start_key
- start_week_time = report.key_to_time(report.start_key)
- next_week = report.time_to_key(start_week_time + 1.week)
+
+ start_week_time = report.key_to_time(report.start_key)
+ next_week = report.time_to_key(start_week_time + 1.week)
- assert_equal report.user_count_in_cohort(start_week), 16
+ assert_equal report.user_count_in_cohort(report.start_key), 16
assert_equal report.user_count_in_cohort(next_week), 15
end
test "getting a line of percentages" do
setup_weekly_data_to_report_on
Cohortly::Metric.weekly_cohort_chart_for_tag
report = Cohortly::Report.new
- line = report.percent_line(report.start_key)
- assert_equal line, [16, 100, 94, 88, 81, 75, 69, 63, 56, 50, 44, 38, 31, 25, 19, 13, 6]
+ line = report.percent_line(report.time_to_key(report.key_to_time(report.start_key) + 1.week ))
+ assert_equal line, [15, 100, 93, 87, 80, 73, 67, 60, 53, 47, 40, 33, 27, 20, 13]
end
test "javascript day of year" do
StoredProcedures.store_procedures
assert_equal Time.now.utc.strftime('%j').to_i, StoredProcedures.execute(:day_of_year, Time.now.utc)
assert_equal (Time.now.utc + 1.day).strftime('%j').to_i, StoredProcedures.execute(:day_of_year, Time.now.utc + 1.day)
end
test "javascript week of year" do
+
StoredProcedures.store_procedures
assert_equal Time.now.utc.strftime('%W').to_i, StoredProcedures.execute(:week_of_year, Time.now.utc)
assert_equal (Time.now.utc + 1.week).strftime('%W').to_i, StoredProcedures.execute(:week_of_year, Time.now.utc + 1.week)
+ week_end_minus_15 = Time.now.end_of_week - 15.hours
+ # these are one second off of eachother bleh :P
30.times { |x|
- assert_equal (Time.now.utc + x.days).strftime('%W').to_i, StoredProcedures.execute(:week_of_year, Time.now.utc + x.days)
+ assert_equal (week_end_minus_15 + x.hours + 1.second).utc.strftime('%W').to_i, StoredProcedures.execute(:week_of_year, week_end_minus_15 + x.hours)
}
end
test "javascript time to week key" do
StoredProcedures.store_procedures
assert_equal Time.now.utc.strftime('%Y-%W'), StoredProcedures.execute(:week_key, Time.now.utc)
+ week_end_minus_15 = Time.now.end_of_week - 15.hours
30.times { |x|
- assert_equal (Time.now.utc + x.days).strftime('%Y-%W'), StoredProcedures.execute(:week_key, Time.now.utc + x.days)
+ assert_equal (week_end_minus_15 + x.days + 1.second).strftime('%Y-%W'), StoredProcedures.execute(:week_key, week_end_minus_15 + x.days)
}
end
test "name to args" do
assert_equal Metric.report_name_to_args("cohortly_report-weekly"), [nil, nil, true]
assert_equal Metric.report_name_to_args("cohortly_report-groups=rand_0-weekly"), [nil, ['rand_0'], true]
assert_equal Metric.report_name_to_args("cohortly_report-tags=rand_5-groups=rand_0:rand_1-weekly"), [['rand_5'], ['rand_0', 'rand_1'], true]
assert_equal Metric.report_name_to_args("cohortly_report-tags=rand_1:rand_5-groups=rand_0:rand_1-weekly"), [['rand_1', 'rand_5'], ['rand_0', 'rand_1'], true]
assert_equal Metric.report_name_to_args("cohortly_report-tags=rand_1:rand_5-monthly"), [['rand_1', 'rand_5'], nil, false]
+ end
+
+ def weekly_data_generate
+ data = (1..15).to_a.reverse.collect {|x| (1..x).to_a.reverse}
+ user_id_level = 0
+ data.collect do |ds|
+ start_date = ds.first.weeks.ago;
+ user_base = user_id_level * 1000
+ ds.each do |d|
+ occured_on = d.weeks.ago
+ d.times { |user_id| yield user_base + user_id + 1, start_date, occured_on }
+ end
+ user_id_level += 1
+ end
+ end
+
+ def weekly_data
+ payload = {
+ :controller => "session",
+ :action => "login",
+ :add_tags => ['login']
+ }
+ weekly_data_generate do |user_id, started_on, occurred_on|
+ payload[:user_id] = user_id
+ payload[:username] = "user-#{user_id}"
+ payload[:user_start_date] = started_on
+ payload[:created_at] = occurred_on
+ Cohortly::Metric.store! [nil, nil, nil, nil, payload]
+ end
end
def setup_data_to_report_on
payload = { :user_start_date => Time.now,
:user_id => 5,