require 'rubygems'
require 'bundler/setup'
require 'test/unit'

require File.expand_path(File.dirname(__FILE__) + '/stubs')
require File.expand_path(File.dirname(__FILE__) + '/../model/regulacion')

class TestRegulacion < Test::Unit::TestCase
  def setup
    path = File.dirname(__FILE__) + '/fixtures/regulaciones.csv'
    @regulaciones = Regulacion.many_from_csv(path)
  end

  def teardown
    Regulacion.delete(@regulaciones.first)
    Regulacion.delete(@regulaciones.last)
  end

  def test_it_initializes_many_from_csv
    assert (@regulaciones.size == 6), "Should have 6 objects"
  end

  def test_it_initializes_from_a_csv
    r1 = @regulaciones.first

    expected = ("ACM-929-558" == r1.clave)
    assert expected, "Should initialize property @clave"
    expected = (/Manual de procedimiento/ =~ r1.tema)
    assert expected, "Should initialize property @tema"
    expected = (/Creación de Manual de procedimiento/ =~ r1.que)
    assert expected, "Should initialize property @que"
    expected = (r1.quien == 'ACM, Association of Computing Machinery')
    assert expected, "Should initialize property @quien"
    expected = (/Vacío Regulatorio/ =~ r1.porque)
    assert expected, "Should initialize property @porque"
    expected = (/Establecer los requisitos que/ =~ r1.objetivo)
    assert expected, "Should initialize property @objetivo"
  end

  def test_it_outputs_markdown
    r1 = @regulaciones.first

    expected = (/Regulación:\*\* \w+/ =~ r1.to_markdown)
    assert expected, "Should output markdown including @tema"
    expected = (/de la propuesta\?\*\*  \n#{r1.objetivo}/ =~ r1.to_markdown)
    assert expected, "Should output markdown including @objetivo"
  end

  def test_it_inserts_regulacion_into_database
    r1 = @regulaciones.first

    Regulacion.create(r1)
    found = Regulacion.find(r1)
    assert found, "Should find a row in database"

    Regulacion.delete(r1)
    found = Regulacion.find(r1)
    assert found.empty?, "Should erase the row in database"
  end

  def test_it_returns_id_on_create
    r1 = @regulaciones.first
    r1_id = Regulacion.create(r1)
    assert (/\d+/ =~ r1_id.to_s), "Create returns id"
  end

  def test_it_adds_categories_on_create
    r1 = @regulaciones.first
    Regulacion.create(r1)
    assert r1.has_category?('cofemer'), "Should have category cofemer"
    assert r1.has_category?('regulación'), "Should have category regulación"
    assert r1.has_category?(r1.dependencia), "Should have category #{r1.dependencia}"
  end

  def test_it_adds_content_on_create
    r1 = @regulaciones.first
    Regulacion.create(r1)
    expected = (r1.introduction.length == r1.to_markdown.length)
    assert expected, "Should have inserted markdown output as doc intro"
  end

  def test_it_adds_group_cofemer_on_create
    r1 = @regulaciones.first
    Regulacion.create(r1)
    group = ENV['MADISON_GROUP']
    assert r1.has_group?(group), "Should have group #{group}"
  end

  def test_it_adds_default_comments_on_create
    r1 = @regulaciones.first
    Regulacion.create(r1)
    count = r1.comments.size
    assert count == 4, "Should add 4 default comments, found #{count}"
  end

  def test_it_truncates_too_long_titles
    r6 = @regulaciones.last
    r6.tema = ':godmode:' * 1024
    assert_nothing_raised do
      Regulacion.create(r6)
    end
    found = Regulacion.find(r6)
    assert found['title'].length < 256, "Should keep title <= 255 chars"
  end

end

