<commit_before>require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")

begin
require 'yaml'
require 'json'
rescue LoadError => e
  skip_warn "serialization plugin: can't load json (#{e.class}: #{e})"
else
describe "Serialization plugin" do
  before do
    @c = Class.new(Sequel::Model(:items)) do
      no_primary_key
      columns :id, :abc, :def, :ghi
    end
    MODEL_DB.reset
  end
  
  it "should allow setting additional serializable attributes via plugin :serialization call" do
    @c.plugin :serialization, :yaml, :abc
    @c.create(:abc => 1, :def=> 2)
    MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 2|2, '--- 1\n(\.\.\.\n)?')\)/

    @c.plugin :serialization, :marshal, :def
    @c.create(:abc => 1, :def=> 1)
    MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 'BAhpBg==\n'|'BAhpBg==\n', '--- 1\n(\.\.\.\n)?')\)/
    
    @c.plugin :serialization, :json, :ghi
    @c.create(:ghi => [123])
    MODEL_DB.sqls.last.should =~ /INSERT INTO items \((ghi)\) VALUES \('\[123\]'\)/
  end

  it "should allow serializing attributes to yaml" do
    @c.plugin :serialization, :yaml, :abc
    @c.create(:abc => 1)
    @c.create(:abc => "hello")

    MODEL_DB.sqls.map{|s| s.sub("...\n", '')}.should == ["INSERT INTO items (abc) VALUES ('--- 1\n')", "INSERT INTO items (abc) VALUES ('--- hello\n')"]
  end

  it "serialized_columns should be the columns serialized" do
    @c.plugin :serialization, :yaml, :abc
    @c.serialized_columns.should == [:abc]
  end

  it "should allow serializing attributes to marshal" do
    @c.plugin :serialization, :marshal, :abc
    @c.create(:abc => 1)
    @c.create(:abc => "hello")
    x = [Marshal.dump("hello")].pack('m')

    MODEL_DB.sqls.should == [ \
      "INSERT INTO items (abc) VALUES ('BAhpBg==\n')", \
      "INSERT INTO items (abc) VALUES ('#{x}')", \
    ]
  end
  
  it "should allow serializing attributes to json" do
    @c.plugin :serialization, :json, :ghi
    @c.create(:ghi => [1])
    @c.create(:ghi => ["hello"])
    
    x = ["hello"].to_json
    MODEL_DB.sqls.should == [ \
      "INSERT INTO items (ghi) VALUES ('[1]')", \
      "INSERT INTO items (ghi) VALUES ('#{x}')", \
    ]
  end

  it "should allow serializing attributes using arbitrary callable" do
    @c.plugin :serialization, [proc{|s| s.reverse}, proc{}], :abc
    @c.create(:abc => "hello")
    MODEL_DB.sqls.should == ["INSERT INTO items (abc) VALUES ('olleh')"]
  end
  
  it "should translate values to and from yaml serialization format using accessor methods" do
    @c.set_primary_key :id
    @c.plugin :serialization, :yaml, :abc, :def
    vals = nil
    @c.dataset._fetch = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}

    o = @c.first
    o.id.should == 1
    o.abc.should == 1
    o.abc.should == 1<commit_msg>Add a spec for calling a serializer with an unregistered format symbol<commit_after>require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")

begin
require 'yaml'
require 'json'
rescue LoadError => e
  skip_warn "serialization plugin: can't load json (#{e.class}: #{e})"
else
describe "Serialization plugin" do
  before do
    @c = Class.new(Sequel::Model(:items)) do
      no_primary_key
      columns :id, :abc, :def, :ghi
    end
    MODEL_DB.reset
  end
  
  it "should allow setting additional serializable attributes via plugin :serialization call" do
    @c.plugin :serialization, :yaml, :abc
    @c.create(:abc => 1, :def=> 2)
    MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 2|2, '--- 1\n(\.\.\.\n)?')\)/

    @c.plugin :serialization, :marshal, :def
    @c.create(:abc => 1, :def=> 1)
    MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 'BAhpBg==\n'|'BAhpBg==\n', '--- 1\n(\.\.\.\n)?')\)/
    
    @c.plugin :serialization, :json, :ghi
    @c.create(:ghi => [123])
    MODEL_DB.sqls.last.should =~ /INSERT INTO items \((ghi)\) VALUES \('\[123\]'\)/
  end

  it "should allow serializing attributes to yaml" do
    @c.plugin :serialization, :yaml, :abc
    @c.create(:abc => 1)
    @c.create(:abc => "hello")

    MODEL_DB.sqls.map{|s| s.sub("...\n", '')}.should == ["INSERT INTO items (abc) VALUES ('--- 1\n')", "INSERT INTO items (abc) VALUES ('--- hello\n')"]
  end

  it "serialized_columns should be the columns serialized" do
    @c.plugin :serialization, :yaml, :abc
    @c.serialized_columns.should == [:abc]
  end

  it "should allow serializing attributes to marshal" do
    @c.plugin :serialization, :marshal, :abc
    @c.create(:abc => 1)
    @c.create(:abc => "hello")
    x = [Marshal.dump("hello")].pack('m')

    MODEL_DB.sqls.should == [ \
      "INSERT INTO items (abc) VALUES ('BAhpBg==\n')", \
      "INSERT INTO items (abc) VALUES ('#{x}')", \
    ]
  end
  
  it "should allow serializing attributes to json" do
    @c.plugin :serialization, :json, :ghi
    @c.create(:ghi => [1])
    @c.create(:ghi => ["hello"])
    
    x = ["hello"].to_json
    MODEL_DB.sqls.should == [ \
      "INSERT INTO items (ghi) VALUES ('[1]')", \
      "INSERT INTO items (ghi) VALUES ('#{x}')", \
    ]
  end

  it "should allow serializing attributes using arbitrary callable" do
    @c.plugin :serialization, [proc{|s| s.reverse}, proc{}], :abc
    @c.create(:abc => "hello")
    MODEL_DB.sqls.should == ["INSERT INTO items (abc) VALUES ('olleh')"]
  end
  
  it "should raise an error if specificing serializer as an unregistered symbol" do
    proc{@c.plugin :serialization, :foo, :abc}.should raise_error(Sequel::Error)
  end
  
  it "should translate values to and from yaml serialization format using accessor methods" do
    @c.set_primary_key :id
    @c.plugin :serialization, :yaml, :abc, :def
    vals = nil
    @c.dataset._fetch = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}

    o = @c.first
    o.id.should == 1
    o.abc.should == 1
    o.abc.should == 1
{'fraction_non_alphanumeric': 0.1971737323358271, 'fraction_numerical': 0.008645054031587697, 'mean_word_length': 2.963109354413702, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '5200852', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2158, 'n_words': 581}