Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom field types #342

Merged
merged 6 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
351 changes: 351 additions & 0 deletions spec/granite/converters/converters_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,351 @@
require "../../spec_helper"

describe Granite::Converters do
Blacksmoke16 marked this conversation as resolved.
Show resolved Hide resolved
{% if env("CURRENT_ADAPTER") == "pg" %}
describe "#save" do
it "should handle nil values" do
model = ConverterModel.new
model.save.should be_true
model.id.should be_a Int64

# Enums
model.smallint_enum.should be_nil
model.bigint_enum.should be_nil
model.string_enum.should be_nil
model.enum_enum.should be_nil
model.binary_enum.should be_nil

# UUID
model.string_uuid.should be_nil
model.binary_uuid.should be_nil

# Numeric
model.numeric.should be_nil

# JSON
model.string_json.should be_nil
model.string_jsonb.should be_nil
model.binary_json.should be_nil
end

it "should handle actual values" do
uuid = UUID.random
obj = MyType.new

model = ConverterModel.new binary_uuid: uuid, string_uuid: uuid, numeric: Math::PI.round(20)

model.binary_json = model.string_jsonb = model.string_json = obj

model.smallint_enum = MyEnum::Zero
model.bigint_enum = MyEnum::One
model.string_enum = MyEnum::Two
model.enum_enum = MyEnum::Three
model.binary_enum = MyEnum::Four

model.save.should be_true
model.id.should be_a Int64

# Enums
model.smallint_enum.should eq MyEnum::Zero
model.bigint_enum.should eq MyEnum::One
model.string_enum.should eq MyEnum::Two
model.enum_enum.should eq MyEnum::Three
model.binary_enum.should eq MyEnum::Four

# UUID
model.string_uuid.should eq uuid
model.binary_uuid.should eq uuid

# Numeric
model.numeric.should eq Math::PI.round(20)

# JSON
model.string_json.should eq obj
model.string_jsonb.should eq obj
model.binary_json.should eq obj
end
end

describe "#read" do
it "should handle nil values" do
model = ConverterModel.new
model.save.should be_true
model.id.should be_a Int64

retrieved_model = ConverterModel.find! model.id

# Enums
retrieved_model.smallint_enum.should be_nil
retrieved_model.bigint_enum.should be_nil
retrieved_model.string_enum.should be_nil
retrieved_model.enum_enum.should be_nil
retrieved_model.binary_enum.should be_nil

# UUID
retrieved_model.string_uuid.should be_nil
retrieved_model.binary_uuid.should be_nil

# Numeric
retrieved_model.numeric.should be_nil

# JSON
retrieved_model.string_json.should be_nil
retrieved_model.string_jsonb.should be_nil
retrieved_model.binary_json.should be_nil
end

it "should handle actual values" do
uuid = UUID.random
obj = MyType.new

model = ConverterModel.new binary_uuid: uuid, string_uuid: uuid, numeric: Math::PI.round(20)

model.binary_json = model.string_jsonb = model.string_json = obj

model.smallint_enum = MyEnum::Zero
model.bigint_enum = MyEnum::One
model.string_enum = MyEnum::Two
model.enum_enum = MyEnum::Three
model.binary_enum = MyEnum::Four

model.save.should be_true
model.id.should be_a Int64

retrieved_model = ConverterModel.find! model.id

# Enum
retrieved_model.smallint_enum.should eq MyEnum::Zero
retrieved_model.bigint_enum.should eq MyEnum::One
retrieved_model.string_enum.should eq MyEnum::Two
retrieved_model.enum_enum.should eq MyEnum::Three
retrieved_model.binary_enum.should eq MyEnum::Four

# UUID
retrieved_model.string_uuid.should eq uuid
retrieved_model.binary_uuid.should eq uuid

# Numeric
retrieved_model.numeric.should eq Math::PI.round(20)

# JSON
retrieved_model.string_json.should eq obj
retrieved_model.string_jsonb.should eq obj
retrieved_model.binary_json.should eq obj
end
end
{% elsif env("CURRENT_ADAPTER") == "sqlite" %}
describe "#save" do
it "should handle nil values" do
model = ConverterModel.new
model.save.should be_true
model.id.should be_a Int64

# Enums
model.int_enum.should be_nil
model.string_enum.should be_nil
model.binary_enum.should be_nil

# UUID
model.string_uuid.should be_nil
model.binary_uuid.should be_nil

# JSON
model.string_json.should be_nil
model.binary_json.should be_nil
end

it "should handle actual values" do
uuid = UUID.random
obj = MyType.new

model = ConverterModel.new binary_uuid: uuid, string_uuid: uuid

model.binary_json = model.string_json = obj

model.int_enum = MyEnum::Zero
model.string_enum = MyEnum::Two
model.binary_enum = MyEnum::Four

model.save.should be_true
model.id.should be_a Int64

# Enums
model.int_enum.should eq MyEnum::Zero
model.string_enum.should eq MyEnum::Two
model.binary_enum.should eq MyEnum::Four

