<commit_before>require File.dirname(__FILE__) + '/../test_helper'

class VersionTest < ActiveSupport::TestCase
  should_belong_to :rubygem
  should_have_many :dependencies
  should_have_many :downloads, :dependent => :destroy

  context "with a rubygem" do
    setup do
      @rubygem = Factory(:rubygem)
    end

    should "not allow duplicate versions" do
      @version = Factory.build(:version, :rubygem => @rubygem, :number => "1.0.0", :platform => "ruby")
      @dup_version = @version.dup
      @number_version = Factory.build(:version, :rubygem => @rubygem, :number => "2.0.0", :platform => "ruby")
      @platform_version = Factory.build(:version, :rubygem => @rubygem, :number => "1.0.0", :platform => "mswin32")

      assert @version.save
      assert @number_version.save
      assert @platform_version.save
      assert ! @dup_version.valid?
    end
  end

  context "with a version" do
    setup do
      @version = Factory(:version)
      @info = "some info"
    end
    subject { @version }

    should_not_allow_values_for :number, "#YAML<CEREALIZATION-FAIL>",
                                         "1.2.3-\"[javalol]\"",
                                         "0.8.45::Gem::PLATFORM::FAILBOAT"


    should "give number for #to_s" do
      assert_equal @version.number, @version.to_s
    end

    should "return just number for to_slug if platform is ruby" do
      assert ! @version.platformed?
      assert_equal @version.number, @version.to_slug
      assert_equal @version, Version.find_from_slug!(@version.rubygem_id, @version.number)
    end

    should "raise an ActiveRecord::RecordNotFound if an invalid slug is given" do
      assert_raise ActiveRecord::RecordNotFound do
        Version.find_from_slug!(@version.rubygem_id, "some stupid version 399")
      end
    end

    %w[x86_64-linux java mswin x86-mswin32-60].each do |platform|
      should "be able to deal with platform of #{platform}" do
        @version.update_attribute(:platform, platform)
        slug = "#{@version.number}-#{platform}"

        assert @version.platformed?
        assert_equal slug, @version.to_slug
        assert_equal @version, Version.find_from_slug!(@version.rubygem_id, slug)
      end
    end

    should "have a default download count" do
      assert @version.downloads_count.zero?
    end

    should "give no version flag for the latest version" do
      new_version = Factory(:version, :rubygem => @version.rubygem, :built_at => 1.day.from_now)

      assert_equal "gem install #{@version.rubygem.name} -v #{@version.number}", @version.to_install
      assert_equal "gem install #{new_version.rubygem.name}", new_version.to_install
    end

    should "tack on prerelease flag" do
      @version.update_attribute(:number, "0.3.0.pre")
      new_version = Factory(:version, :rubygem  => @version.rubygem,
                                      :built_at => 1.day.from_now,
                                      :number   => "0.4.0.pre")

      assert @version.prerelease
      assert new_version.prerelease

      assert_equal "gem install #{@version.rubygem.name} -v #{@version.number} --pre",
        @version.to_install
      assert_equal "gem install #{new_version.rubygem.name} --pre",
        new_version.to_install
    end


    should "give title for #to_title" do
      assert_equal "#{@version.rubygem.name} (#{@version.to_s})", @version.to_title
    end

    should "have description for info" do
      @version.description = @info
      assert_equal @info, @version.info
    end

    should "have summary for info if description does not exist" do
      @version.description = nil
      @version.summary = @info
      assert_equal @info, @version.info
    end

    should "have summary for info if description is blank" do
      @version.description = ""
      @version.summary = @info
      assert_equal @info, @version.info
    end

    should "have some text for info if neither summary or description exist" do
      @version.description = nil
      @version.summary = nil
      assert_equal "This rubygem does not have a description or summary.", @version.info
    end

    should "create a gem spec" do
      spec = @version.to_spec
      assert spec.is_a?(Gem::Specification)
      assert_equal @version.rubygem.name, spec.name
      assert_equal @version.number, spec.version.to_s
      assert_equal [@version.authors], spec.authors
      assert_equal @version.description, spec.description
      assert_equal @version.summary, spec.summary

      date = @version.built_at
      assert_equal Time.local(date.year, date.month, date.day), spec.date
    end

    should "join multiple authors on gemspecs" do
      @version.authors = "<NAME>, <NAME>, <NAME>"
      assert_equal ["<NAME>", "<NAME>", "<NAME>"], @version.to_spec.authors
    end

    should "create gemspec with some dependencies" do
      @dep_one = Factory(:dependency, :version => @version, :requirements => ">= 0, = 1.2.3")
      @dep_two = Factory(:dependency, :version => @version, :requirements => "= 3.0.0")
      spec = @version.to_spec

      assert_equal 2, spec.dependencies.size
      assert_equal @dep_one.rubygem.name, spec.dependencies.last.name
      assert_equal @dep_one.requirements.split(", "), spec.dependencies.last.requirements_list

      assert_equal @dep_two.rubygem.name, spec.dependencies.first.name
      assert_equal [@dep_two.requirements], spec.dependencies.first.requirements_list
    end
  end

  context "when indexing" do
    setup do
      @rubygem = Factory(:rubygem)
      @first_version  = Factory(:version, :rubygem => @rubygem, :number => "0.0.1", :built_at => 7.days.ago)
      @second_version = Factory(:version, :rubygem => @rubygem, :number => "0.0.2", :built_at => 6.days.ago)
      @third_version  = Factory(:version, :rubygem => @rubygem, :number => "0.0.3", :built_at => 5.days.ago)
      @fourth_version = Factory(:version, :rubygem => @rubygem, :number => "0.0.4", :built_at => 5.days.ago)
    end

    should "always sort properly" do<commit_msg>fix failing unit test on Version#to_spec<commit_after>require File.dirname(__FILE__) + '/../test_helper'

