spec/query_spec.rb in rasti-db-2.0.1 vs spec/query_spec.rb in rasti-db-2.1.0
- old
+ new
@@ -3,28 +3,28 @@
describe 'Query' do
before do
custom_db[:languages].insert name: 'Spanish'
- 1.upto(10) do |i|
+ 1.upto(10) do |i|
db[:users].insert name: "User #{i}"
- db[:people].insert user_id: i,
- document_number: "document_#{i}",
- first_name: "Name #{i}",
- last_name: "Last Name #{i}",
+ db[:people].insert user_id: i,
+ document_number: "document_#{i}",
+ first_name: "Name #{i}",
+ last_name: "Last Name #{i}",
birth_date: Date.parse('2020-04-24')
db[:languages_people].insert language_id: 1, document_number: "document_#{i}"
end
db[:posts].insert user_id: 2, title: 'Sample post', body: '...', language_id: 1
db[:posts].insert user_id: 1, title: 'Another post', body: '...', language_id: 1
db[:posts].insert user_id: 4, title: 'Best post', body: '...', language_id: 1
1.upto(3) { |i| db[:categories].insert name: "Category #{i}" }
-
+
db[:comments].insert post_id: 1, user_id: 5, text: 'Comment 1'
db[:comments].insert post_id: 1, user_id: 7, text: 'Comment 2'
db[:comments].insert post_id: 2, user_id: 2, text: 'Comment 3'
db[:categories_posts].insert post_id: 1, category_id: 1
@@ -33,15 +33,17 @@
db[:categories_posts].insert post_id: 2, category_id: 3
db[:categories_posts].insert post_id: 3, category_id: 3
end
let(:users_query) { Rasti::DB::Query.new collection_class: Users, dataset: db[:users], environment: environment }
-
+
let(:posts_query) { Rasti::DB::Query.new collection_class: Posts, dataset: db[:posts], environment: environment }
-
+
let(:comments_query) { Rasti::DB::Query.new collection_class: Comments, dataset: db[:comments], environment: environment }
+ let(:people_query) { Rasti::DB::Query.new collection_class: People, dataset: db[:people], environment: environment }
+
let(:languages_query) { Rasti::DB::Query.new collection_class: Languages, dataset: custom_db[:languages], environment: environment }
it 'Count' do
users_query.count.must_equal 10
end
@@ -85,23 +87,23 @@
categories = db[:categories].where(id: [1,2]).select(:id).map { |c| Category.new c }
post = Post.new db[:posts].where(id: 1).first.merge(user: user, categories: categories)
selected_attributes = {
- user: [:id],
- 'user.person' => [:document_number, :user_id],
+ user: [:id],
+ 'user.person' => [:document_number, :user_id],
'user.person.languages' => [:id],
categories: [:id]
}
posts_query.where(id: 1)
.graph(*selected_attributes.keys)
.select_graph_attributes(selected_attributes)
.all
.must_equal [post]
end
-
+
it 'Exclude graph attributes' do
language = Language.new custom_db[:languages].where(id: 1).select(:id).first
person = Person.new db[:people].where(document_number: 'document_2').select(:document_number, :user_id).first.merge(languages: [language])
@@ -110,23 +112,23 @@
categories = db[:categories].where(id: [1,2]).select(:id).map { |c| Category.new c }
post = Post.new db[:posts].where(id: 1).first.merge(user: user, categories: categories)
excluded_attributes = {
- user: [:name],
- 'user.person' => [:first_name, :last_name, :birth_date],
+ user: [:name],
+ 'user.person' => [:first_name, :last_name, :birth_date],
'user.person.languages' => [:name],
categories: [:name]
}
posts_query.where(id: 1)
.graph(*excluded_attributes.keys)
.exclude_graph_attributes(excluded_attributes)
.all
.must_equal [post]
end
-
+
it 'All graph attributes' do
person = Person.new db[:people].where(document_number: 'document_2').first
user = User.new db[:users].where(id: 2).select(:id).first.merge(person: person)
@@ -138,57 +140,124 @@
.all_graph_attributes('user.person')
.all
.must_equal [post]
end
+ describe 'Append computed attribute' do
+ it 'With join' do
+ db[:comments].insert post_id: 1, user_id: 5, text: 'Comment 4'
+ users_query.append_computed_attribute(:comments_count)
+ .where(id: 5)
+ .all
+ .must_equal [User.new(id: 5, name: 'User 5', comments_count: 2)]
+ end
+
+ it 'Without join' do
+ person_expected = Person.new user_id: 1,
+ document_number: 'document_1',
+ first_name: 'Name 1',
+ last_name: 'Last Name 1',
+ birth_date: Date.parse('2020-04-24'),
+ full_name: 'Name 1 Last Name 1'
+
+ people_query.append_computed_attribute(:full_name)
+ .where(document_number: 'document_1')
+ .all
+ .must_equal [person_expected]
+ end
+ end
+
it 'Map' do
users_query.map(&:name).must_equal db[:users].map(:name)
end
it 'Detect' do
users_query.detect(id: 3).must_equal User.new(id: 3, name: 'User 3')
end
+ describe 'Each' do
+
+ it 'without size' do
+ users = []
+
+ users_query.each do |user|
+ users << user
+ end
+
+ users.size.must_equal 10
+ users.each_with_index do |user, i|
+ user.must_equal User.new(id: i+1, name: "User #{i+1}")
+ end
+ end
+
+ it 'with size' do
+ users = []
+ users_query.each(batch_size: 2) do |user|
+ users << user
+ end
+
+ users.size.must_equal 10
+ users.each_with_index do |user, i|
+ user.must_equal User.new(id: i+1, name: "User #{i+1}")
+ end
+ end
+
+ end
+
+ it 'Each batch' do
+ users_batch = []
+ users_query.each_batch(size: 2) do |page|
+ users_batch << page
+ end
+
+ users_batch.size.must_equal 5
+ i = 1
+ users_batch.each do |user_page|
+ user_page.must_equal [User.new(id: i, name: "User #{i}"), User.new(id: i+1, name: "User #{i+1}")]
+ i += 2
+ end
+ end
+
it 'Where' do
users_query.where(id: 3).all.must_equal [User.new(id: 3, name: 'User 3')]
end
-
+
it 'Exclude' do
users_query.exclude(id: [1,2,3,4,5,6,7,8,9]).all.must_equal [User.new(id: 10, name: 'User 10')]
end
-
+
it 'And' do
users_query.where(id: [1,2]).where(name: 'User 2').all.must_equal [User.new(id: 2, name: 'User 2')]
end
-
+
it 'Or' do
users_query.where(id: 1).or(name: 'User 2').all.must_equal [
- User.new(id: 1, name: 'User 1'),
+ User.new(id: 1, name: 'User 1'),
User.new(id: 2, name: 'User 2')
]
end
-
+
it 'Order' do
posts_query.order(:title).all.must_equal [
- Post.new(id: 2, user_id: 1, title: 'Another post', body: '...', language_id: 1),
- Post.new(id: 3, user_id: 4, title: 'Best post', body: '...', language_id: 1),
+ Post.new(id: 2, user_id: 1, title: 'Another post', body: '...', language_id: 1),
+ Post.new(id: 3, user_id: 4, title: 'Best post', body: '...', language_id: 1),
Post.new(id: 1, user_id: 2, title: 'Sample post', body: '...', language_id: 1)
]
end
-
+
it 'Reverse order' do
posts_query.reverse_order(:title).all.must_equal [
Post.new(id: 1, user_id: 2, title: 'Sample post', body: '...', language_id: 1),
- Post.new(id: 3, user_id: 4, title: 'Best post', body: '...', language_id: 1),
+ Post.new(id: 3, user_id: 4, title: 'Best post', body: '...', language_id: 1),
Post.new(id: 2, user_id: 1, title: 'Another post', body: '...', language_id: 1)
]
end
-
+
it 'Limit and offset' do
users_query.limit(1).offset(1).all.must_equal [User.new(id: 2, name: 'User 2')]
end
-
+
it 'First' do
users_query.first.must_equal User.new(id: 1, name: 'User 1')
end
it 'Last' do
@@ -201,25 +270,25 @@
it 'Graph with multiple data sources' do
language = Language.new id: 1, name: 'Spanish'
person = Person.new user_id: 2,
- document_number: 'document_2',
- first_name: 'Name 2',
- last_name: 'Last Name 2',
+ document_number: 'document_2',
+ first_name: 'Name 2',
+ last_name: 'Last Name 2',
birth_date: Date.parse('2020-04-24'),
languages: [language]
- user = User.new id: 2,
+ user = User.new id: 2,
name: 'User 2',
person: person
- post = Post.new id: 1,
- user_id: 2,
+ post = Post.new id: 1,
+ user_id: 2,
user: user,
- title: 'Sample post',
- body: '...',
+ title: 'Sample post',
+ body: '...',
language_id: 1,
language: language
posts_query.where(id: 1).graph(:language, 'user.person.languages').first.must_equal post
end
@@ -321,11 +390,11 @@
it 'Invalid expression' do
error = proc { posts_query.nql('a + b') }.must_raise Rasti::DB::NQL::InvalidExpressionError
error.message.must_equal 'Invalid filter expression: a + b'
end
-
+
it 'Filter to self table' do
posts_query.nql('user_id > 1')
.pluck(:user_id)
.sort
.must_equal [2, 4]
@@ -346,9 +415,52 @@
it 'Filter combined' do
posts_query.nql('(categories.id = 1 | categories.id = 3) & comments.user.person.document_number = document_2')
.pluck(:id)
.must_equal [2]
+ end
+
+ describe 'Computed Attributes' do
+
+ 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')]
+ 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')]
+ 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)')
+ .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)')
+ .order(:id)
+ .all
+ .must_equal [ User.new(id: 5, name: 'User 5') ]
+ end
+
+ it 'Filter simple computed attribute' do
+ person_expected = Person.new user_id: 1,
+ document_number: 'document_1',
+ first_name: 'Name 1',
+ last_name: 'Last Name 1',
+ birth_date: Date.parse('2020-04-24')
+
+ people_query.nql('full_name = Name 1 Last Name 1')
+ .all
+ .must_equal [person_expected]
+ end
+
end
end
end
\ No newline at end of file