# UUID
model.string_uuid.should eq uuid
model.binary_uuid.should eq uuid

# JSON
model.string_json.should eq obj
model.binary_json.should eq obj
end
end

describe "#read" do
it "should handle nil values" do
model = ConverterModel.new
model.save.should be_true
model.id.should be_a Int64

retrieved_model = ConverterModel.find! model.id

# Enums
retrieved_model.int_enum.should be_nil
retrieved_model.string_enum.should be_nil
retrieved_model.binary_enum.should be_nil

# UUID
retrieved_model.string_uuid.should be_nil
retrieved_model.binary_uuid.should be_nil

# JSON
retrieved_model.string_json.should be_nil
retrieved_model.binary_json.should be_nil
end

it "should handle actual values" do
uuid = UUID.random
obj = MyType.new

model = ConverterModel.new binary_uuid: uuid, string_uuid: uuid

model.binary_json = model.string_json = obj

model.int_enum = MyEnum::Zero
model.string_enum = MyEnum::Two
model.binary_enum = MyEnum::Four

model.save.should be_true
model.id.should be_a Int64

retrieved_model = ConverterModel.find! model.id

# Enums
retrieved_model.int_enum.should eq MyEnum::Zero
retrieved_model.string_enum.should eq MyEnum::Two
retrieved_model.binary_enum.should eq MyEnum::Four

# UUID
retrieved_model.string_uuid.should eq uuid
retrieved_model.binary_uuid.should eq uuid

# JSON
retrieved_model.string_json.should eq obj
retrieved_model.binary_json.should eq obj
end
end
{% elsif env("CURRENT_ADAPTER") == "mysql" %}
describe "#save" do
it "should handle nil values" do
model = ConverterModel.new
model.save.should be_true
model.id.should be_a Int64

# Enums
model.int_enum.should be_nil
model.string_enum.should be_nil
model.enum_enum.should be_nil
model.binary_enum.should be_nil

# UUID
model.string_uuid.should be_nil
model.binary_uuid.should be_nil

# JSON
model.string_json.should be_nil
model.binary_json.should be_nil
end

it "should handle actual values" do
uuid = UUID.random
obj = MyType.new

model = ConverterModel.new binary_uuid: uuid, string_uuid: uuid

model.binary_json = model.string_json = obj

model.int_enum = MyEnum::Zero
model.string_enum = MyEnum::Two
model.enum_enum = MyEnum::Three
model.binary_enum = MyEnum::Four

model.save.should be_true
model.id.should be_a Int64

# Enums
model.int_enum.should eq MyEnum::Zero
model.string_enum.should eq MyEnum::Two
model.enum_enum.should eq MyEnum::Three
model.binary_enum.should eq MyEnum::Four

# UUID
model.string_uuid.should eq uuid
model.binary_uuid.should eq uuid

# JSON
model.string_json.should eq obj
model.binary_json.should eq obj
end
end

describe "#read" do
it "should handle nil values" do
model = ConverterModel.new
model.save.should be_true
model.id.should be_a Int64

retrieved_model = ConverterModel.find! model.id

# Enums
retrieved_model.int_enum.should be_nil
retrieved_model.string_enum.should be_nil
retrieved_model.enum_enum.should be_nil
retrieved_model.binary_enum.should be_nil

# UUID
retrieved_model.string_uuid.should be_nil
retrieved_model.binary_uuid.should be_nil

# JSON
retrieved_model.string_json.should be_nil
retrieved_model.binary_json.should be_nil
end

it "should handle actual values" do
uuid = UUID.random
obj = MyType.new

model = ConverterModel.new binary_uuid: uuid, string_uuid: uuid

model.binary_json = model.string_json = obj

model.int_enum = MyEnum::Zero
model.string_enum = MyEnum::Two
model.enum_enum = MyEnum::Three
model.binary_enum = MyEnum::Four

model.save.should be_true
model.id.should be_a Int64

retrieved_model = ConverterModel.find! model.id

# Enums
retrieved_model.int_enum.should eq MyEnum::Zero
retrieved_model.string_enum.should eq MyEnum::Two
retrieved_model.enum_enum.should eq MyEnum::Three
retrieved_model.binary_enum.should eq MyEnum::Four

# UUID
retrieved_model.string_uuid.should eq uuid
retrieved_model.binary_uuid.should eq uuid

# JSON
retrieved_model.string_json.should eq obj
retrieved_model.binary_json.should eq obj
end
end
{% end %}
end
7 changes: 3 additions & 4 deletions spec/granite/fields/uuid_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ describe "UUID creation" do
item = UUIDModel.new
item.uuid.should be_nil
item.save
item.uuid.should be_a(String)
uuid = UUID.new item.uuid!
uuid.version.to_s.should eq "V4"
uuid.variant.to_s.should eq "RFC4122"
item.uuid.should be_a(UUID)
item.uuid!.version.v4?.should be_true
item.uuid!.variant.rfc4122?.should be_true
end
end
Loading