Skip to content

Commit

Permalink
spec(orm): test serializable instance without table
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolab committed Apr 14, 2021
1 parent c80ae44 commit 1b7a687
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
29 changes: 29 additions & 0 deletions spec/adapter_tests/orm/migrations/migrations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,32 @@ class Tag < DBX::ORM::Model
relation profiles : Array(Profile)
end
end

# Just a noop Model without relation nor table.
class Noop < DBX::ORM::Model
# For generic tests adapters
{% if ADAPTER_NAME == :pg %}adapter :pg{% end %}
{% if ADAPTER_NAME == :sqlite %}adapter :sqlite{% end %}

table :noop
connection "app"

class_getter pk_name : String = "uid"
class_getter pk_type = String
class_getter fk_name = "noop_uid"

# DB table schema
class Schema < DBX::ORM::Schema
@[DB::Field(ignore: true)]
@[JSON::Field(ignore: true)]
def _pk
self.uid
end

@[JSON::Field(key: "createdAt")]
field created_at : Time

field uid : String
field name : String
end
end
18 changes: 18 additions & 0 deletions spec/adapter_tests/orm/model/schema_tests.cr
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ describe DBX::ORM::Schema do
)
end

it "should be able to create a serializable instance even if there is no table" do
now = Time.utc
noop = Noop::Schema.new(created_at: now, name: "noop", uid: "uuuuuu")
noop.should be_a DBX::ORM::Schema
noop.created_at.should be_a Time
noop.created_at.should eq now
noop.uid.should eq "uuuuuu"
noop.name.should eq "noop"
noop.to_json.should eq %({"createdAt":"#{now.to_rfc3339}","uid":"uuuuuu","name":"noop"})

noop = Noop::Schema.from_json %({"createdAt":"#{now.to_rfc3339(fraction_digits: 9)}","uid":"iii","name":"noop2"})
noop.should be_a DBX::ORM::Schema
noop.created_at.should be_a Time
noop.created_at.should eq now
noop.uid.should eq "iii"
noop.name.should eq "noop2"
end

pending ".from_rs" do
User
end
Expand Down

0 comments on commit 1b7a687

Please sign in to comment.