From e51f32e1a2c129f24d338f25c662b953da5c7a6e Mon Sep 17 00:00:00 2001 From: nelsonic Date: Sat, 8 Jun 2024 13:05:45 +0100 Subject: [PATCH] add test/app/org_test.exs for #13 --- lib/app/github.ex | 3 ++- lib/app/org.ex | 21 ++++++++++++++----- lib/app/repository.ex | 2 -- .../20221005213110_create_users.exs | 2 +- .../migrations/20240608112859_create_orgs.exs | 10 +++++---- test/app/github_test.exs | 4 ++-- test/app/org_test.exs | 20 ++++++++++++++++++ 7 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 test/app/org_test.exs diff --git a/lib/app/github.ex b/lib/app/github.ex index 3a01a5d..33072d6 100644 --- a/lib/app/github.ex +++ b/lib/app/github.ex @@ -14,7 +14,8 @@ defmodule App.GitHub do """ def repository(owner, reponame) do Logger.info "Fetching repository #{owner}/#{reponame}" - {_status, data, _res} = Tentacat.Repositories.repo_get(@client, owner, reponame) + {_status, data, _res} = + Tentacat.Repositories.repo_get(@client, owner, reponame) data |> Useful.atomize_map_keys() end diff --git a/lib/app/org.ex b/lib/app/org.ex index c08a718..6a81121 100644 --- a/lib/app/org.ex +++ b/lib/app/org.ex @@ -1,16 +1,20 @@ defmodule App.Org do + alias App.{Repo} use Ecto.Schema import Ecto.Changeset + require Logger + alias __MODULE__ schema "orgs" do - field :name, :string + field :avatar_url, :string + field :blog, :string + field :company, :string field :description, :string + field :followers, :integer field :location, :string field :login, :string - field :avatar_url, :string - field :company, :string + field :name, :string field :public_repos, :integer - field :followers, :integer timestamps() end @@ -22,5 +26,12 @@ defmodule App.Org do |> validate_required([:login, :avatar_url, :name, :company, :public_repos, :description, :followers]) end - + @doc """ + Creates an `org`. + """ + def create(attrs) do + %Org{} + |> changeset(attrs) + |> Repo.insert() + end end diff --git a/lib/app/repository.ex b/lib/app/repository.ex index 81abe92..2740461 100644 --- a/lib/app/repository.ex +++ b/lib/app/repository.ex @@ -38,6 +38,4 @@ defmodule App.Repository do |> Repo.insert() end - - end diff --git a/priv/repo/migrations/20221005213110_create_users.exs b/priv/repo/migrations/20221005213110_create_users.exs index bcdf560..bfe3f83 100644 --- a/priv/repo/migrations/20221005213110_create_users.exs +++ b/priv/repo/migrations/20221005213110_create_users.exs @@ -3,8 +3,8 @@ defmodule App.Repo.Migrations.CreateUsers do def change do create table(:users) do - add :login, :string add :avatar_url, :string + add :login, :string add :name, :string add :company, :string add :bio, :string diff --git a/priv/repo/migrations/20240608112859_create_orgs.exs b/priv/repo/migrations/20240608112859_create_orgs.exs index 2d99279..577a9fb 100644 --- a/priv/repo/migrations/20240608112859_create_orgs.exs +++ b/priv/repo/migrations/20240608112859_create_orgs.exs @@ -3,14 +3,16 @@ defmodule App.Repo.Migrations.CreateOrgs do def change do create table(:orgs) do - add :login, :string add :avatar_url, :string - add :name, :string + add :blog, :string add :company, :string - add :public_repos, :integer - add :location, :string + add :created_at, :string add :description, :string add :followers, :integer + add :location, :string + add :login, :string + add :name, :string + add :public_repos, :integer timestamps() end diff --git a/test/app/github_test.exs b/test/app/github_test.exs index 5f0338f..6d15aea 100644 --- a/test/app/github_test.exs +++ b/test/app/github_test.exs @@ -6,7 +6,7 @@ defmodule App.GitHubTest do owner = "dwyl" reponame = "start-here" - GitHub.repository(owner, reponame) |> IO.inspect + GitHub.repository(owner, reponame) # |> IO.inspect # assert html_response(conn, 200) =~ "LiveView App Page" end @@ -15,4 +15,4 @@ defmodule App.GitHubTest do GitHub.user(user) # |> IO.inspect # assert html_response(conn, 200) =~ "LiveView App Page" end -end \ No newline at end of file +end diff --git a/test/app/org_test.exs b/test/app/org_test.exs new file mode 100644 index 0000000..46676c9 --- /dev/null +++ b/test/app/org_test.exs @@ -0,0 +1,20 @@ +defmodule App.OrgTest do + use App.DataCase + + test "App.Org.create/1" do + org = %{ + avatar_url: "https://avatars.githubusercontent.com/u/4185328?v=4", + blog: "https://blog.dwyl.com", + company: "@dwyl", + created_at: "2013-04-17T21:10:06Z", + description: "the dwyl org", + followers: 378, + location: "London, UK", + login: "dwyl", + name: "dwyl", + public_repos: 31 + } + assert {:ok, inserted_org} = App.Org.create(org) + assert inserted_org.name == org.name + end +end