require 'test_helper'

class PostTest < ActiveSupport::TestCase
  setup do
    @post1 = posts(:post1)
    @post2 = posts(:post2)
  end

  test "published returns posts where published_at is not nil" do
    @post2.update_attributes(published_at: nil)

    posts = Post.published

    assert_not_nil posts
    assert_equal [@post1], posts
  end

  test "draft returns posts where published_at is nil" do
    @post2.update_attributes(published_at: nil)

    posts = Post.draft

    assert_not_nil posts
    assert_equal [@post2], posts
  end

  test "published? returns true if published_at is not nil" do
    @post2.update_attributes(published_at: nil)

    assert_equal true, @post1.published?
    assert_equal false, @post2.published?
  end

  test "draft? returns true if published_at is nil" do
    @post2.update_attributes(published_at: nil)

    assert_equal false, @post1.draft?
    assert_equal true, @post2.draft?
  end

  test "published_at_slug returns year/month/day if published" do
    @post2.update_attributes(published_at: nil)

    assert_equal "2015/02/01", @post1.published_at_slug
    assert_nil @post2.published_at_slug

  end

  test "should set slug if nil" do
    @post1.update_attributes(title: "Testing", slug: nil)

    assert_equal "testing", @post1.slug
  end

  test "should update html on save" do
    @post1.update_attributes(body_markdown: "Hello, World!")

    assert_equal "<p>Hello, World!</p>\n", @post1.body_html
  end
end
