spec/objects_spec.rb in knjrbfw-0.0.68 vs spec/objects_spec.rb in knjrbfw-0.0.69
- old
+ new
@@ -172,10 +172,20 @@
{"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
{"name" => "name", "type" => "varchar"}
]
})
+ $db.tables.create("Timelog", {
+ "columns" => [
+ {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
+ {"name" => "person_id", "type" => "int"}
+ ],
+ "indexes" => [
+ "person_id"
+ ]
+ })
+
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"]
@@ -210,15 +220,23 @@
end
class Person < Knj::Datarow
has_one [:Project]
+ has_many [
+ {:class => :Timelog, :autozero => true}
+ ]
+
def html
return self[:name]
end
end
+ class Timelog < Knj::Datarow
+
+ end
+
$ob = Knj::Objects.new(:db => $db, :datarow => true, :require => false)
$ob.add(:Person, {
:name => "Kasper"
})
@@ -330,9 +348,30 @@
raise "Expected count to be #{pers_length} but it wasnt: #{count}" if count != pers_length
persons = $ob.list(:Person).to_a
raise "Expected persons count to be 0 but it wasnt: #{persons.map{|e| e.data} }" if persons.length > 0
+ end
+
+ it "should do autozero when deleting objects" do
+ person1 = $ob.add(:Person, {
+ :name => "Kasper"
+ })
+ person2 = $ob.add(:Person, {
+ :name => "Charlotte"
+ })
+
+ timelog1 = $ob.add(:Timelog, {
+ :person_id => person1.id
+ })
+ timelog2 = $ob.add(:Timelog, {
+ :person_id => person2.id
+ })
+
+ $ob.delete(person1)
+
+ raise "Expected timelog1's person-ID to be zero but it wasnt: '#{timelog1[:person_id]}'." if timelog1[:person_id].to_i != 0
+ raise "Expected timelog2's person-ID to be #{person2.id} but it wasnt: '#{timelog2[:person_id]}'." if timelog2[:person_id].to_i != person2.id.to_i
end
it "should delete the temp database again." do
db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
File.unlink(db_path) if File.exists?(db_path)
\ No newline at end of file