Skip to content

Commit

Permalink
add Org.backfill/0 function to get orgs as part of the background pro…
Browse files Browse the repository at this point in the history
…cess #226 - back up to 100% cov 😉
  • Loading branch information
nelsonic committed Feb 1, 2025
1 parent a8d42a8 commit c84e15a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
8 changes: 2 additions & 6 deletions lib/app/api_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ defmodule App.ApiManager do
App.User.get_user_from_api(user)
App.Orgmember.get_orgs_for_user(user)
end)

App.Org.list_incomplete_orgs()
|> Enum.each(fn org ->
App.Org.get_org_from_api(org)
end)

# Backfill 5 orgs with full data:
App.Org.backfill()
end
end
end
21 changes: 18 additions & 3 deletions lib/app/org.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule App.Org do
@doc false
def changeset(org, attrs) do
org
|> cast(attrs, [:id, :login, :avatar_url, :description, :name, :company, :created_at, :public_repos, :location, :followers, :show])
|> cast(attrs, [:id, :login, :avatar_url, :hex, :description, :name, :company, :created_at, :public_repos, :location, :followers, :show])
|> validate_required([:id, :login, :avatar_url])
end

Expand All @@ -38,9 +38,14 @@ defmodule App.Org do
|> Repo.insert(on_conflict: :replace_all, conflict_target: [:id]) # upsert
end

def get_org(login) do
from(o in Org, where: o.login == ^login)
|> Repo.one()
end

# `org` map must include the `id` and `login` fields
def get_org_from_api(org) do
data = App.GitHub.org(org.login) # |> dbg()
data = App.GitHub.org(org.login)
# Not super happy about this crude error handling ... feel free to refactor.
if Map.has_key?(data, :status) && data.status == "404" do
# {:ok, user} = dummy_data(user) |> create() # don't insert dummy data!
Expand Down Expand Up @@ -69,9 +74,19 @@ defmodule App.Org do
# Therefore we need to back-fill the data by selecting and querying
# SELECT COUNT(*) FROM orgs WHERE created_at IS NULL
def list_incomplete_orgs do
from(o in Org, select: %{login: o.login}, where: is_nil(o.created_at))
from(o in Org,
select: %{login: o.login},
where: is_nil(o.created_at)
)
|> limit(5)
|> order_by(desc: :inserted_at)
|> Repo.all()
end

def backfill do
list_incomplete_orgs()
|> Enum.each(fn org ->
get_org_from_api(org)
end)
end
end
9 changes: 4 additions & 5 deletions test/app/github_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule App.GitHubTest do
assert length(list) > 2

[org | _] = Enum.filter(list, fn org -> org.login == "dwyl" end)
assert org.id == 11708465
assert org.id == 11_708_465
end

test "App.GitHub.org_user_list/1" do
Expand All @@ -32,15 +32,14 @@ defmodule App.GitHubTest do
end

test "App.GitHub.user/1 known 404 (unhappy path)" do
username ="kittenking"
username = "kittenking"
data = App.GitHub.user(username)
assert data.status == "404"
end

test "App.GitHub.org/1 get org data" do
org = "ideaq"
App.GitHub.org(org) |> dbg
# assert length(list) > 2
org = App.GitHub.org("ideaq")
assert org.id == 6_831_072
end

test "App.GitHub.org_repos/1 get repos for org" do
Expand Down
40 changes: 40 additions & 0 deletions test/app/org_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
defmodule App.OrgTest do
use App.DataCase


test "App.Org.create/1" do
org = %{
id: 123,
avatar_url: "https://avatars.githubusercontent.com/u/4185328?v=4",
blog: "https://blog.dwyl.com",
company: "@dwyl",
Expand All @@ -17,4 +19,42 @@ defmodule App.OrgTest do
assert {:ok, inserted_org} = App.Org.create(org)
assert inserted_org.name == org.name
end

test "get_org_from_api/1 retrieves and inserts the org into DB" do
org = App.Org.get_org_from_api(%{login: "dwyl"})
assert org.id == 11_708_465
assert org.followers > 650 # https://github.com/orgs/dwyl/followers
assert org.hex == "48B8A8" # https://www.colorhexa.com/48b8a8
end

test "get_org_from_api/1 -> 404 (unhappy path)" do
data = %{login: "superlongnamepleasedontregister"}
org = App.Org.get_org_from_api(data)
assert org.login == data.login
end

test "App.Org.list_incomplete_orgs" do
{:ok, org} = App.Org.create(%{
id: 123,
avatar_url: "https://avatars.githubusercontent.com/u/4185328?v=4",
login: "dwyl"
})
list = App.Org.list_incomplete_orgs()
assert length(list) > 0
o = Enum.filter(list, fn o -> o.login == org.login end) |> List.first
assert o.login == org.login
end

test "App.Org.backfill" do
{:ok, org} = App.Org.create(%{
id: 6_831_072,
avatar_url: "https://avatars.githubusercontent.com/u/6831072",
login: "ideaq"
})
App.Org.backfill()
updated_org = App.Org.get_org(org.login)
assert updated_org.hex == "F8F8F8"
assert updated_org.description == "a Q of Ideas"
assert updated_org.created_at == "2014-03-02T13:18:11Z"
end
end
2 changes: 1 addition & 1 deletion test/app/user_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule App.UserTest do
end

test "list_users_avatars/0" do
user = user() |> User.map_github_user_fields_to_table()
user = user() |> User.map_github_fields_to_table()
User.create(user)
list = App.User.list_users_avatars()
assert length(list) > 0
Expand Down

0 comments on commit c84e15a

Please sign in to comment.