Skip to content

Commit

Permalink
Item view test for index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
maiphuong-van committed Sep 7, 2020
1 parent 22e82c1 commit 6fc4cbe
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 30 deletions.
30 changes: 0 additions & 30 deletions test/crawly_ui/manager/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ defmodule CrawlyUi.ManagerTest do
alias CrawlyUI.Manager.Job
alias CrawlyUI.Manager.Item

@job_abandoned_timeout 60 * 30 + 10

describe "update_job_status/0" do
test "updates job state to abandoned" do
%{id: job_id_1} = insert_job()
Expand Down Expand Up @@ -394,32 +392,4 @@ defmodule CrawlyUi.ManagerTest do

assert %Ecto.Changeset{} = Manager.change_item(item)
end

## Private function for inserting up jobs and items
defp insert_job(params \\ %{}) do
params =
Map.merge(%{spider: "Crawly", state: "running", tag: "test", node: "crawly@test"}, params)

Repo.insert!(struct(Job, params))
end

defp insert_item(job_id, inserted_at \\ nil, data \\ %{}) do
inserted_at =
case inserted_at do
nil -> inserted_at_valid()
_ -> inserted_at
end

Repo.insert!(%Item{job_id: job_id, inserted_at: inserted_at, data: data})
end

defp inserted_at_valid(), do: inserted_at(0)

defp inserted_at_expired, do: inserted_at(@job_abandoned_timeout)

defp inserted_at(shift) do
NaiveDateTime.utc_now()
|> NaiveDateTime.add(shift * -1, :second)
|> NaiveDateTime.truncate(:second)
end
end
85 changes: 85 additions & 0 deletions test/crawly_ui_web/views/item_view_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
defmodule CrawlyUIWeb.ItemViewTest do
import CrawlyUI.DataCase
use CrawlyUIWeb.ConnCase

import Phoenix.View
import Ecto.Query

describe "render jobs/:job_id/items/index.html" do
test "shows list items" do
%{id: job_id} = insert_job()
insert_item(job_id, nil, %{"field_1" => "data_1_1", "field_2" => "data_2_1"})
insert_item(job_id, nil, %{"field_1" => "data_1_2", "field_2" => "data_2_2"})

query = from i in CrawlyUI.Manager.Item, where: i.job_id == ^job_id
items = CrawlyUI.Repo.all(query)
page = CrawlyUI.Repo.paginate(query, %{})

params = [items: items, page: page, search: nil]

rendered_string = render_to_string(CrawlyUIWeb.ItemView, "index.html", params)

assert rendered_string =~
"<td class=\"w\"><b>field_1</b></td>\n <td class=\"c\">data_1_1</td>"

assert rendered_string =~
"<td class=\"w\"><b>field_2</b></td>\n <td class=\"c\">data_2_1</td>"

assert rendered_string =~
"<td class=\"w\"><b>field_1</b></td>\n <td class=\"c\">data_1_2</td>"

assert rendered_string =~
"<td class=\"w\"><b>field_2</b></td>\n <td class=\"c\">data_2_2</td>"
end

test " with field value of list type in item data" do
params = index_params(%{"list" => ["data_1", "data_2"]})

assert render_to_string(CrawlyUIWeb.ItemView, "index.html", params) =~
"<td class=\"c\">data_2data_1</td>"
end

test " with image field in item data" do
params = index_params(%{"image" => "image_src"})

assert render_to_string(CrawlyUIWeb.ItemView, "index.html", params) =~
"<td class=\"c\"><img width='150px' src='image_src' /></td>"
end

test " with url field in item data" do
params = index_params(%{"url" => "https://example.com"})

rendered_string = render_to_string(CrawlyUIWeb.ItemView, "index.html", params)

assert rendered_string =~
"<td class=\"c\"><a target='blank' href='https://example.com'>https://example.com</a></td>"

assert rendered_string =~
~r/<th>Item<\/th>\n <th>Discovery time: (.)* \[<a href='\/jobs\/(.)*\/items\/(.)*'> Preview <\/a>\]<\/th>/
end

test "with empty search string" do
params = index_params(%{"field" => "value"}, nil)

assert render_to_string(CrawlyUIWeb.ItemView, "index.html", params) =~
"<div class=\"column\"><input type=\"text\" placeholder=\"Search\" name=\"search\"></div>"
end

test "with search param" do
params = index_params(%{"field" => "value"}, "search string")

assert render_to_string(CrawlyUIWeb.ItemView, "index.html", params) =~
"<div class=\"column\"><input type=\"text\" placeholder=\"search string\" name=\"search\"></div>"
end
end

defp index_params(data, search \\ nil) do
%{id: job_id} = insert_job()
insert_item(job_id, nil, data)
query = from i in CrawlyUI.Manager.Item, where: i.job_id == ^job_id
items = CrawlyUI.Repo.all(query)
page = CrawlyUI.Repo.paginate(query, %{})

[items: items, page: page, search: search]
end
end
33 changes: 33 additions & 0 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule CrawlyUI.DataCase do
import Ecto.Changeset
import Ecto.Query
import CrawlyUI.Manager
import CrawlyUI.DataCase
end
end

Expand Down Expand Up @@ -52,4 +53,36 @@ defmodule CrawlyUI.DataCase do
end)
end)
end

def insert_job(params \\ %{}) do
params =
Map.merge(%{spider: "Crawly", state: "running", tag: "test", node: "crawly@test"}, params)

CrawlyUI.Repo.insert!(struct(CrawlyUI.Manager.Job, params))
end

def insert_item(job_id, inserted_at \\ nil, data \\ %{}) do
inserted_at =
case inserted_at do
nil -> inserted_at_valid()
_ -> inserted_at
end

CrawlyUI.Repo.insert!(%CrawlyUI.Manager.Item{
job_id: job_id,
inserted_at: inserted_at,
data: data
})
end

def inserted_at_valid(), do: inserted_at(0)

@job_abandoned_timeout 60 * 30 + 10
def inserted_at_expired, do: inserted_at(@job_abandoned_timeout)

def inserted_at(shift) do
NaiveDateTime.utc_now()
|> NaiveDateTime.add(shift * -1, :second)
|> NaiveDateTime.truncate(:second)
end
end

0 comments on commit 6fc4cbe

Please sign in to comment.