-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for computed attributes related and simple
- Loading branch information
1 parent
94f17f1
commit 606c949
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |