<commit_before>require 'test_helper'

class BitmaskAttributesTest < ActiveSupport::TestCase

  def self.context_with_classes(label,campaign_class,company_class)
    context label do
      setup do
        @campaign_class = campaign_class
        @company_class = company_class
      end

      teardown do
        @company_class.destroy_all
        @campaign_class.destroy_all
      end

      should "return all defined values of a given bitmask attribute" do
        assert_equal @campaign_class.values_for_medium, [:web, :print, :email, :phone]
      end

      should "can assign single value to bitmask" do
        assert_stored @campaign_class.new(:medium => :web), :web
      end

      should "can assign multiple values to bitmask" do
        assert_stored @campaign_class.new(:medium => [:web, :print]), :web, :print
      end

      should "can add single value to bitmask" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_stored campaign, :web, :print
        campaign.medium << :phone
        assert_stored campaign, :web, :print, :phone
      end

      should "ignores duplicate values added to bitmask" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_stored campaign, :web, :print
        campaign.medium << :phone
        assert_stored campaign, :web, :print, :phone
        campaign.medium << :phone
        assert_stored campaign, :web, :print, :phone
        campaign.medium << "phone"
        assert_stored campaign, :web, :print, :phone
        assert_equal 1, campaign.medium.select { |value| value == :phone }.size
        assert_equal 0, campaign.medium.select { |value| value == "phone" }.size
      end

      should "can assign new values at once to bitmask" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_stored campaign, :web, :print
        campaign.medium = [:phone, :email]
        assert_stored campaign, :phone, :email
      end

      should "can assign raw bitmask values" do
        campaign = @campaign_class.new
        campaign.medium_bitmask = 3
        assert_stored campaign, :web, :print
        campaign.medium_bitmask = 0
        assert_empty campaign.medium
      end

      should "can save bitmask to db and retrieve values transparently" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_stored campaign, :web, :print
        assert campaign.save
        assert_stored @campaign_class.find(campaign.id), :web, :print
      end

      should "can add custom behavor to value proxies during bitmask definition" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_raises NoMethodError do
          campaign.medium.worked?
        end
        assert_nothing_raised do
          campaign.misc.worked?
        end
        assert campaign.misc.worked?
      end

      should "cannot use unsupported values" do
        assert_unsupported { @campaign_class.new(:medium => [:web, :print, :this_will_fail]) }
        campaign = @campaign_class.new(:medium => :web)
        assert_unsupported { campaign.medium << :this_will_fail_also }
        assert_unsupported { campaign.medium = [:so_will_this] }
      end

      should "can determine bitmasks using convenience method" do
        assert @campaign_class.bitmask_for_medium(:web, :print)
        assert_equal(
          @campaign_class.bitmasks[:medium][:web] | @campaign_class.bitmasks[:medium][:print],
          @campaign_class.bitmask_for_medium(:web, :print)
        )
      end

      should "assert use of unknown value in convenience method will result in exception" do
        assert_unsupported { @campaign_class.bitmask_for_medium(:web, :and_this_isnt_valid)  }
      end

      should "hash of values is with indifferent access" do
        string_bit = nil
        assert_nothing_raised do
          assert (string_bit = @campaign_class.bitmask_for_medium('web', 'print'))
        end
        assert_equal @campaign_class.bitmask_for_medium(:web, :print), string_bit
      end

      should "save bitmask with non-standard attribute names" do
        campaign = @campaign_class.new(:Legacy => [:upper, :case])
        assert campaign.save<commit_msg>Ensure unsupported values for raw bitmasks raise an exception<commit_after>require 'test_helper'

class BitmaskAttributesTest < ActiveSupport::TestCase

  def self.context_with_classes(label,campaign_class,company_class)
    context label do
      setup do
        @campaign_class = campaign_class
        @company_class = company_class
      end

      teardown do
        @company_class.destroy_all
        @campaign_class.destroy_all
      end

      should "return all defined values of a given bitmask attribute" do
        assert_equal @campaign_class.values_for_medium, [:web, :print, :email, :phone]
      end

      should "can assign single value to bitmask" do
        assert_stored @campaign_class.new(:medium => :web), :web
      end

      should "can assign multiple values to bitmask" do
        assert_stored @campaign_class.new(:medium => [:web, :print]), :web, :print
      end

      should "can add single value to bitmask" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_stored campaign, :web, :print
        campaign.medium << :phone
        assert_stored campaign, :web, :print, :phone
      end

      should "ignores duplicate values added to bitmask" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_stored campaign, :web, :print
        campaign.medium << :phone
        assert_stored campaign, :web, :print, :phone
        campaign.medium << :phone
        assert_stored campaign, :web, :print, :phone
        campaign.medium << "phone"
        assert_stored campaign, :web, :print, :phone
        assert_equal 1, campaign.medium.select { |value| value == :phone }.size
        assert_equal 0, campaign.medium.select { |value| value == "phone" }.size
      end

      should "can assign new values at once to bitmask" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_stored campaign, :web, :print
        campaign.medium = [:phone, :email]
        assert_stored campaign, :phone, :email
      end

      should "can assign raw bitmask values" do
        campaign = @campaign_class.new
        campaign.medium_bitmask = 3
        assert_stored campaign, :web, :print
        campaign.medium_bitmask = 0
        assert_empty campaign.medium
      end

      should "can save bitmask to db and retrieve values transparently" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_stored campaign, :web, :print
        assert campaign.save
        assert_stored @campaign_class.find(campaign.id), :web, :print
      end

      should "can add custom behavor to value proxies during bitmask definition" do
        campaign = @campaign_class.new(:medium => [:web, :print])
        assert_raises NoMethodError do
          campaign.medium.worked?
        end
        assert_nothing_raised do
          campaign.misc.worked?
        end
        assert campaign.misc.worked?
      end

      should "cannot use unsupported values" do
        assert_unsupported { @campaign_class.new(:medium => [:web, :print, :this_will_fail]) }
        campaign = @campaign_class.new(:medium => :web)
        assert_unsupported { campaign.medium << :this_will_fail_also }
        assert_unsupported { campaign.medium = [:so_will_this] }
      end

      should "cannot use unsupported values for raw bitmask values" do
        campaign = @campaign_class.new(:medium => :web)
        assert_unsupported { campaign.medium_bitmask = :this_will_fail }
      end

      should "can determine bitmasks using convenience method" do
        assert @campaign_class.bitmask_for_medium(:web, :print)
        assert_equal(
          @campaign_class.bitmasks[:medium][:web] | @campaign_class.bitmasks[:medium][:print],
          @campaign_class.bitmask_for_medium(:web, :print)
        )
      end

      should "assert use of unknown value in convenience method will result in exception" do
        assert_unsupported { @campaign_class.bitmask_for_medium(:web, :and_this_isnt_valid)  }
      end

      should "hash of values is with indifferent access" do
        string_bit = nil
        assert_nothing_raised do
          assert (string_bit = @campaign_class.bitmask_for_medium('web', 'print'))
        end
        assert_equal @campaign_class.bitmask_for_medium(:web, :print), string_bit
      end

      should "save bitmask with non-standard attribute names" do
        campaign = @campaign_class.new(:Legacy => [:upper, :case])
        assert campaign.save
{'fraction_non_alphanumeric': 0.0915032679738562, 'fraction_numerical': 0.0009173259947253755, 'mean_word_length': 2.7790294627383014, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, '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': '3722840', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2405, 'n_words': 701}