-
Notifications
You must be signed in to change notification settings - Fork 21
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
Specs & Integration for ActiveRecord and ROM #37
Conversation
spec/build_schema_spec.rb
Outdated
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra blank line detected.
spec/build_schema_spec.rb
Outdated
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra blank line detected.
1 similar comment
spec/orms/rom/instances_spec.rb
Outdated
# frozen_string_literal: true | ||
|
||
class UserModel | ||
include Surrealist |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
include is used at the top level. Use inside class or module.
spec/orms/rom/instances_spec.rb
Outdated
# frozen_string_literal: true | ||
|
||
class UserModel | ||
include Surrealist |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
include is used at the top level. Use inside class or module.
@AlessandroMinali please review. I have decided to limit this PR to only AR and ROM because it might get too bloated. Some notes:
class Human < ActiveRecord::Base
include Surrealist
has_many :children
json_schema { { name: String, children: { age: Integer } } }
end
Human.first.surrealize
# => { name: 'John', children: [ { name: 'Jane', age: 15}, { name: 'Josh', age: 5 } ] }
|
I'll look at this next week, currently on vacation. A quick scan looks good, but I'll want to try and pull out specific references to AR and ROM from the code. |
# Conflicts: # .rubocop.yml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems good but it's unfortunate(possibly unavoidable) that we have to make library specific patches.
lib/surrealist.rb
Outdated
# Class variable name that is set by SchemaDefiner | ||
CLASS_VARIABLE = '@@__surrealist_schema'.freeze | ||
# Struct to carry schema along | ||
Schema = Struct.new(:key, :value).freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this bee at the top level, it is only ever used in Builder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I'll place it in Builder
.
# @param [Symbol] method | ||
# | ||
# @return [Boolean] | ||
def ar_collection?(instance, method) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to remove this...but with no success, this will probably become pretty nasty with more integrations. Seems to be an issue caused only when joining queries(?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's unfortunate, but I didn't find a solution to escape this check. It's caused by ActiveRecord::Associations::AssociationProxy
@@ -15,7 +15,11 @@ class SchemaDefiner | |||
def self.call(klass, hash) | |||
raise Surrealist::InvalidSchemaError, 'Schema should be defined as a hash' unless hash.is_a?(Hash) | |||
|
|||
klass.instance_variable_set('@__surrealist_schema', hash) | |||
if klass.name =~ /ROM::Struct/ | |||
klass.class_variable_set('@@__surrealist_schema', hash) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will probably cause undesired behavior with ROM(?)
Probably need similar spec for ROM similar to this one.
surrealist/spec/build_schema_spec.rb
Lines 192 to 198 in 0a8a7ad
context 'inheritance of class that has delegated but we don\'t delegate' do | |
it 'raises RuntimeError' do | |
expect { FriendOfGuest.new.build_schema } | |
.to raise_error(Surrealist::UnknownSchemaError, | |
"Can't serialize FriendOfGuest - no schema was provided.") | |
end | |
end |
I guess this brings up the problem of what is the minimum expected behaviour we want to test since we are not fully replicating all tests for each ORM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added such spec. Talking about minimal behaviour to test, I think that ROM is pretty much covered right now, though it may seem that there are not so many examples there. AR tests are pretty massive because of scopes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a few more specs to cover some deeper examples of what I was worried about.
add spec to cover grandchild delegation subtleties
@AlessandroMinali thanks! |
#12
Collections, scopes, associations etc
TODO: