Created
March 1, 2012 03:46
-
-
Save nelsnelson/1947131 to your computer and use it in GitHub Desktop.
Testing non-persistent fields on persisted identity-mapped entity instances in JRuby and Sequel through jdbc-postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env jruby | |
require 'rubygems' | |
require 'sequel' | |
gem 'jdbc-postgres' | |
@connection = Sequel.connect('jdbc:postgresql://localhost:5432/test?user=test&password=test') | |
Sequel::Model.db = @connection | |
Sequel::Model.plugin :identity_map | |
module Sequel | |
class Model | |
module ClassMethods | |
def implicit_table_name | |
underscore(demodulize(name)).to_sym | |
end | |
end | |
end | |
end | |
Sequel::Model.db.create_table! :test do | |
primary_key :id | |
foreign_key :parent_id, :test, :on_delete => :set_null | |
index :name | |
String :name | |
String :object_type, :null => false | |
end unless Sequel::Model.db.table_exists? :test | |
class Test < Sequel::Model | |
plugin :identity_map | |
plugin :rcte_tree | |
plugin :single_table_inheritance, :object_type | |
def non_persistent_stuff | |
@non_persistent_stuff ||= [] | |
end | |
def to_s | |
"#{name} <object_id:#{object_id},entity_id:#{id}>" | |
end | |
end | |
a = Test.find_or_create(:name => 'A') | |
b = nil | |
c = nil | |
t0 = Thread.new do | |
b = Test.find_or_create(:name => 'B') | |
c = Test.find_or_create(:name => 'C') | |
a.add_child b | |
puts "a.children.include? b # => #{a.children.include? b}" | |
b.non_persistent_stuff << c | |
puts "" | |
puts "b (should be 'B') # => #{b}" | |
puts "b.non_persistent_stuff # => [#{b.non_persistent_stuff}]" | |
puts "b.non_persistent_stuff.include? c # => #{b.non_persistent_stuff.include? c}" | |
end | |
t0.join | |
puts "" | |
t1 = Thread.new do | |
x = Test.find(:name => 'A') | |
y = x.children.first | |
z = Test.find(:name => 'C') | |
puts "x (should be 'A') # => #{x}" | |
puts "z (should be 'C') # => #{z}" | |
puts "x.children.include? b # => #{a.children.include? b}" | |
puts "" | |
puts "b (should be 'B') # => #{b}" | |
puts "y (should be 'B') # => #{y}" | |
puts "" | |
puts "b == y (should be true) # => #{b == y}" | |
puts "b.object_id == y.object_id (shouldn't this be true? perhaps I don't understand what an identity map does) # => #{b.object_id == y.object_id}" | |
puts "" | |
puts "y.non_persistent_stuff # => [#{y.non_persistent_stuff}]" | |
puts "y.non_persistent_stuff.include? z # => #{y.non_persistent_stuff.include? z}" | |
end | |
t1.join | |
Sequel::Model.db.drop_table :test if Sequel::Model.db.table_exists? :test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment