Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

Commit

Permalink
fix_user_impression_stats_test
Browse files Browse the repository at this point in the history
  • Loading branch information
StareIntoTheBeard committed Oct 11, 2018
1 parent d3c510f commit d853086
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions test/code_fund/stats/user_impressions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,65 @@ defmodule CodeFund.Stats.UserImpressionsTest do
alias CodeFund.Impressions
import CodeFund.Factory

setup do
System.put_env("USER_IMPRESSION_STATS_REFRESH_INTERVAL_IN_MINUTES", "0")
:ok
end

test "last_thirty_days initial state" do
stats = UserImpressions.last_thirty_days()
assert stats.click_count == 0
assert stats.click_rate == 0.0
assert stats.distribution_amount == 0.0
assert stats.impression_count == 0
Ecto.Adapters.SQL.query!(
CodeFund.Repo,
"REFRESH MATERIALIZED VIEW CONCURRENTLY user_impressions;",
[],
timeout: :infinity
)

assert UserImpressions.last_thirty_days() == %CodeFund.Stats.UserImpressions.State{
click_count: 0,
click_rate: 0.0,
distribution_amount: 0.0,
impression_count: 0,
refreshed_at: nil
}
end

test "stats update after :refresh message sent to genserver" do
redirected_at = Timex.now()
audience = insert(:audience)
campaign = insert(:campaign, audience: audience, user: insert(:user, company: "Company Name"))

insert(:impression,
campaign: campaign,
redirected_at: redirected_at,
country: "US",
distribution_amount: Decimal.new("0.1"),
revenue_amount: Decimal.new("0.2")
)

insert(:impression, campaign: campaign, country: "US")

Ecto.Adapters.SQL.query!(
CodeFund.Repo,
"REFRESH MATERIALIZED VIEW CONCURRENTLY user_impressions;",
[],
timeout: :infinity
)

pid = Process.whereis(UserImpressions)
Process.send(pid, :refresh, [])
stats = UserImpressions.last_thirty_days()
assert stats.refreshed_at != nil

assert Enum.count(Impressions.list_impressions()) == 2
assert CodeFund.Schema.UserImpression |> CodeFund.Repo.all() |> length == 2

assert %CodeFund.Stats.UserImpressions.State{
click_count: 1,
click_rate: 0.5,
distribution_amount: distribution_amount,
impression_count: 2,
refreshed_at: date
} = UserImpressions.last_thirty_days()

assert date
assert distribution_amount == Decimal.new("0.100000000000")
end
end

0 comments on commit d853086

Please sign in to comment.