4 class HeatmapsControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/user/username/heatmap", :method => :get },
10 { :controller => "users/heatmaps", :action => "show", :user_display_name => "username" }
16 # Create two changesets
17 create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 10)
18 create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
20 get user_heatmap_path(user)
22 assert_response :success
23 # The data should not be empty
24 heatmap_data = assigns(:heatmap_data)
25 assert_not_nil heatmap_data
26 assert_predicate heatmap_data[:data], :any?
27 # The data should be in the right format
28 heatmap_data[:data].each_value do |entry|
29 assert_equal [:date, :max_id, :total_changes], entry.keys.sort, "Heatmap data entries should have expected keys"
31 assert_equal 30, heatmap_data[:count]
34 def test_show_data_caching
35 # Enable caching to be able to test
37 @original_cache_store = Rails.cache
38 Rails.cache = ActiveSupport::Cache::MemoryStore.new
42 # Create an initial changeset
43 create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 15)
45 # First request to populate the cache
46 get user_heatmap_path(user)
47 first_response_data = assigns(:heatmap_data)
48 assert_not_nil first_response_data, "Expected heatmap data to be assigned on the first request"
49 assert_equal 1, first_response_data[:data].values.count { |day| day[:total_changes].positive? }, "Expected one entry in the heatmap data"
51 # Inspect cache after the first request
52 cached_data = Rails.cache.read("heatmap_data_of_user_#{user.id}")
53 assert_equal first_response_data, cached_data, "Expected the cache to contain the first response data"
55 # Add a new changeset to the database
56 create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
59 get user_heatmap_path(user)
60 second_response_data = assigns(:heatmap_data)
62 # Confirm that the cache is still being used
63 assert_equal first_response_data, second_response_data, "Expected cached data to be returned on the second request"
65 # Clear the cache and make a third request to confirm new data is retrieved
67 get user_heatmap_path(user)
68 third_response_data = assigns(:heatmap_data)
70 # Ensure the new entry is now included
71 assert_equal 2, third_response_data[:data].values.count { |day| day[:total_changes].positive? }, "Expected two entries in the heatmap data after clearing the cache"
73 # Reset caching config to defaults
75 Rails.cache = @original_cache_store
78 def test_show_data_no_changesets
81 get user_heatmap_path(user)
83 assert_response :success
84 assert_empty(assigns(:heatmap_data)[:data].values)
85 assert_select ".heatmap", :count => 0
88 def test_show_rendering_of_user_with_no_changesets
89 user_without_changesets = create(:user)
91 get user_heatmap_path(user_without_changesets)
93 assert_response :success
94 assert_select ".heatmap", 0
97 def test_show_rendering_of_user_with_changesets
99 changeset39 = create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 39)
100 _changeset5 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 5)
101 changeset11 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 11)
103 get user_heatmap_path(user)
105 assert_response :success
106 assert_select ".heatmap a", 2
108 history_path = user_history_path(user)
109 assert_select ".heatmap a[data-date='#{4.months.ago.to_date}'][data-count='39'][href='#{history_path}?before=#{changeset39.id + 1}']"
110 assert_select ".heatmap a[data-date='#{3.months.ago.to_date}'][data-count='16'][href='#{history_path}?before=#{changeset11.id + 1}']"
111 assert_select ".heatmap [data-date='#{5.months.ago.to_date}']:not([data-count])"
114 def test_headline_changeset_zero
117 get user_heatmap_path(user)
119 assert_response :success
120 assert_select "h2.text-body-secondary.fs-5", :count => 0
123 def test_headline_changeset_singular
125 create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 1)
127 get user_heatmap_path(user)
129 assert_response :success
130 assert_select "h2.text-body-secondary.fs-5", :text => "1 contribution in the last year"
133 def test_headline_changeset_plural
135 create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 12)
137 get user_heatmap_path(user)
139 assert_response :success
140 assert_select "h2.text-body-secondary.fs-5", :text => "12 contributions in the last year"