My space on the web https://inhji.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

139 lines
4.5 KiB

defmodule Mirage.NotesTest do
use Mirage.DataCase
alias Mirage.Notes
alias Mirage.Notes.Note
alias Mirage.Tags
describe "notes" do
@valid_attrs %{content: "some content", title: "some title"}
@at_attrs %{content: "some content", title: "@home"}
@update_attrs %{content: "some updated content"}
@update_attrs_with_link %{
content: "some updated content with a reference [[@OWN_ID]] [[@NOTE_ID]]"
}
@update_attrs_with_link_and_title %{
content: "some updated content with a reference [[@OWN_ID|Link to myself]] [[@NOTE_ID]]"
}
@invalid_attrs %{content: nil}
def note_fixture(attrs \\ %{}) do
{:ok, note} =
attrs
|> Enum.into(@valid_attrs)
|> Notes.create_note()
note
|> Notes.preload_note()
end
test "list_notes/0 returns all notes" do
note = note_fixture()
assert Notes.list_notes() == [note]
end
test "list_notes/1 with param :at returns all @ notes" do
note = note_fixture(@at_attrs)
assert Notes.list_notes(:at) == [note]
end
test "list_notes/1 with param :untagged returns all untagged notes" do
note = note_fixture(@valid_attrs)
assert Notes.list_notes(:untagged) == [note]
Tags.update_tags(note, "foo,bar")
assert Notes.list_notes(:untagged) == []
end
test "list_notes/1 with param :unlinked returns all untagged notes" do
note = note_fixture(@valid_attrs)
assert Notes.list_notes(:unlinked) == [note]
note2 = note_fixture(@valid_attrs |> Map.put(:content, "link to [[#{note.id}]]"))
Notes.link_note(note2)
assert Notes.list_notes(:unlinked) == []
end
test "list_notes/1 with param :today returns today's note and creates it otherwise" do
title = Notes.today_title()
assert Notes.get_note_by_title(title) == nil
[note] = Notes.list_notes(:today)
assert Notes.get_note_by_title(title) |> Notes.preload_note() == note
end
test "get_note!/1 returns the note with given id" do
note = note_fixture()
assert Notes.get_note!(note.id) |> Notes.preload_note() == note
end
test "create_note/1 with valid data creates a note" do
assert {:ok, %Note{} = note} = Notes.create_note(@valid_attrs)
assert note.content == "some content"
assert note.title == "some title"
end
test "create_note/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Notes.create_note(@invalid_attrs)
end
test "update_note/2 with valid data updates the note" do
note = note_fixture()
assert {:ok, %Note{} = note} = Notes.update_note(note, @update_attrs)
assert note.content == "some updated content"
end
test "update_note/2 with a cross-ref link renders a link to the referenced note" do
note = note_fixture()
attrs =
Map.update!(@update_attrs_with_link, :content, fn content ->
content
|> String.replace("@OWN_ID", to_string(note.id))
|> String.replace("@NOTE_ID", to_string(note.id + 1))
end)
assert {:ok, %Note{} = note} = Notes.update_note(note, attrs)
assert note.content ==
"some updated content with a reference [[#{note.id}]] [[#{note.id + 1}]]"
assert note.content_html =~ "#{note.title}"
end
test "update_note/2 with a cross-ref link and title renders a link to the referenced note" do
note = note_fixture()
attrs =
Map.update!(@update_attrs_with_link_and_title, :content, fn content ->
content
|> String.replace("@OWN_ID", to_string(note.id))
|> String.replace("@NOTE_ID", to_string(note.id + 1))
end)
assert {:ok, %Note{} = note} = Notes.update_note(note, attrs)
assert note.content ==
"some updated content with a reference [[#{note.id}|Link to myself]] [[#{
note.id + 1
}]]"
assert note.content_html =~ "Link to myself"
end
test "update_note/2 with invalid data returns error changeset" do
note = note_fixture()
assert {:error, %Ecto.Changeset{}} = Notes.update_note(note, @invalid_attrs)
assert note == Notes.get_note!(note.id) |> Notes.preload_note()
end
test "delete_note/1 deletes the note" do
note = note_fixture()
assert {:ok, %Note{}} = Notes.delete_note(note)
assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id) end
end
test "change_note/1 returns a note changeset" do
note = note_fixture()
assert %Ecto.Changeset{} = Notes.change_note(note)
end
end
end