-
-
Save nelsnelson/1955486 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' | |
DB = Sequel.connect('jdbc:postgresql://localhost:5432/test?user=test&password=test') | |
DB.create_table? :test do | |
primary_key :id | |
foreign_key :parent_id, :test, :on_delete => :set_null | |
String :name | |
String :object_type, :null => false | |
index :name | |
end | |
Sequel::Model.plugin :identity_map | |
class Test < Sequel::Model(:test) | |
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 | |
t0 = Thread.new do | |
a = Test.find_or_create(:name => 'A') | |
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 << "Hi!" | |
puts "" | |
puts "b (should be 'B') # => #{b}" | |
puts "b.non_persistent_stuff # => [#{b.non_persistent_stuff}]" | |
puts "b.non_persistent_stuff.include? \"Hi!\" # => #{b.non_persistent_stuff.include? "Hi!"}" | |
end | |
t0.join | |
puts "" | |
t1 = Thread.new do | |
Test.with_identity_map do | |
a = Test.find(:name => 'A') | |
b = Test.find(:name => 'B') | |
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 (should be true) # => #{b.object_id == y.object_id}" | |
puts "" | |
puts "y.non_persistent_stuff # => [#{y.non_persistent_stuff}]" | |
puts "y.non_persistent_stuff.include? \"Hi!\" # => #{y.non_persistent_stuff.include? "Hi!"}" | |
end | |
end | |
t1.join | |
DB.drop_table :test if DB.table_exists? :test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment