]> git.openstreetmap.org Git - rails.git/blob - test/controllers/users/heatmaps_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / users / heatmaps_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Users
6   class HeatmapsControllerTest < ActionDispatch::IntegrationTest
7     ##
8     # test all routes which lead to this controller
9     def test_routes
10       assert_routing(
11         { :path => "/user/username/heatmap", :method => :get },
12         { :controller => "users/heatmaps", :action => "show", :user_display_name => "username" }
13       )
14     end
15
16     def test_show_data
17       user = create(:user)
18       # Create two changesets
19       create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 10)
20       create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
21
22       get user_heatmap_path(user)
23
24       assert_response :success
25       # The data should not be empty
26       heatmap_data = assigns(:heatmap_data)
27       assert_not_nil heatmap_data
28       assert_predicate heatmap_data[:data], :any?
29       # The data should be in the right format
30       heatmap_data[:data].each_value do |entry|
31         assert_equal [:date, :max_id, :total_changes], entry.keys.sort, "Heatmap data entries should have expected keys"
32       end
33       assert_equal 30, heatmap_data[:count]
34     end
35
36     def test_show_data_caching
37       # Enable caching to be able to test
38       Rails.cache.clear
39       @original_cache_store = Rails.cache
40       Rails.cache = ActiveSupport::Cache::MemoryStore.new
41
42       user = create(:user)
43
44       # Create an initial changeset
45       create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 15)
46
47       # First request to populate the cache
48       get user_heatmap_path(user)
49       first_response_data = assigns(:heatmap_data)
50       assert_not_nil first_response_data, "Expected heatmap data to be assigned on the first request"
51       assert_equal 1, first_response_data[:data].values.count { |day| day[:total_changes].positive? }, "Expected one entry in the heatmap data"
52
53       # Inspect cache after the first request
54       cached_data = Rails.cache.read("heatmap_data_of_user_#{user.id}")
55       assert_equal first_response_data, cached_data, "Expected the cache to contain the first response data"
56
57       # Add a new changeset to the database
58       create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
59
60       # Second request
61       get user_heatmap_path(user)
62       second_response_data = assigns(:heatmap_data)
63
64       # Confirm that the cache is still being used
65       assert_equal first_response_data, second_response_data, "Expected cached data to be returned on the second request"
66
67       # Clear the cache and make a third request to confirm new data is retrieved
68       Rails.cache.clear
69       get user_heatmap_path(user)
70       third_response_data = assigns(:heatmap_data)
71
72       # Ensure the new entry is now included
73       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"
74
75       # Reset caching config to defaults
76       Rails.cache.clear
77       Rails.cache = @original_cache_store
78     end
79
80     def test_show_data_no_changesets
81       user = create(:user)
82
83       get user_heatmap_path(user)
84
85       assert_response :success
86       assert_empty(assigns(:heatmap_data)[:data].values)
87       assert_select ".heatmap", :count => 0
88     end
89
90     def test_show_rendering_of_user_with_no_changesets
91       user_without_changesets = create(:user)
92
93       get user_heatmap_path(user_without_changesets)
94
95       assert_response :success
96       assert_select ".heatmap", 0
97     end
98
99     def test_show_rendering_of_user_with_changesets
100       user = create(:user)
101       changeset39 = create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 39)
102       _changeset5 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 5)
103       changeset11 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 11)
104
105       get user_heatmap_path(user)
106
107       assert_response :success
108       assert_select ".heatmap a", 2
109
110       history_path = user_history_path(user)
111       assert_select ".heatmap a[data-date='#{4.months.ago.to_date}'][data-count='39'][href='#{history_path}?before=#{changeset39.id + 1}']"
112       assert_select ".heatmap a[data-date='#{3.months.ago.to_date}'][data-count='16'][href='#{history_path}?before=#{changeset11.id + 1}']"
113       assert_select ".heatmap [data-date='#{5.months.ago.to_date}']:not([data-count])"
114     end
115
116     def test_headline_changeset_zero
117       user = create(:user)
118
119       get user_heatmap_path(user)
120
121       assert_response :success
122       assert_select "h2.text-body-secondary.fs-5", :count => 0
123     end
124
125     def test_headline_changeset_singular
126       user = create(:user)
127       create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 1)
128
129       get user_heatmap_path(user)
130
131       assert_response :success
132       assert_select "h2.text-body-secondary.fs-5", :text => "1 contribution in the last year"
133     end
134
135     def test_headline_changeset_plural
136       user = create(:user)
137       create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 12)
138
139       get user_heatmap_path(user)
140
141       assert_response :success
142       assert_select "h2.text-body-secondary.fs-5", :text => "12 contributions in the last year"
143     end
144   end
145 end