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

Specs & Integration for ActiveRecord and ROM #37

Merged
merged 34 commits into from
Nov 22, 2017
Merged

Specs & Integration for ActiveRecord and ROM #37

merged 34 commits into from
Nov 22, 2017

Conversation

nesaulov
Copy link
Owner

@nesaulov nesaulov commented Oct 27, 2017

#12

Collections, scopes, associations etc

TODO:

  • ActiveRecord
  • ROM (v3)

Repository owner deleted a comment from coveralls Oct 28, 2017
Repository owner deleted a comment from coveralls Oct 28, 2017
Repository owner deleted a comment from coveralls Oct 28, 2017
@nesaulov nesaulov changed the title [WIP] Specs for all ORMs [WIP] Specs & Integration for all ORMs Oct 28, 2017




Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line detected.





Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line detected.

Repository owner deleted a comment from houndci-bot Oct 29, 2017
Repository owner deleted a comment from houndci-bot Oct 29, 2017
Repository owner deleted a comment from houndci-bot Oct 29, 2017
Repository owner deleted a comment from houndci-bot Oct 29, 2017
Repository owner deleted a comment from houndci-bot Oct 29, 2017
Repository owner deleted a comment from houndci-bot Oct 29, 2017
Repository owner deleted a comment from houndci-bot Oct 29, 2017
Repository owner deleted a comment from houndci-bot Oct 29, 2017
Repository owner deleted a comment from houndci-bot Oct 29, 2017
Repository owner deleted a comment from houndci-bot Oct 29, 2017
@coveralls
Copy link

coveralls commented Oct 29, 2017

Coverage Status

Coverage increased (+0.005%) to 99.583% when pulling 6ace23a on orm_specs into cf9cad5 on master.

@coveralls
Copy link

coveralls commented Oct 29, 2017

Coverage Status

Coverage increased (+0.005%) to 99.583% when pulling d2f5ec7 on orm_specs into cf9cad5 on master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.005%) to 99.583% when pulling d2f5ec7 on orm_specs into cf9cad5 on master.

@coveralls
Copy link

coveralls commented Oct 29, 2017

Coverage Status

Coverage increased (+0.005%) to 99.583% when pulling e9e5a31 on orm_specs into cf9cad5 on master.

Repository owner deleted a comment from houndci-bot Nov 4, 2017
# frozen_string_literal: true

class UserModel
include Surrealist

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.

# frozen_string_literal: true

class UserModel
include Surrealist

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.

@nesaulov nesaulov changed the title [WIP] Specs & Integration for all ORMs [WIP] Specs & Integration for ActiveRecord and ROM Nov 6, 2017
@nesaulov
Copy link
Owner Author

nesaulov commented Nov 6, 2017

@AlessandroMinali please review. I have decided to limit this PR to only AR and ROM because it might get too bloated. Some notes:

  • I've had to mess up Builder a bit because of AR::CollectionProxy serialization. I've done it so that Surrealist would work nicely with has_many associations:
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've had to mess up SchemaDefiner a bit because seems like instance variables are cleared with every call of ROM objects, so the only solution I see here is to define a class variable (which is not very good, but I don't know how to do it better - do you have any thoughts?)

@AlessandroMinali
Copy link
Collaborator

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.

@nesaulov nesaulov mentioned this pull request Nov 16, 2017
# Conflicts:
#	.rubocop.yml
Repository owner deleted a comment from houndci-bot Nov 16, 2017
Copy link
Collaborator

@AlessandroMinali AlessandroMinali left a 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.

# Class variable name that is set by SchemaDefiner
CLASS_VARIABLE = '@@__surrealist_schema'.freeze
# Struct to carry schema along
Schema = Struct.new(:key, :value).freeze
Copy link
Collaborator

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

Copy link
Owner Author

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)
Copy link
Collaborator

@AlessandroMinali AlessandroMinali Nov 17, 2017

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(?)

Copy link
Owner Author

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)
Copy link
Collaborator

@AlessandroMinali AlessandroMinali Nov 17, 2017

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.

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.

Copy link
Owner Author

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

@nesaulov nesaulov changed the title [WIP] Specs & Integration for ActiveRecord and ROM Specs & Integration for ActiveRecord and ROM Nov 18, 2017
Copy link
Collaborator

@AlessandroMinali AlessandroMinali left a 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.

#49

add spec to cover grandchild delegation subtleties
@nesaulov
Copy link
Owner Author

@AlessandroMinali thanks!

@nesaulov nesaulov merged commit 4ea3723 into master Nov 22, 2017
@nesaulov nesaulov deleted the orm_specs branch November 22, 2017 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants