defmodule GitGud.EventsTest do
use GitGud.DataCase, async: false
import GitGud.ForgeFixtures
alias GitGud.Events
alias GitGud.Issues
test "records and lists events oldest first" do
{user, repo} = repository_fixture()
issue = issue_fixture(repo, user)
:ok = Events.record(issue, "opened", user)
:ok = Events.record(issue, "title_changed", user, %{from: "a", to: "b"})
:ok = Events.record(issue, "closed", user)
kinds = issue |> Events.list_for() |> Enum.map(& &1.kind)
assert kinds == ["opened", "title_changed", "closed"]
end
test "payload keys survive the JSONB round trip as strings" do
{user, repo} = repository_fixture()
issue = issue_fixture(repo, user)
:ok = Events.record(issue, "title_changed", user, %{from: "old", to: "new"})
[event] = Events.list_for(issue)
assert event.data == %{"from" => "old", "to" => "new"}
end
test "preloads the actor, and tolerates not having one" do
{user, repo} = repository_fixture()
issue = issue_fixture(repo, user)
:ok = Events.record(issue, "closed", user)
:ok = Events.record(issue, "pushed", nil, %{to: "abc123"})
[closed, pushed] = Events.list_for(issue)
assert closed.actor.id == user.id
assert pushed.actor == nil
end
test "an unknown kind is dropped rather than raised" do
{user, repo} = repository_fixture()
issue = issue_fixture(repo, user)
# Best-effort by design: a bad event must not fail the action it
# was describing.
assert :ok = Events.record(issue, "not_a_real_kind", user)
assert Events.list_for(issue) == []
end
test "counts only the target's own events" do
{user, repo} = repository_fixture()
one = issue_fixture(repo, user)
two = issue_fixture(repo, user)
:ok = Events.record(one, "opened", user)
:ok = Events.record(one, "closed", user)
:ok = Events.record(two, "opened", user)
assert Events.count_for(one) == 2
assert Events.count_for(two) == 1
end
test "issues and pull requests with the same row id don't collide" do
{user, repo} = repository_fixture()
issue = issue_fixture(repo, user)
# Forge a PR struct carrying the issue's id — the polymorphic key is
# (target_type, target_id), so the same id under a different type
# must stay separate.
pr = %GitGud.PullRequests.PullRequest{id: issue.id}
:ok = Events.record(issue, "opened", user)
:ok = Events.record(pr, "merged", user)
assert [%{kind: "opened"}] = Events.list_for(issue)
assert [%{kind: "merged"}] = Events.list_for(pr)
end
test "works on a struct the context has attached labels to" do
{user, repo} = repository_fixture()
issue = issue_fixture(repo, user)
# `get_issue!/2` Map.puts a :labels key onto the struct; recording
# against that shape has to keep working.
loaded = Issues.get_issue!(repo, issue.number)
:ok = Events.record(loaded, "closed", user)
assert [%{kind: "closed"}] = Events.list_for(loaded)
end
end
neiam /gitgud
Git Gud
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Packages
⭐
Log in to mark this repository.
2.9 KiB · text
History
ce54fcf