feat:added more persistence code
This commit is contained in:
@@ -87,6 +87,53 @@ defmodule BDS.PostsTest do
|
||||
assert reopened.updated_at >= published.updated_at
|
||||
end
|
||||
|
||||
test "publish_post writes frontmatter to the project data directory and clears draft content" do
|
||||
temp_dir = Path.join(System.tmp_dir!(), "bds-post-publish-#{System.unique_integer([:positive])}")
|
||||
File.mkdir_p!(temp_dir)
|
||||
on_exit(fn -> File.rm_rf(temp_dir) end)
|
||||
|
||||
assert {:ok, project} = BDS.Projects.create_project(%{name: "Filesystem", data_path: temp_dir})
|
||||
|
||||
assert {:ok, post} =
|
||||
BDS.Posts.create_post(%{
|
||||
project_id: project.id,
|
||||
title: "Published Post",
|
||||
excerpt: "Summary",
|
||||
content: "Hello from markdown",
|
||||
tags: ["alpha"],
|
||||
categories: ["notes"],
|
||||
author: "Writer",
|
||||
language: "en",
|
||||
template_slug: "article"
|
||||
})
|
||||
|
||||
assert {:ok, post} = BDS.Posts.update_post(post.id, %{do_not_translate: true})
|
||||
assert {:ok, published} = BDS.Posts.publish_post(post.id)
|
||||
|
||||
assert published.status == :published
|
||||
assert published.content == nil
|
||||
assert published.file_path =~ ~r/^posts\/\d{4}\/\d{2}\/published-post\.md$/
|
||||
assert is_integer(published.published_at)
|
||||
|
||||
full_path = Path.join(temp_dir, published.file_path)
|
||||
assert File.exists?(full_path)
|
||||
|
||||
file_contents = File.read!(full_path)
|
||||
|
||||
assert file_contents =~ "---\nid: #{published.id}\n"
|
||||
assert file_contents =~ "title: Published Post\n"
|
||||
assert file_contents =~ "slug: published-post\n"
|
||||
assert file_contents =~ "status: published\n"
|
||||
assert file_contents =~ "excerpt: Summary\n"
|
||||
assert file_contents =~ "author: Writer\n"
|
||||
assert file_contents =~ "language: en\n"
|
||||
assert file_contents =~ "do_not_translate: true\n"
|
||||
assert file_contents =~ "template_slug: article\n"
|
||||
assert file_contents =~ "tags:\n - alpha\n"
|
||||
assert file_contents =~ "categories:\n - notes\n"
|
||||
assert file_contents =~ "\n---\nHello from markdown\n"
|
||||
end
|
||||
|
||||
defp errors_on(changeset) do
|
||||
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
|
||||
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
defmodule BDS.ProjectsTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
alias BDS.Projects.Project
|
||||
alias BDS.Repo
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
end
|
||||
@@ -37,4 +40,18 @@ defmodule BDS.ProjectsTest do
|
||||
assert refetched_second.is_active == true
|
||||
assert Enum.count(BDS.Projects.list_projects(), & &1.is_active) == 1
|
||||
end
|
||||
|
||||
test "ensure_default_project creates the default project once and keeps it active" do
|
||||
Repo.delete_all(Project)
|
||||
|
||||
assert {:ok, default_project} = BDS.Projects.ensure_default_project()
|
||||
assert default_project.id == "default"
|
||||
assert default_project.name == "My Blog"
|
||||
assert default_project.slug == "my-blog"
|
||||
assert default_project.is_active == true
|
||||
|
||||
assert {:ok, same_project} = BDS.Projects.ensure_default_project()
|
||||
assert same_project.id == default_project.id
|
||||
assert Repo.aggregate(Project, :count, :id) == 1
|
||||
end
|
||||
end
|
||||
|
||||
37
test/bds/scripts_test.exs
Normal file
37
test/bds/scripts_test.exs
Normal file
@@ -0,0 +1,37 @@
|
||||
defmodule BDS.ScriptsTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
{:ok, project} = BDS.Projects.create_project(%{name: "Scripts"})
|
||||
%{project: project}
|
||||
end
|
||||
|
||||
test "create_script creates draft scripts with default entrypoints by kind", %{project: project} do
|
||||
assert {:ok, utility} =
|
||||
BDS.Scripts.create_script(%{
|
||||
project_id: project.id,
|
||||
title: "Importer",
|
||||
kind: :utility,
|
||||
content: "function main() end"
|
||||
})
|
||||
|
||||
assert utility.slug == "importer"
|
||||
assert utility.entrypoint == "main"
|
||||
assert utility.status == :draft
|
||||
assert utility.enabled == true
|
||||
assert utility.version == 1
|
||||
assert utility.file_path == ""
|
||||
|
||||
assert {:ok, macro_script} =
|
||||
BDS.Scripts.create_script(%{
|
||||
project_id: project.id,
|
||||
title: "Render Card",
|
||||
kind: :macro,
|
||||
content: "function render() end"
|
||||
})
|
||||
|
||||
assert macro_script.entrypoint == "render"
|
||||
assert macro_script.slug == "render-card"
|
||||
end
|
||||
end
|
||||
42
test/bds/tags_test.exs
Normal file
42
test/bds/tags_test.exs
Normal file
@@ -0,0 +1,42 @@
|
||||
defmodule BDS.TagsTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
temp_dir = Path.join(System.tmp_dir!(), "bds-tags-#{System.unique_integer([:positive])}")
|
||||
File.mkdir_p!(temp_dir)
|
||||
on_exit(fn -> File.rm_rf(temp_dir) end)
|
||||
|
||||
{:ok, project} = BDS.Projects.create_project(%{name: "Tags", data_path: temp_dir})
|
||||
%{project: project, temp_dir: temp_dir}
|
||||
end
|
||||
|
||||
test "create_tag persists the row and rewrites meta/tags.json sorted by name", %{project: project, temp_dir: temp_dir} do
|
||||
assert {:ok, zebra} = BDS.Tags.create_tag(%{project_id: project.id, name: "Zebra", color: "#000000"})
|
||||
assert {:ok, alpha} = BDS.Tags.create_tag(%{project_id: project.id, name: "Alpha"})
|
||||
|
||||
assert zebra.name == "Zebra"
|
||||
assert zebra.color == "#000000"
|
||||
assert alpha.color == nil
|
||||
|
||||
tags_path = Path.join([temp_dir, "meta", "tags.json"])
|
||||
assert File.exists?(tags_path)
|
||||
|
||||
assert %{"tags" => [%{"name" => "Alpha"}, %{"color" => "#000000", "name" => "Zebra"}]} =
|
||||
Jason.decode!(File.read!(tags_path))
|
||||
end
|
||||
|
||||
test "create_tag rejects case-insensitive duplicates per project", %{project: project} do
|
||||
assert {:ok, _tag} = BDS.Tags.create_tag(%{project_id: project.id, name: "Elixir"})
|
||||
assert {:error, changeset} = BDS.Tags.create_tag(%{project_id: project.id, name: "elixir"})
|
||||
assert "has already been taken" in errors_on(changeset).name
|
||||
end
|
||||
|
||||
defp errors_on(changeset) do
|
||||
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
|
||||
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
|
||||
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
|
||||
end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
31
test/bds/templates_test.exs
Normal file
31
test/bds/templates_test.exs
Normal file
@@ -0,0 +1,31 @@
|
||||
defmodule BDS.TemplatesTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
{:ok, project} = BDS.Projects.create_project(%{name: "Templates"})
|
||||
%{project: project}
|
||||
end
|
||||
|
||||
test "create_template creates a draft template with slug deduplication", %{project: project} do
|
||||
assert {:ok, template} =
|
||||
BDS.Templates.create_template(%{
|
||||
project_id: project.id,
|
||||
title: "Article View",
|
||||
kind: :post,
|
||||
content: "<article>{{ content }}</article>"
|
||||
})
|
||||
|
||||
assert template.slug == "article-view"
|
||||
assert template.status == :draft
|
||||
assert template.enabled == true
|
||||
assert template.version == 1
|
||||
assert template.file_path == ""
|
||||
assert template.content == "<article>{{ content }}</article>"
|
||||
|
||||
assert {:ok, duplicate} =
|
||||
BDS.Templates.create_template(%{project_id: project.id, title: "Article View", kind: :post, content: "x"})
|
||||
|
||||
assert duplicate.slug == "article-view-2"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user