Skip to content

Commit

Permalink
test(post): add factory for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
akinsey committed Aug 19, 2024
1 parent c96c9bd commit d134003
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/support/factories/post.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Test.Support.Factories.Post do
@moduledoc """
Factory for `Post`
"""
alias EpochtalkServer.Models.Post

defmacro __using__(_opts) do
quote do
def post_attributes_factory(%{user: user, thread: thread} = attrs) do
%{
"user_id" => user.id,
"thread_id" => thread.id,
"content" => %{
title: Map.get(attrs, :title) || sequence(:post_title, &"RE: Post title #{&1}"),
body: sequence(:post_body, &"Post body #{&1}")
}
}
end

def post_factory(%{user: user, thread: thread} = attrs) do
attributes = build(:post_attributes, attrs)

Post.create(attributes)
end
end
end
end
1 change: 1 addition & 0 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Test.Support.Factory do
use Test.Support.Factories.Category
use Test.Support.Factories.Board
use Test.Support.Factories.Thread
use Test.Support.Factories.Post
use Test.Support.Factories.Mention
use Test.Support.Factories.Notification
use Test.Support.Factories.Poll
Expand Down

0 comments on commit d134003

Please sign in to comment.