Skip to content

Commit

Permalink
add tests for computed attributes related and simple
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosenarruzza committed Oct 29, 2020
1 parent 94f17f1 commit 606c949
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions spec/computed_attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'minitest_helper'

describe 'ComputedAttributes' do

it 'Apply to Relation Computed Attribute must generate correct query' do
dataset = db[:comments]
computed_attribute = Rasti::DB::ComputedAttributes::Relation.new value: Sequel.function('count', :id),
table: dataset,
type: :inner,
foreign_key: :user_id,
primary_key: :id,
attributes: [:field1, :field2]

expected_query = "SELECT * FROM `comments` INNER JOIN (SELECT count(`id`) AS 'value', `field1`, `field2`, `user_id` FROM `comments` GROUP BY `user_id`) AS 'count_comments' ON (`count_comments`.`user_id` = `comments`.`id`)"
computed_attribute.apply_to(dataset, :count_comments)
.sql
.must_equal expected_query
end

it 'Apply to Simple Computed Attribute must generate correct query' do
primary_key = People.primary_key
dataset = db[:people]
computed_attribute = Rasti::DB::ComputedAttributes::Simple.new value: Sequel.join([:first_name, ' ', :last_name]),
table: dataset,
primary_key: primary_key

expected_query = "SELECT * FROM `people` INNER JOIN (SELECT (`first_name` || ' ' || `last_name`) AS 'value', `#{primary_key}` FROM `people`) AS 'full_text' ON (`full_text`.`#{primary_key}` = `people`.`#{primary_key}`)"
computed_attribute.apply_to(dataset, :full_text)
.sql
.must_equal expected_query
end

end

0 comments on commit 606c949

Please sign in to comment.