From 8bdd5b8307cc100b5f084bb6d570501a8358354e Mon Sep 17 00:00:00 2001 From: dsenarruzza Date: Thu, 5 Nov 2020 15:37:22 -0300 Subject: [PATCH] fix tests about computed attributes according with changes in it --- spec/minitest_helper.rb | 11 ++++++++++- spec/nql/computed_attributes_spec.rb | 12 ++++++------ spec/nql/syntax_parser_spec.rb | 7 +++---- spec/query_spec.rb | 10 +++++----- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/spec/minitest_helper.rb b/spec/minitest_helper.rb index 4f26795..c40dc54 100644 --- a/spec/minitest_helper.rb +++ b/spec/minitest_helper.rb @@ -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] @@ -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 diff --git a/spec/nql/computed_attributes_spec.rb b/spec/nql/computed_attributes_spec.rb index a36912a..4c37746 100644 --- a/spec/nql/computed_attributes_spec.rb +++ b/spec/nql/computed_attributes_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/nql/syntax_parser_spec.rb b/spec/nql/syntax_parser_spec.rb index ff83fab..4e0ae0e 100644 --- a/spec/nql/syntax_parser_spec.rb +++ b/spec/nql/syntax_parser_spec.rb @@ -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 diff --git a/spec/query_spec.rb b/spec/query_spec.rb index 9305dd4..59a1734 100644 --- a/spec/query_spec.rb +++ b/spec/query_spec.rb @@ -380,18 +380,18 @@ 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') ] @@ -399,7 +399,7 @@ 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') ] @@ -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