spec/model_handler_spec.rb in baza-0.0.16 vs spec/model_handler_spec.rb in baza-0.0.17

- old
+ new

@@ -1,16 +1,10 @@ require "spec_helper" describe "Objects" do - it "should be able to cache rows" do - begin - require "#{File.dirname(__FILE__)}/../../array_enumerator/lib/array_enumerator" - rescue LoadError - require "array_enumerator" - end - - require "sqlite3" if RUBY_ENGINE != "jruby" + before(:all) do + require "sqlite3" unless RUBY_ENGINE == "jruby" require "tmpdir" $db_path = "#{Dir.tmpdir}/knjrbfw_objects_cache_test.sqlite3" File.unlink($db_path) if File.exists?($db_path) $db = Baza::Db.new(:type => :sqlite3, :path => $db_path, :return_keys => "symbols", :debug => false) @@ -53,10 +47,40 @@ {:username => "User 3"}, {:username => "User 4"}, {:username => "User 5"} ]) + class Project < Baza::Model + has_many [ + {:class => :Task, :col => :project_id, :depends => true} + ] + end + + class Task < Baza::Model + has_one [ + {:class => :Person, :required => true}, + :Project + ] + end + + class Person < Baza::Model + has_one [:Project] + + has_many [ + {:class => :Timelog, :autozero => true} + ] + + def html + return self[:name] + end + end + + class Timelog < Baza::Model + end + end + + it "should be able to cache rows" do raise "Expected user-ID-cache to be 5 but it wasnt: #{$ob.ids_cache[:User].length}" if $ob.ids_cache[:User].length != 5 user = $ob.get(:User, 4) raise "No user returned." if !user $ob.delete(user) @@ -81,11 +105,13 @@ users = $ob.list(:User).to_a #Stress it to test threadsafety... threads = [] 0.upto(5) do |tc| - threads << Knj::Thread.new do + threads << Thread.new do + Thread.current.abort_on_exception = true + 0.upto(5) do |ic| user = $ob.add(:User, {:username => "User #{tc}-#{ic}"}) raise "No user returned." if !user $ob.delete(user) @@ -121,11 +147,13 @@ :require => false ) threads = [] 0.upto(5) do - threads << Knj::Thread.new do + threads << Thread.new do + Thread.current.abort_on_exception = true + 0.upto(5) do ret = $ob2.add(:Group, {:groupname => "User 1"}, {:skip_ret => true}) raise "Expected empty return but got something: #{ret}" if ret end end @@ -140,11 +168,13 @@ File.unlink($db_path) if File.exists?($db_path) end #Moved from "knjrbfw_spec.rb" it "should be able to generate a sample SQLite database and add a sample table, with sample columns and with a sample index to it" do - $db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3" + require 'tmpdir' + + $db_path = "#{Dir.tmpdir}/knjrbfw_test_sqlite3.sqlite3" $db = Baza::Db.new( :type => :sqlite3, :path => $db_path, :index_append_table_name => true ) @@ -190,11 +220,11 @@ }) table = $db.tables[:Project] indexes = table.indexes - raise "Could not find the sample-index 'category_id' that should have been created." if !indexes[:Project__category_id] + raise "Could not find the sample-index 'category_id' that should have been created." unless indexes[:Project__category_id] #If we insert a row the ID should increase and the name should be the same as inserted (or something is very very wrong)... $db.insert("Project", { "name" => "Test project" @@ -209,39 +239,10 @@ raise "Expected count of 1 but it wasnt: #{count}" if count != 1 end it "should be able to automatic generate methods on datarow-classes (has_many, has_one)." do - class Project < Baza::Model - has_many [ - {:class => :Task, :col => :project_id, :depends => true} - ] - end - - class Task < Baza::Model - has_one [ - {:class => :Person, :required => true}, - :Project - ] - end - - class Person < Baza::Model - has_one [:Project] - - has_many [ - {:class => :Timelog, :autozero => true} - ] - - def html - return self[:name] - end - end - - class Timelog < Baza::Model - - end - $ob = Baza::ModelHandler.new(:db => $db, :datarow => true, :require => false) $ob.add(:Person, { :name => "Kasper" }) @@ -310,17 +311,13 @@ #this should happen - it should not possible to delete project 1 because task 1 depends on it." end end it "should be able to generate lists for inputs" do - Knj::Web.inputs([{ - :title => "Test 3", - :name => :seltest3, - :type => :select, - :default => 1, - :opts => $ob.list_optshash(:Task) - }]) + list = $ob.list_optshash(:Task) + list.length.should eq 1 + list[1].should eq 'Test task' end it "should be able to connect to objects 'no-html' callback and test it." do task = $ob.get(:Task, 1) $ob.events.connect(:no_html) do |event, classname| @@ -406,9 +403,10 @@ raise "Failure ID was the same" if person3.id == person2.id person3[:name].should eql("get_or_add3") end it "should delete the temp database again." do - db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3" + require 'tmpdir' + db_path = "#{Dir.tmpdir}/knjrbfw_test_sqlite3.sqlite3" File.unlink(db_path) if File.exists?(db_path) end end \ No newline at end of file