Skip to content

Commit

Permalink
testing the progress report generation
Browse files Browse the repository at this point in the history
  • Loading branch information
evsasse committed Dec 23, 2024
1 parent 3eb82d4 commit 2e048a6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 56 deletions.
14 changes: 9 additions & 5 deletions app/services/projects/simplecov_progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ def initialize(project)
def call
# TODO: could be improved to only get the last report of each date.
# TODO: could be improved to remove blank coverage reports at the SQL level.
project.reports.simplecov.where(branch: "main").order(:created_at).map do |report|
next if report.general_coverage.blank?

[ report.created_at, report.general_coverage ]
end.compact
project
.reports
.simplecov
.where(branch: "main")
.where("JSON_EXTRACT(results, '$.general_coverage') IS NOT NULL")
.group("DATE(created_at)")
.order(:created_at)
.map { |report| [ report.created_at, report.general_coverage ] }
.compact
end
end
end
44 changes: 44 additions & 0 deletions test/services/projects/simplecov_progress_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "test_helper"

class ::Projects::SimplecovProgressTest < ActiveSupport::TestCase
def setup
organization = Organization.create!(handle: "test-organization")
@project = Project.create!(handle: "test-project", organization:)

def create_report(created_at, general_coverage)
Report.create!(
organization: @project.organization,
project: @project,
name: "name",
kind: :simplecov,
created_at:,
results: { general_coverage: }
)
end

create_report("2024-11-27 01:01:01", 87.7)

create_report("2024-11-28 01:01:01", nil)

create_report("2024-11-29 01:01:01", 88.1).update!(results: {})

create_report("2024-11-30 01:01:01", 89.1)
create_report("2024-11-30 02:02:02", 89.2)

create_report("2024-12-01 01:01:01", 90.1)
create_report("2024-12-01 02:02:02", 92)
create_report("2024-12-01 03:03:03", 90.3)
end

def test_one_datapoint_per_date
results = ::Projects::SimplecovProgress.new(@project).call

assert_equal(results.map do |created_at, coverage|
[ created_at.to_date.to_s, coverage ]
end, [
[ "2024-11-27", 87.7 ],
[ "2024-11-30", 89.1 ],
[ "2024-12-01", 90.1 ]
])
end
end
100 changes: 49 additions & 51 deletions test/services/users/omniauth_upserter_test.rb
Original file line number Diff line number Diff line change
@@ -1,69 +1,67 @@
require "test_helper"

module Users
class OmniauthUpserterTest < ActiveSupport::TestCase
Auth = Struct.new(:provider, :uid, :info)
AuthInfo = Struct.new(:nickname, :email)
class Users::OmniauthUpserterTest < ActiveSupport::TestCase
Auth = Struct.new(:provider, :uid, :info)
AuthInfo = Struct.new(:nickname, :email)

def setup
@auth = Auth.new(
provider: "github",
uid: "12345",
info: AuthInfo.new(
nickname: "testuser",
email: "[email protected]"
)
def setup
@auth = Auth.new(
provider: "github",
uid: "12345",
info: AuthInfo.new(
nickname: "testuser",
email: "[email protected]"
)
)

@organization = Organization.create!(
handle: "test-organization",
user_queue: [ "testuser" ]
)
end

def test_creates_new_user_if_not_existing
assert_difference "User.count", 1 do
OmniauthUpserter.new(@auth).call
end
@organization = Organization.create!(
handle: "test-organization",
user_queue: [ "testuser" ]
)
end

user = User.find_by(uid: @auth.uid)
assert_equal "github", user.provider
assert_equal "12345", user.uid
assert_equal "testuser", user.handle
assert_equal "[email protected]", user.email
def test_creates_new_user_if_not_existing
assert_difference "User.count", 1 do
::Users::OmniauthUpserter.new(@auth).call
end

def test_finds_existing_user_if_already_exists
existing_user = User.create!(
provider: "github",
uid: "12345",
handle: "testuser",
email: "[email protected]",
password: "securepassword"
)
user = User.find_by(uid: @auth.uid)
assert_equal "github", user.provider
assert_equal "12345", user.uid
assert_equal "testuser", user.handle
assert_equal "[email protected]", user.email
end

assert_no_difference "User.count" do
OmniauthUpserter.new(@auth).call
end
def test_finds_existing_user_if_already_exists
existing_user = User.create!(
provider: "github",
uid: "12345",
handle: "testuser",
email: "[email protected]",
password: "securepassword"
)

user = User.find_by(uid: @auth.uid)
assert_equal existing_user.id, user.id
assert_no_difference "User.count" do
::Users::OmniauthUpserter.new(@auth).call
end

def test_enrolls_user_from_organization_queues
OmniauthUpserter.new(@auth).call
user = User.find_by(uid: @auth.uid)
user = User.find_by(uid: @auth.uid)
assert_equal existing_user.id, user.id
end

assert @organization.reload.user_queue.exclude?(user.handle)
assert OrganizationUser.exists?(organization: @organization, user: user)
end
def test_enrolls_user_from_organization_queues
::Users::OmniauthUpserter.new(@auth).call
user = User.find_by(uid: @auth.uid)

assert @organization.reload.user_queue.exclude?(user.handle)
assert OrganizationUser.exists?(organization: @organization, user: user)
end

def test_does_not_fail_when_handle_not_in_queue
@organization.update!(user_queue: [])
def test_does_not_fail_when_handle_not_in_queue
@organization.update!(user_queue: [])

assert_nothing_raised do
OmniauthUpserter.new(@auth).call
end
assert_nothing_raised do
::Users::OmniauthUpserter.new(@auth).call
end
end
end

0 comments on commit 2e048a6

Please sign in to comment.