Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 500 error if paginated_stats are empty #422

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions api/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,18 @@
]
}}
]).to_a[0]
total_count = paginated_stats["pagination"][0]["total_count"]
num_pages = [1, (total_count / per_page.to_f).ceil].max
data = paginated_stats["data"].map do |user_stats|
{
:username => user_stats["username"]
}.merge(user_stats["course_stats"].except(*exclude_from_stats))
data = []
num_pages = 0
page = 0
total_count = 0
if not paginated_stats["pagination"].empty?
total_count = paginated_stats["pagination"][0]["total_count"]
num_pages = [1, (total_count / per_page.to_f).ceil].max
data = paginated_stats["data"].map do |user_stats|
{
:username => user_stats["username"]
}.merge(user_stats["course_stats"].except(*exclude_from_stats))
end
end
else
# If a list of usernames is provided, then sort by the order in which those names appear
Expand Down
8 changes: 8 additions & 0 deletions spec/api/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,14 @@ def build_structure_and_response(course_id, authors, build_initial_stats = true,
expect(res["user_stats"]).to eq expected_result
end

it "handle stats for user with no activity" do
invalid_course_id = "course-v1:edX+DNE+Not_EXISTS"
get "/api/v1/users/#{invalid_course_id}/stats"
expect(last_response.status).to eq(200)
res = parse(last_response.body)
expect(res["user_stats"]).to eq []
end

it "returns user's stats filtered by user with default/activity sort" do
usernames = %w[userauthor-1 userauthor-2 userauthor-3].sample(2)
usernames = usernames.shuffle.join(',')
Expand Down