# 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) .to contain_exactly( ['lotr', '03', 16, 'finished'], ['king kong', '42', 128, 'not required'], ['the hobbit', '40', 32, 'finished'], ['the hobbit', '01', 64, 'scheduled'] ) end end describe 'query with select and filter' do subject do super() .select(:project, :shot, :version, :status) .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') ) ) .execute end it 'returns the correct data' do expect(subject) .to contain_exactly( ['the hobbit', 45.0], ['lotr', 15.0], ['the hobbit', 22.8] ) end end end