class VersionTest < ActiveSupport::TestCase
  should_belong_to :rubygem
  should_have_many :dependencies
  should_have_many :downloads, :dependent => :destroy

  context "with a rubygem" do
    setup do
      @rubygem = Factory(:rubygem)
    end

    should "not allow duplicate versions" do
      @version = Factory.build(:version, :rubygem => @rubygem, :number => "1.0.0", :platform => "ruby")
      @dup_version = @version.dup
      @number_version = Factory.build(:version, :rubygem => @rubygem, :number => "2.0.0", :platform => "ruby")
      @platform_version = Factory.build(:version, :rubygem => @rubygem, :number => "1.0.0", :platform => "mswin32")

      assert @version.save
      assert @number_version.save
      assert @platform_version.save
      assert ! @dup_version.valid?
    end
  end

  context "with a version" do
    setup do
      @version = Factory(:version)
      @info = "some info"
    end
    subject { @version }

    should_not_allow_values_for :number, "#YAML<CEREALIZATION-FAIL>",
                                         "1.2.3-\"[javalol]\"",
                                         "0.8.45::Gem::PLATFORM::FAILBOAT"


    should "give number for #to_s" do
      assert_equal @version.number, @version.to_s
    end

    should "return just number for to_slug if platform is ruby" do
      assert ! @version.platformed?
      assert_equal @version.number, @version.to_slug
      assert_equal @version, Version.find_from_slug!(@version.rubygem_id, @version.number)
    end

    should "raise an ActiveRecord::RecordNotFound if an invalid slug is given" do
      assert_raise ActiveRecord::RecordNotFound do
        Version.find_from_slug!(@version.rubygem_id, "some stupid version 399")
      end
    end

    %w[x86_64-linux java mswin x86-mswin32-60].each do |platform|
      should "be able to deal with platform of #{platform}" do
        @version.update_attribute(:platform, platform)
        slug = "#{@version.number}-#{platform}"

        assert @version.platformed?
        assert_equal slug, @version.to_slug
        assert_equal @version, Version.find_from_slug!(@version.rubygem_id, slug)
      end
    end

    should "have a default download count" do
      assert @version.downloads_count.zero?
    end

    should "give no version flag for the latest version" do
      new_version = Factory(:version, :rubygem => @version.rubygem, :built_at => 1.day.from_now)

      assert_equal "gem install #{@version.rubygem.name} -v #{@version.number}", @version.to_install
      assert_equal "gem install #{new_version.rubygem.name}", new_version.to_install
    end

    should "tack on prerelease flag" do
      @version.update_attribute(:number, "0.3.0.pre")
      new_version = Factory(:version, :rubygem  => @version.rubygem,
                                      :built_at => 1.day.from_now,
                                      :number   => "0.4.0.pre")

      assert @version.prerelease
      assert new_version.prerelease

      assert_equal "gem install #{@version.rubygem.name} -v #{@version.number} --pre",
        @version.to_install
      assert_equal "gem install #{new_version.rubygem.name} --pre",
        new_version.to_install
    end


    should "give title for #to_title" do
      assert_equal "#{@version.rubygem.name} (#{@version.to_s})", @version.to_title
    end

    should "have description for info" do
      @version.description = @info
      assert_equal @info, @version.info
    end

    should "have summary for info if description does not exist" do
      @version.description = nil
      @version.summary = @info
      assert_equal @info, @version.info
    end

    should "have summary for info if description is blank" do
      @version.description = ""
      @version.summary = @info
      assert_equal @info, @version.info
    end

    should "have some text for info if neither summary or description exist" do
      @version.description = nil
      @version.summary = nil
      assert_equal "This rubygem does not have a description or summary.", @version.info
    end

    should "create a gem spec" do
      spec = @version.to_spec
      assert spec.is_a?(Gem::Specification)
      assert_equal @version.rubygem.name, spec.name
      assert_equal @version.number, spec.version.to_s
      assert_equal [@version.authors], spec.authors
      assert_equal @version.description, spec.description
      assert_equal @version.summary, spec.summary

      date = @version.built_at
      assert_equal Time.local(date.year, date.month, date.day), spec.date
    end

    should "join multiple authors on gemspecs" do
      @version.authors = "<NAME>, <NAME>, <NAME>"
      assert_equal ["<NAME>", "<NAME>", "<NAME>"], @version.to_spec.authors
    end

    should "create gemspec with some dependencies" do
      @dep_one = Factory(:dependency, :version => @version, :requirements => ">= 0, = 1.2.3")
      @dep_two = Factory(:dependency, :version => @version, :requirements => "= 3.0.0")
      spec = @version.to_spec

      @spec_dep_one = spec.dependencies.detect { |d| d.name == @dep_one.rubygem.name }
      @spec_dep_two = spec.dependencies.detect { |d| d.name == @dep_two.rubygem.name }

      assert_equal 2, spec.dependencies.size
      assert @spec_dep_one
      assert @spec_dep_two

      assert_equal @dep_one.requirements.split(", "), @spec_dep_one.requirements_list
      assert_equal @dep_two.requirements.split(", "), @spec_dep_two.requirements_list
    end
  end

  context "when indexing" do
    setup do
      @rubygem = Factory(:rubygem)
      @first_version  = Factory(:version, :rubygem => @rubygem, :number => "0.0.1", :built_at => 7.days.ago)
      @second_version = Factory(:version, :rubygem => @rubygem, :number => "0.0.2", :built_at => 6.days.ago)
      @third_version  = Factory(:version, :rubygem => @rubygem, :number => "0.0.3", :built_at => 5.days.ago)
      @fourth_version = Factory(:version, :rubygem => @rubygem, :number => "0.0.4", :built_at => 5.days.ago)
    end

    should "always sort properly" do
{'fraction_non_alphanumeric': 0.12097971301335972, 'fraction_numerical': 0.010390895596239486, 'mean_word_length': 2.9207888781118654, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2852501', 'n_tokens_mistral': 3872, 'n_tokens_neox': 3726, 'n_words': 944}