Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first working test

  • Loading branch information...
commit 0fe7985f12db64a35f43f5d314086f685344da6d 1 parent af6d9c9
Lin Jen-Shin (godfat) godfat authored
2  Rakefile
View
@@ -11,6 +11,8 @@ proj = 'cerealize'
require "#{proj}/version"
Bones{
+ ruby_opts [''] # silence warning for now
+
version Cerealize::VERSION
depend_on 'activerecord', :version => '>=2.3.5'
1  init.rb
View
@@ -1 +1,2 @@
+# encoding: utf-8
require 'cerealize'
39 lib/cerealize/marshalize.rb → lib/cerealize.rb
View
@@ -1,12 +1,15 @@
# encoding: utf-8
-module Marshalize
-
+require 'active_record'
+autoload :YAML, 'yaml'
+
+module Cerealize
+
#
- # Dirty functionality: note that *_changed? and changed? work,
+ # Dirty functionality: note that *_changed? and changed? work,
# but *_was, *_change, and changes do NOT work.
#
-
+
def self.included(base)
base.send :extend, ClassMethods
end
@@ -23,7 +26,7 @@ def self.yaml_to(obj)
def self.yaml_from(s)
obj && YAML.load(obj)
end
-
+
def self.encode(obj, encoding)
case encoding
when :marshal; marshal_to(obj)
@@ -38,9 +41,9 @@ def self.decode(s)
else raise "Unknown field format for '#{s}'"
end
end
-
+
module ClassMethods
-
+
def marshalize property, klass=nil, options={}
field_pre = "@#{property}_pre".to_sym
field_cache = "@#{property}".to_sym
@@ -56,21 +59,21 @@ def marshalize property, klass=nil, options={}
# Rails.logger.debug "#{property} (READER)"
# See if no assignment yet
- if !instance_variable_defined?(field_cache)
-
+ if !instance_variable_defined?(field_cache)
+
# Save property if not already saved
if !instance_variable_defined?(field_pre)
instance_variable_set(field_pre, read_attribute(property))
end
# Set cached from pre
- v = Marshalize.decode(instance_variable_get(field_pre))
+ v = Cerealize.decode(instance_variable_get(field_pre))
raise ActiveRecord::SerializationTypeMismatch, "expected #{klass}, got #{v.class}" \
if klass && !v.nil? && !v.kind_of?(klass)
instance_variable_set(field_cache, v)
end
-
- # Return cached
+
+ # Return cached
instance_variable_get(field_cache)
end
@@ -82,23 +85,23 @@ def marshalize property, klass=nil, options={}
instance_variable_set(field_cache, v)
end
- # Callback for before_save
+ # Callback for before_save
#
define_method "#{property}_update_if_dirty" do
# Rails.logger.debug "#{property}_update_if_dirty"
-
+
# See if we have a new cur value
if instance_variable_defined?(field_cache)
v = instance_variable_get(field_cache)
- v_enc = Marshalize.encode(v, options[:encoding] || :marshal)
+ v_enc = Cerealize.encode(v, options[:encoding] || :marshal)
- # See if no pre at all (i.e. it was written to before being read),
+ # See if no pre at all (i.e. it was written to before being read),
# or if different. When comparing, compare both marshalized string,
# and Object ==.
#
if !instance_variable_defined?(field_pre) ||
- (v_enc != instance_variable_get(field_pre) &&
- v != Marshalize.decode(instance_variable_get(field_pre)))
+ (v_enc != instance_variable_get(field_pre) &&
+ v != Cerealize.decode(instance_variable_get(field_pre)))
write_attribute(property, v_enc)
end
end
121 test/marshalize_test.rb → test/test_basic.rb
View
@@ -1,18 +1,16 @@
-require 'test_helper'
+# encoding: utf-8
-
-def debug(*args)
- Rails.logger.debug(*args)
-end
+require 'cerealize'
+require 'test/unit'
class Person
def initialize(name, age=nil)
self.name = name
self.age = age
end
-
+
ATTR = [ :name, :age, :hat, :pocket ]
- attr_accessor *ATTR
+ attr_accessor(*ATTR)
def ==(other)
ATTR.all?{|m| send(m) == other.send(m) }
@@ -29,44 +27,51 @@ def initialize(color); self.color = color; end
class Blob; end
-ActiveRecordTester.create_table :boats, :force => true do |t|
+ActiveRecord::Base.establish_connection(
+ :adapter => 'sqlite3',
+ :database => ':memory:'
+)
+
+ActiveRecord::Base.connection.create_table :boats, :force => true do |t|
t.string :name
t.integer :tonnage
t.string :captain
t.string :cargo
end
+
class Boat < ActiveRecord::Base
- include Marshalize
+ include Cerealize
marshalize :captain
marshalize :cargo, Blob
end
-class MarshalizeTest < ActiveSupport::TestCase
+class MarshalizeTest < Test::Unit::TestCase
def setup
Boat.delete_all
end
+
def teardown
end
-
+
def set_encoding(encoding)
Boat.send :marshalize, :captain, nil, :encoding => encoding
end
- test "encoding yaml" do
+ def test_encoding_yaml
set_encoding(:yaml)
Boat.create :name => 'yamato', :captain => Person.new('kosaku')
s = Boat.connection.select_value("SELECT captain FROM boats WHERE name='yamato';")
- assert s[0..2] = '---'
+ assert s[0..2] = '---'
end
-
- test "encoding marshal" do
+
+ def test_encoding_marshal
set_encoding(:marshal)
Boat.create :name => 'santa maria', :captain => Person.new('columbus')
s = Boat.connection.select_value("SELECT captain FROM boats WHERE name='santa maria';")
- assert s[0..1] = 'BA'
+ assert s[0..1] = 'BA'
end
-
- test 'class checking' do
+
+ def test_class_checking
Boat.create :name => 'lollypop', :cargo => Blob.new
Boat.find_by_name 'lollypop'
@@ -75,36 +80,32 @@ def set_encoding(encoding)
Boat.find_by_name('minerva').cargo
end
end
-
+
[ :yaml, :marshal ].each do |encoding|
- test "#{encoding} basic" do
+ define_method "test_#{encoding}_basic" do
set_encoding(encoding)
-
- debug '(0)'
+
b = Boat.create(:name => 'pequod')
-
- debug '(1)'
+
b.captain = Person.new('ahab')
b.save!
-
- debug "(2)"
+
b = Boat.find_by_name('pequod')
assert_equal Person, b.captain.class
assert_equal 'ahab', b.captain.name
-
+
b.captain = Person.new('ishmael') # make sure not frozen!
assert_equal 'ishmael', b.captain.name
b.captain.name = 'call me ishmael'
b.save!
-
- debug '(3)'
+
b = Boat.find_by_name('pequod')
assert_equal 'call me ishmael', b.captain.name
end
['string', {:key1 => 'v', :key2 => [3,4]}].each do |captain|
- test "#{encoding} for #{captain} changed?" do
+ define_method "test_#{encoding}_for_#{captain}_changed?" do
b = Boat.create(:captain => captain)
assert !b.changed?
b.captain = b.captain
@@ -121,23 +122,23 @@ def set_encoding(encoding)
end
end
- [ :none, :read, :read_write, :write ].each do |action|
+ [ :none, :read, :read_write, :write ].each do |action|
- test "#{encoding} partial update #{action}" do
+ define_method "test_#{encoding}_partial_update_#{action}" do
set_encoding(encoding)
Boat.delete_all
-
+
b1 = Boat.create :name => 'titanic', :tonnage => 1000
b1.captain = Person.new 'smith'
b1.save!
b1 = Boat.find_by_name 'titanic'
b1.tonnage += 1000
- if action.included_in? [:read, :read_write]
+ if [:read, :read_write].include?(action)
b1.captain
end
- if action.included_in? [:read_write, :write]
+ if [:read_write, :write].include?(action)
b1.captain = Person.new 'dawson'
end
@@ -148,80 +149,82 @@ def set_encoding(encoding)
b1.save! # Potentially overwriting save!
b3 = Boat.find_by_name 'titanic'
- if action.included_in? [:read_write, :write]
+ if [:read_write, :write].include?(action)
assert_equal 'dawson', b3.captain.name
elsif action == :none
assert_equal 'rose', b3.captain.name
elsif action == :read
- assert b3.captain.name.included_in?(%w{ rose dawson }),
+ assert %w{ rose dawson }.include?(b3.captain.name),
"captain.name is #{b3.captain.name}"
end
end
end
- test "#{encoding} just save" do
+ define_method "test_#{encoding}_just_save" do
set_encoding(encoding)
b1 = Boat.new :name => 'alinghi'
b1.save!
assert_equal nil, Boat.find_by_name('alinghi').captain
end
- test "#{encoding} just save writing" do
+
+ define_method "test_#{encoding}_just_save_writing" do
set_encoding(encoding)
b1 = Boat.new :name => 'surprise'
b1.captain = Person.new 'aubrey'
b1.save!
assert_equal 'aubrey', Boat.find_by_name('surprise').captain.name
end
- test "#{encoding} just reading" do
+
+ define_method "test_#{encoding}_just_reading" do
set_encoding(encoding)
b1 = Boat.create :name => 'black pearl', :captain => Person.new('sparrow')
b2 = Boat.find_by_name 'black pearl'
assert_equal 'sparrow', b2.captain.name
end
-
- test "#{encoding} deep change" do
+
+ define_method "test_#{encoding}_deep_change" do
set_encoding(encoding)
b1 = Boat.create :name => 'bounty', :captain => (Person.new('bligh'))
-
+
b2 = Boat.find_by_name 'bounty'
b2.captain.hat = Hat.new('blue')
b2.save!
-
+
assert_equal 'blue', Boat.find_by_name('bounty').captain.hat.color
end
- test "#{encoding} deeper change object" do
+ define_method "test_#{encoding}_deeper_change_object" do
set_encoding(encoding)
b1 = Boat.create :name => 'bounty', :captain => (Person.new('bligh'))
-
+
b2 = Boat.find_by_name 'bounty'
b2.captain.pocket = [ :pounds ]
b2.save!
-
+
b2 = Boat.find_by_name 'bounty'
b2.captain.pocket << :francs
b2.save!
-
+
assert_equal [ :pounds, :francs ], Boat.find_by_name('bounty').captain.pocket
end
- test "#{encoding} deeper change hash" do
+ define_method "test_#{encoding}_deeper_change_hash" do
set_encoding(encoding)
b1 = Boat.create :name => 'bounty', :captain => { :name => :bligh }
-
+
b2 = Boat.find_by_name 'bounty'
b2.captain[:pocket] = [ :pounds ]
b2.save!
-
+
b2 = Boat.find_by_name 'bounty'
b2.captain[:pocket] << :francs
b2.save!
-
+
assert_equal [ :pounds, :francs ], Boat.find_by_name('bounty').captain[:pocket]
end
end
- test 'simple hash' do
+ def test_simple_hash
b = Boat.new
h = { :name => 'skipper' }
b.captain = h
@@ -232,7 +235,7 @@ def set_encoding(encoding)
assert_equal 'alan hale', b.captain[:actor]
end
- test 'check if really saved' do
+ def test_check_if_really_saved
b = Boat.new
b.captain = { :name => 'ramius' }
b.save
@@ -246,22 +249,22 @@ def set_encoding(encoding)
assert_equal 'russian', b3.captain[:nationality]
end
- test 'array marshalize' do
+ def test_array_marshalize
b = Boat.new
b.captain = [123, 456]
b.save
assert_equal [123, 456], b.captain
assert_equal [123, 456], Boat.find(b.id).captain
end
-
- test 'repeated saves' do
+
+ def test_repeated_saves
b = Boat.new
b.captain = { :name => 'kirk', :age => 23 }
b.save
-
+
assert_equal 23, b.captain[:age]
assert_equal 23, Boat.first.captain[:age]
-
+
b.captain[:age] += 1
b.save
assert_equal 24, b.captain[:age]
Please sign in to comment.
Something went wrong with that request. Please try again.