Skip to content

Commit

Permalink
fix tests about computed attributes according with changes in it
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosenarruzza committed Nov 5, 2020
1 parent e2a930e commit 8bdd5b8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
11 changes: 10 additions & 1 deletion spec/minitest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

User = Rasti::DB::Model[:id, :name, :posts, :comments, :person, :comments_count]
Post = Rasti::DB::Model[:id, :title, :body, :user_id, :user, :comments, :categories, :language_id, :language]
Post = Rasti::DB::Model[:id, :title, :body, :user_id, :user, :comments, :categories, :language_id, :language, :notice, :author]
Comment = Rasti::DB::Model[:id, :text, :user_id, :user, :post_id, :post]
Category = Rasti::DB::Model[:id, :name, :posts]
Person = Rasti::DB::Model[:document_number, :first_name, :last_name, :birth_date, :user_id, :user, :languages, :full_name]
Expand Down Expand Up @@ -59,6 +59,15 @@ class Posts < Rasti::DB::Collection
.distinct
end
end

computed_attribute :notice do
Rasti::DB::ComputedAttribute.new Sequel.join([:title, ': ', :body])
end

computed_attribute :author do
Rasti::DB::ComputedAttribute.new Sequel[:user]
end

end

class Comments < Rasti::DB::Collection
Expand Down
12 changes: 6 additions & 6 deletions spec/nql/computed_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ def parse(expression)
end

it 'must have one computed attributes' do
tree = parse 'count_paragraphs() = 1'
tree = parse 'notice = any notice'

tree.computed_attributes.must_equal [:count_paragraphs]
tree.computed_attributes(Posts).must_equal [:notice]
end

it 'must have multiple computed attributes' do
tree = parse 'count_paragraphs() = 1 & (body(): Hi | title() = good morning)'
tree = parse 'notice = any notice & (author: anonym | title = good morning)'

tree.computed_attributes.must_equal [:count_paragraphs, :body, :title]
tree.computed_attributes(Posts).must_equal [:notice, :author]
end

it 'must have not repeated computed attributes when expression have it' do
tree = parse 'body() = Hi | body() = Bye'
tree = parse 'notice = Hi | notice = Bye'

tree.computed_attributes.must_equal [:body]
tree.computed_attributes(Posts).must_equal [:notice]
end

end
7 changes: 3 additions & 4 deletions spec/nql/syntax_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,14 @@ def parse(expression)

left_hand_operand = tree.proposition.attribute
left_hand_operand.tables.must_equal ['relation_table_one', 'relation_table_two']
left_hand_operand.column.must_equal 'column'
left_hand_operand.column.must_equal :column
end

it 'must parse expression with computed attribute' do
tree = parse 'computed_attribute() = 1'

tree = parse 'comments_count = 1'
computed = tree.proposition.attribute
computed.tables.must_equal []
computed.computed_attributes.must_equal [:computed_attribute]
computed.computed_attributes(Users).must_equal [:comments_count]
end

end
Expand Down
10 changes: 5 additions & 5 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,26 +380,26 @@

it 'Filter relation computed attribute' do
db[:comments].insert post_id: 1, user_id: 5, text: 'Comment 4'
users_query.nql('comments_count() = 2').all.must_equal [User.new(id: 5, name: 'User 5')]
users_query.nql('comments_count = 2').all.must_equal [User.new(id: 5, name: 'User 5')]
end

it 'Filter with relation computed attribute with "and" combined' do
db[:comments].insert post_id: 1, user_id: 5, text: 'Comment 4'
db[:comments].insert post_id: 1, user_id: 4, text: 'Comment 3'
users_query.nql('(comments_count() > 1) & (id = 5)').all.must_equal [User.new(id: 5, name: 'User 5')]
users_query.nql('(comments_count > 1) & (id = 5)').all.must_equal [User.new(id: 5, name: 'User 5')]
end

it 'Filter relation computed attribute with "or" combined' do
db[:comments].insert post_id: 1, user_id: 2, text: 'Comment 3'
users_query.nql('(comments_count() = 2) | (id = 5)')
users_query.nql('(comments_count = 2) | (id = 5)')
.order(:id)
.all
.must_equal [ User.new(id: 2, name: 'User 2'), User.new(id: 5, name: 'User 5') ]
end

it 'Filter relation computed attribute with "and" and "or" combined' do
db[:comments].insert post_id: 1, user_id: 2, text: 'Comment 3'
users_query.nql('((comments_count() = 2) | (id = 5)) & (name: User 5)')
users_query.nql('((comments_count = 2) | (id = 5)) & (name: User 5)')
.order(:id)
.all
.must_equal [ User.new(id: 5, name: 'User 5') ]
Expand All @@ -412,7 +412,7 @@
last_name: 'Last Name 1',
birth_date: Date.parse('2020-04-24')

people_query.nql('full_name() = Name 1 Last Name 1')
people_query.nql('full_name = Name 1 Last Name 1')
.all
.must_equal [person_expected]
end
Expand Down

0 comments on commit 8bdd5b8

Please sign in to comment.