Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Technical Interview Template - Database

technical_interview_db.livemd

Technical Interview Template - Database

# Dependencies
Mix.install([
  {:heap, "~> 3.0"},
  {:libgraph, "~> 0.16.0"},
  # visualizations for Livebooks
  {:kino, "~> 0.11.0"},
  # http request library
  {:req, "~> 0.4.4"},
  # Repository data mapper
  {:ecto, "~> 3.10"},
  # GraphQL library
  {:absinthe, "~> 1.7"},
  {:kino_db, "~> 0.2.3"},
  {:postgrex, ">= 0.0.0"},
  {:ecto_sql, "~> 3.10"}
])

# Test Framework
ExUnit.start(autorun: false)

Requirements

Story:

>

Questions / Clarifications

*

Approach

1.

Examples

defmodule Assignment.Fixtures do
  use ExUnit.CaseTemplate

  setup _context do
    %{
      example: %{}
    }
  end
end

Database Setup

opts = [
  hostname: "localhost",
  port: 5432,
  username: "daverussell",
  password: System.fetch_env!("LB_PG_PASSWORD"),
  database: "postgres"
]

{:ok, conn} = Kino.start_child({Postgrex, opts})
defmodule Repo do
  use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.Postgres
end

Kino.start_child({Repo, url: "postgres://postgres:postgres@localhost/postgres"})
defmodule Migrations.Tables do
  use Ecto.Migration

  def up do
    create table("users") do
      timestamps()
    end

    create table("weather") do
      add(:city, :string, size: 40)
      add(:temp_lo, :integer)
      add(:temp_hi, :integer)
      add(:prcp, :float)

      timestamps()
    end
  end

  def down do
    drop(table("weather"))
    drop(table("users"))
  end
end

Ecto.Migrator.down(Repo, 1, Migrations.Tables)
Ecto.Migrator.up(Repo, 1, Migrations.Tables)
defmodule Weather do
  use Ecto.Schema

  schema "weather" do
    field(:city, :string)
    field(:temp_lo, :integer)
    field(:temp_hi, :integer)
    field(:prcp, :float, default: 0.0)

    timestamps()
  end
end

Implementation

defmodule Assignment do
  def run() do
    weather = %Weather{temp_lo: 0, temp_hi: 23}
    Repo.insert!(weather)
  end
end
Repo.all(Weather)
|> Kino.DataTable.new()

Testing

defmodule Test.Assignment do
  import Assignment
  use ExUnit.Case, async: true
  use Assignment.Fixtures

  describe "Assignment.run/1" do
    test "it works" do
      assert %Kino.JS.Live{} = run()
    end
  end
end

ExUnit.run()

Retrospective

Timeline

Start Time End Time Total Time
??? ??? 1:50

Results / Feedback

*