2016-03-28 23:45:07 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
require "base64"
|
|
|
|
|
|
|
|
class SimpleMapping
|
|
|
|
DB.mapping({
|
|
|
|
c0: Int32,
|
2016-09-06 16:24:57 +00:00
|
|
|
c1: String,
|
2016-03-28 23:45:07 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2016-07-04 15:13:39 +00:00
|
|
|
class NonStrictMapping
|
|
|
|
DB.mapping({
|
|
|
|
c1: Int32,
|
2016-09-06 16:24:57 +00:00
|
|
|
c2: String,
|
2016-07-04 15:13:39 +00:00
|
|
|
}, strict: false)
|
|
|
|
end
|
|
|
|
|
2016-03-28 23:45:07 +00:00
|
|
|
class MappingWithDefaults
|
|
|
|
DB.mapping({
|
2016-09-06 16:24:57 +00:00
|
|
|
c0: {type: Int32, default: 10},
|
|
|
|
c1: {type: String, default: "c"},
|
2016-03-28 23:45:07 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
class MappingWithNilables
|
|
|
|
DB.mapping({
|
2016-09-06 16:24:57 +00:00
|
|
|
c0: {type: Int32, nilable: true, default: 10},
|
|
|
|
c1: {type: String, nilable: true},
|
2016-03-28 23:45:07 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2017-02-14 21:51:45 +00:00
|
|
|
class MappingWithNilTypes
|
|
|
|
DB.mapping({
|
|
|
|
c0: {type: Int32?, default: 10},
|
|
|
|
c1: String?,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2017-03-06 15:18:04 +00:00
|
|
|
class MappingWithNilUnionTypes
|
|
|
|
DB.mapping({
|
|
|
|
c0: {type: Int32 | Nil, default: 10},
|
|
|
|
c1: Nil | String,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2016-03-28 23:45:07 +00:00
|
|
|
class MappingWithKeys
|
|
|
|
DB.mapping({
|
2016-09-06 16:24:57 +00:00
|
|
|
foo: {type: Int32, key: "c0"},
|
|
|
|
bar: {type: String, key: "c1"},
|
2016-03-28 23:45:07 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
class MappingWithConverter
|
|
|
|
module Base64Converter
|
|
|
|
def self.from_rs(rs)
|
|
|
|
Base64.decode(rs.read(String))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DB.mapping({
|
2016-09-06 16:24:57 +00:00
|
|
|
c0: {type: Slice(UInt8), converter: MappingWithConverter::Base64Converter},
|
|
|
|
c1: {type: String},
|
2016-03-28 23:45:07 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
macro from_dummy(query, type)
|
|
|
|
with_dummy do |db|
|
|
|
|
rs = db.query({{ query }})
|
|
|
|
rs.move_next
|
|
|
|
%obj = {{ type }}.new(rs)
|
|
|
|
rs.close
|
|
|
|
%obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
macro expect_mapping(query, t, values)
|
|
|
|
%obj = from_dummy({{ query }}, {{ t }})
|
|
|
|
%obj.should be_a({{ t }})
|
|
|
|
{% for key, value in values %}
|
|
|
|
%obj.{{key.id}}.should eq({{value}})
|
|
|
|
{% end %}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "DB.mapping" do
|
|
|
|
it "should initialize a simple mapping" do
|
|
|
|
expect_mapping("1,a", SimpleMapping, {c0: 1, c1: "a"})
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should fail to initialize a simple mapping if types do not match" do
|
|
|
|
expect_raises { from_dummy("b,a", SimpleMapping) }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should fail to initialize a simple mapping if there is a missing column" do
|
|
|
|
expect_raises { from_dummy("1", SimpleMapping) }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should fail to initialize a simple mapping if there is an unexpected column" do
|
|
|
|
expect_raises { from_dummy("1,a,b", SimpleMapping) }
|
|
|
|
end
|
|
|
|
|
2016-07-04 15:13:39 +00:00
|
|
|
it "should initialize a non-strict mapping if there is an unexpected column" do
|
|
|
|
expect_mapping("1,2,a,b", NonStrictMapping, {c1: 2, c2: "a"})
|
|
|
|
end
|
|
|
|
|
2016-03-28 23:45:07 +00:00
|
|
|
it "should initialize a mapping with default values" do
|
|
|
|
expect_mapping("1,a", MappingWithDefaults, {c0: 1, c1: "a"})
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should initialize a mapping using default values if columns are missing" do
|
|
|
|
expect_mapping("1", MappingWithDefaults, {c0: 1, c1: "c"})
|
|
|
|
end
|
|
|
|
|
2016-07-04 15:13:39 +00:00
|
|
|
it "should initialize a mapping using default values if values are nil and types are non nilable" do
|
|
|
|
expect_mapping("1,NULL", MappingWithDefaults, {c0: 1, c1: "c"})
|
|
|
|
end
|
|
|
|
|
2017-02-14 21:51:45 +00:00
|
|
|
it "should initialize a mapping with nilable set if columns are missing" do
|
2016-03-28 23:45:07 +00:00
|
|
|
expect_mapping("1", MappingWithNilables, {c0: 1, c1: nil})
|
|
|
|
end
|
|
|
|
|
2017-02-14 21:51:45 +00:00
|
|
|
it "should initialize a mapping with nilable set ignoring default value if NULL" do
|
2016-07-04 15:13:39 +00:00
|
|
|
expect_mapping("NULL,a", MappingWithNilables, {c0: nil, c1: "a"})
|
|
|
|
end
|
|
|
|
|
2017-02-14 21:51:45 +00:00
|
|
|
it "should initialize a mapping with nilable types if columns are missing" do
|
|
|
|
expect_mapping("1", MappingWithNilTypes, {c0: 1, c1: nil})
|
2017-03-06 15:18:04 +00:00
|
|
|
expect_mapping("1", MappingWithNilUnionTypes, {c0: 1, c1: nil})
|
2017-02-14 21:51:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should initialize a mapping with nilable types ignoring default value if NULL" do
|
|
|
|
expect_mapping("NULL,a", MappingWithNilTypes, {c0: nil, c1: "a"})
|
2017-03-06 15:18:04 +00:00
|
|
|
expect_mapping("NULL,a", MappingWithNilUnionTypes, {c0: nil, c1: "a"})
|
2017-02-14 21:51:45 +00:00
|
|
|
end
|
|
|
|
|
2016-03-28 23:45:07 +00:00
|
|
|
it "should initialize a mapping with different keys" do
|
|
|
|
expect_mapping("1,a", MappingWithKeys, {foo: 1, bar: "a"})
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should initialize a mapping with a value converter" do
|
|
|
|
expect_mapping("Zm9v,a", MappingWithConverter, {c0: "foo".to_slice, c1: "a"})
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should initialize multiple instances from a single resultset" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.query("1,a 2,b") do |rs|
|
|
|
|
objs = SimpleMapping.from_rs(rs)
|
|
|
|
objs.size.should eq(2)
|
|
|
|
objs[0].c0.should eq(1)
|
|
|
|
objs[0].c1.should eq("a")
|
|
|
|
objs[1].c0.should eq(2)
|
|
|
|
objs[1].c1.should eq("b")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-04 15:46:45 +00:00
|
|
|
it "should initialize from a query_one" do
|
|
|
|
with_dummy do |db|
|
|
|
|
obj = db.query_one "1,a", as: SimpleMapping
|
|
|
|
obj.c0.should eq(1)
|
|
|
|
obj.c1.should eq("a")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should initialize from a query_all" do
|
|
|
|
with_dummy do |db|
|
|
|
|
objs = db.query_all "1,a 2,b", as: SimpleMapping
|
|
|
|
objs.size.should eq(2)
|
|
|
|
objs[0].c0.should eq(1)
|
|
|
|
objs[0].c1.should eq("a")
|
|
|
|
objs[1].c0.should eq(2)
|
|
|
|
objs[1].c1.should eq("b")
|
|
|
|
end
|
|
|
|
end
|
2016-03-28 23:45:07 +00:00
|
|
|
end
|