test/functional/schema.rb in mosql-0.2.0 vs test/functional/schema.rb in mosql-0.3.0
- old
+ new
@@ -99,6 +99,48 @@
o = {'_id' => BSON::ObjectId.new, 'var' => 0}
row = @map.transform('db.collection', o)
table.insert(row)
assert_equal(o['_id'].to_s, table.select.first[:_id])
end
+
+ describe 'special fields' do
+ SPECIAL_MAP = <<EOF
+---
+db:
+ collection:
+ :meta:
+ :table: special
+ :columns:
+ - _id: TEXT
+ - mosql_updated:
+ :source: $timestamp
+ :type: timestamp
+EOF
+
+ before do
+ @specialmap = MoSQL::Schema.new(YAML.load(SPECIAL_MAP))
+
+ @sequel.drop_table?(:special)
+ @specialmap.create_schema(@sequel)
+ end
+
+ it 'sets a default on the column' do
+ @sequel[:special].insert({_id: 'a'})
+ row = @sequel[:special].select.first
+ assert_instance_of(Time, row[:mosql_updated])
+ end
+
+ it 'Can populate $timestamp on COPY' do
+ objects = [
+ {'_id' => "a"},
+ {'_id' => "b"}
+ ]
+ before = @sequel.select(Sequel.function(:NOW)).first[:now]
+ @specialmap.copy_data(@sequel, 'db.collection',
+ objects.map { |o| @specialmap.transform('db.collection', o) } )
+ after = @sequel.select(Sequel.function(:NOW)).first[:now]
+ rows = @sequel[:special].select.sort_by { |r| r[:_id] }
+ assert_instance_of(Time, rows[0][:mosql_updated])
+ assert(rows[0][:mosql_updated] > before && rows[0][:mosql_updated] < after)
+ end
+ end
end