|
1 | 1 | require 'test_helper'
|
2 | 2 |
|
3 | 3 | class RepoTest < ActiveSupport::TestCase
|
4 |
| - # test "the truth" do |
5 |
| - # assert true |
6 |
| - # end |
| 4 | + test '#generate_sparkline_data picks first commit from every week' do |
| 5 | + repo = create(:repo) |
| 6 | + |
| 7 | + mem_res_type = create(:benchmark_result_type, name: 'Memory', unit: 'rss') |
| 8 | + ips_res_type = create(:benchmark_result_type, name: 'Ips', unit: 'i/s') |
| 9 | + |
| 10 | + type1 = create(:benchmark_type, repo: repo, category: 'Array map') |
| 11 | + type2 = create(:benchmark_type, repo: repo, category: 'String to_i') |
| 12 | + |
| 13 | + # Week of Mon 18-05-2020 to Sun 24-05-2020 |
| 14 | + c1 = create(:commit, repo: repo, created_at: Time.utc(2020, 5, 24, 1)) |
| 15 | + c2 = create(:commit, repo: repo, created_at: Time.utc(2020, 5, 24, 1, 1)) |
| 16 | + c3 = create(:commit, repo: repo, created_at: Time.utc(2020, 5, 24, 1, 1, 1)) |
| 17 | + |
| 18 | + # Week of Mon 25-05-2020 to Sun 31-05-2020 |
| 19 | + c4 = create(:commit, repo: repo, created_at: Time.utc(2020, 5, 26)) |
| 20 | + c5 = create(:commit, repo: repo, created_at: Time.utc(2020, 5, 29)) |
| 21 | + c6 = create(:commit, repo: repo, created_at: Time.utc(2020, 5, 30)) |
| 22 | + |
| 23 | + # Week of Mon 01-06-2020 to Sun 07-06-2020 |
| 24 | + c7 = create(:commit, repo: repo, created_at: Time.utc(2020, 6, 1)) |
| 25 | + c8 = create(:commit, repo: repo, created_at: Time.utc(2020, 6, 2)) |
| 26 | + c9 = create(:commit, repo: repo, created_at: Time.utc(2020, 6, 7)) |
| 27 | + |
| 28 | + commits = [c1, c2, c3, c4, c5, c6, c7, c8, c9] |
| 29 | + commits.each_with_index do |commit, index| |
| 30 | + [type1, type2].each do |type| |
| 31 | + create( |
| 32 | + :benchmark_run, |
| 33 | + initiator_id: commit.id, |
| 34 | + initiator_type: 'Commit', |
| 35 | + result: { rss_kb: commit.id }, |
| 36 | + benchmark_result_type_id: mem_res_type.id, |
| 37 | + benchmark_type_id: type.id |
| 38 | + ) |
| 39 | + create( |
| 40 | + :benchmark_run, |
| 41 | + initiator_id: commit.id, |
| 42 | + initiator_type: 'Commit', |
| 43 | + result: { bench_1: commit.id, bench_2: commit.id + 1 }, |
| 44 | + benchmark_result_type_id: ips_res_type.id, |
| 45 | + benchmark_type_id: type.id |
| 46 | + ) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + # assert_equal([0.0, 3.0, 7.0], [index_of_c1, index_of_c4, index_of_c7]) |
| 51 | + # We should pick the first commit from each week. |
| 52 | + # Since the commits are spread over a period of 3 |
| 53 | + # weeks, we should have 3 commits. These commits |
| 54 | + # should be c1, c4 and c7. |
| 55 | + # We will then pick the benchmark_runs records whose |
| 56 | + # initiator_ids are the commits we picked up earlier. |
| 57 | + data = repo.generate_sparkline_data |
| 58 | + assert_equal( |
| 59 | + data, |
| 60 | + 'Array map' => [ |
| 61 | + { |
| 62 | + benchmark_result_type: 'Ips', |
| 63 | + columns: [ |
| 64 | + { |
| 65 | + name: 'bench_1', |
| 66 | + data: [c1.id, c4.id, c7.id] |
| 67 | + }, |
| 68 | + { |
| 69 | + name: 'bench_2', |
| 70 | + data: [c1.id + 1, c4.id + 1, c7.id + 1] |
| 71 | + } |
| 72 | + ], |
| 73 | + }, |
| 74 | + { |
| 75 | + benchmark_result_type: 'Memory', |
| 76 | + columns: [ |
| 77 | + { |
| 78 | + name: 'rss_kb', |
| 79 | + data: [c1.id, c4.id, c7.id] |
| 80 | + } |
| 81 | + ] |
| 82 | + } |
| 83 | + ], |
| 84 | + 'String to_i' => [ |
| 85 | + { |
| 86 | + benchmark_result_type: 'Ips', |
| 87 | + columns: [ |
| 88 | + { |
| 89 | + name: 'bench_1', |
| 90 | + data: [c1.id, c4.id, c7.id] |
| 91 | + }, |
| 92 | + { |
| 93 | + name: 'bench_2', |
| 94 | + data: [c1.id + 1, c4.id + 1, c7.id + 1] |
| 95 | + } |
| 96 | + ], |
| 97 | + }, |
| 98 | + { |
| 99 | + benchmark_result_type: 'Memory', |
| 100 | + columns: [ |
| 101 | + { |
| 102 | + name: 'rss_kb', |
| 103 | + data: [c1.id, c4.id, c7.id] |
| 104 | + } |
| 105 | + ] |
| 106 | + } |
| 107 | + ] |
| 108 | + ) |
| 109 | + end |
7 | 110 | end
|
0 commit comments