<commit_before>require 'helper'

class FakedModel
  attr_reader :id
  def self.slug_scope_key; "faked_models"; end
  def initialize(id); @id = id; end
end

class SlugHistoryTest < Test::Unit::TestCase
  with_tables :slugs, :users do
    
    context 'for arbitrary models' do
    
      setup do
        @record_a = FakedModel.new(12)
        @record_b = FakedModel.new(4)
        Pseudocephalopod.record_slug(@record_a, "awesome")
        Pseudocephalopod.record_slug(@record_b, "awesome-1")
        Pseudocephalopod.record_slug(@record_a, "ninjas")
      end
    
      should 'let you lookup a given record id easily' do
        assert Pseudocephalopod.last_known_slug_id(FakedModel, "felafel").blank?
        assert Pseudocephalopod.last_known_slug_id(FakedModel, "ninjas-2").blank?
        assert_equal 12, Pseudocephalopod.last_known_slug_id(FakedModel, "awesome")
        assert_equal 4,  Pseudocephalopod.last_known_slug_id(FakedModel, "awesome-1")
        assert_equal 12, Pseudocephalopod.last_known_slug_id(FakedModel, "ninjas")
      end
      
      should 'let you return slug history for a given record'
    
    end
    
    context 'on a specific record' do
      
      should 'by default record slug history' do
        setup_slugs!
        user = User.create :name => "Bob"
        assert_equal [], user.previous_slugs
        user.update_attributes! :name => "Sal"
        user.update_attributes! :name => "Red"
        user.update_attributes! :name => "Jim"
        assert_same_as_slug user, "red"
        assert_same_as_slug user, "sal"
        assert_same_as_slug user, "bob"
        assert_same_as_slug user, "jim"  
      end
      
      should 'let you reset history for a slug' do
        setup_slugs!
        user = User.create :name => "Bob"
        user.update_attributes! :name => "Sal"
        user.update_attributes! :name => "Red"
        user.update_attributes! :name => "Jim"
        assert_equal ["red", "sal", "bob"], user.previous_slugs
        user.remove_slug_history!
        assert_equal [], user.previous_slugs
        assert_none_for_slug "red"
        assert_none_for_slug "sal"
        assert_none_for_slug "bob"
      end

      should 'let you disable recording of slug history' do
        setup_slugs! :history => false
        user = User.create(:name => "Bob")
        assert !user.respond_to?(:previous_slugs)
        user.update_attributes! :name => "Red"
        assert_same_as_slug      user, "red"
        assert_different_to_slug user, "bob"
        assert_none_for_slug "bob"
      end
      
    end
    
  end
  
end<commit_msg>Add failing test for the slug history callback<commit_after>require 'helper'

class FakedModel
  attr_reader :id
  def self.slug_scope_key; "faked_models"; end
  def initialize(id); @id = id; end
end

class SlugHistoryTest < Test::Unit::TestCase
  with_tables :slugs, :users do
    
    context 'for arbitrary models' do
    
      setup do
        @record_a = FakedModel.new(12)
        @record_b = FakedModel.new(4)
        Pseudocephalopod.record_slug(@record_a, "awesome")
        Pseudocephalopod.record_slug(@record_b, "awesome-1")
        Pseudocephalopod.record_slug(@record_a, "ninjas")
      end
    
      should 'let you lookup a given record id easily' do
        assert Pseudocephalopod.last_known_slug_id(FakedModel, "felafel").blank?
        assert Pseudocephalopod.last_known_slug_id(FakedModel, "ninjas-2").blank?
        assert_equal 12, Pseudocephalopod.last_known_slug_id(FakedModel, "awesome")
        assert_equal 4,  Pseudocephalopod.last_known_slug_id(FakedModel, "awesome-1")
        assert_equal 12, Pseudocephalopod.last_known_slug_id(FakedModel, "ninjas")
      end
      
      should 'let you return slug history for a given record'
    
    end
    
    context 'on a specific record' do
      
      should 'by default record slug history' do
        setup_slugs!
        user = User.create :name => "Bob"
        assert_equal [], user.previous_slugs
        user.update_attributes! :name => "Sal"
        user.update_attributes! :name => "Red"
        user.update_attributes! :name => "Jim"
        assert_same_as_slug user, "red"
        assert_same_as_slug user, "sal"
        assert_same_as_slug user, "bob"
        assert_same_as_slug user, "jim"  
      end
      
      should 'let you reset history for a slug' do
        setup_slugs!
        user = User.create :name => "Bob"
        user.update_attributes! :name => "Sal"
        user.update_attributes! :name => "Red"
        user.update_attributes! :name => "Jim"
        assert_equal ["red", "sal", "bob"], user.previous_slugs
        user.remove_slug_history!
        assert_equal [], user.previous_slugs
        assert_none_for_slug "red"
        assert_none_for_slug "sal"
        assert_none_for_slug "bob"
      end

      should 'let you disable recording of slug history' do
        setup_slugs! :history => false
        user = User.create(:name => "Bob")
        assert !user.respond_to?(:previous_slugs)
        user.update_attributes! :name => "Red"
        assert_same_as_slug      user, "red"
        assert_different_to_slug user, "bob"
        assert_none_for_slug "bob"
      end
      
      should 'remove slug history for a record by default on destroy' do
        setup_slugs!
        user = User.create :name => "Bob"
        user.update_attributes! :name => "Sal"
        user.update_attributes! :name => "Red"
        user.update_attributes! :name => "Jim"
        assert_equal ["red", "sal", "bob"], user.previous_slugs
        user.destroy
        assert_equal [], user.previous_slugs
      end
      
    end
    
  end
  
end
{'fraction_non_alphanumeric': 0.09434296861145593, 'fraction_numerical': 0.0039014009576165986, 'mean_word_length': 2.613068545803972, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '326709', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1823, 'n_words': 427}