2019-07-10 21:46:42 +12:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
RSpec.describe AceOfBase::Query do
|
|
|
|
let(:tmpdir) { Dir.mktmpdir }
|
|
|
|
let(:storage) { AceOfBase::Storage.new(tmpdir) }
|
|
|
|
subject { described_class.new(storage) }
|
|
|
|
|
|
|
|
around do |example|
|
|
|
|
# Import our fixture file.
|
|
|
|
path = File.expand_path('../fixtures/sample_file.txt', __dir__)
|
|
|
|
file = File.open(path, 'r')
|
|
|
|
importer = AceOfBase::FileImporter.import(file)
|
|
|
|
importer.records.each { |record| storage.store(record) }
|
|
|
|
|
|
|
|
# Run the example
|
|
|
|
example.run
|
|
|
|
|
|
|
|
# Clean up.
|
|
|
|
FileUtils.remove_entry(tmpdir, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'query with select and order by' do
|
|
|
|
subject do
|
|
|
|
super()
|
|
|
|
.select(:project, :shot, :version, :status)
|
|
|
|
.order_by(:finish_date, :internal_bid)
|
|
|
|
.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct data' do
|
|
|
|
expect(subject)
|
2019-07-12 09:03:28 +12:00
|
|
|
.to contain_exactly(
|
|
|
|
['lotr', '03', 16, 'finished'],
|
|
|
|
['king kong', '42', 128, 'not required'],
|
|
|
|
['the hobbit', '40', 32, 'finished'],
|
|
|
|
['the hobbit', '01', 64, 'scheduled']
|
|
|
|
)
|
2019-07-10 21:46:42 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'query with select and filter' do
|
|
|
|
subject do
|
|
|
|
super()
|
|
|
|
.select(:project, :shot, :version, :status)
|
2019-07-12 09:03:28 +12:00
|
|
|
.filter(described_class::Eq.new(finish_date: Date.civil(2006, 7, 22)))
|
|
|
|
.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct data' do
|
|
|
|
expect(subject).to contain_exactly(['king kong', '42', 128, 'not required'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'group and aggregate functions' do
|
|
|
|
subject do
|
|
|
|
super()
|
|
|
|
.select(:project, :internal_bid, :shot)
|
|
|
|
.aggregate(internal_bid: :sum, shot: :collect)
|
|
|
|
.group_by(:project)
|
|
|
|
.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct data' do
|
|
|
|
expect(subject)
|
|
|
|
.to contain_exactly(
|
|
|
|
['the hobbit', 67.8, %w[01 40]],
|
|
|
|
['king kong', 30.0, ['42']],
|
|
|
|
['lotr', 15.0, ['03']]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'advanced filter functions' do
|
|
|
|
subject do
|
|
|
|
super()
|
|
|
|
.select(:project, :internal_bid)
|
|
|
|
.filter(
|
|
|
|
described_class::Or.new(
|
|
|
|
described_class::Eq.new(project: 'the hobbit'),
|
|
|
|
described_class::Eq.new(project: 'lotr')
|
|
|
|
)
|
|
|
|
)
|
2019-07-10 21:46:42 +12:00
|
|
|
.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct data' do
|
|
|
|
expect(subject)
|
2019-07-12 09:03:28 +12:00
|
|
|
.to contain_exactly(
|
|
|
|
['the hobbit', 45.0],
|
|
|
|
['lotr', 15.0],
|
|
|
|
['the hobbit', 22.8]
|
|
|
|
)
|
2019-07-10 21:46:42 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|