|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe ActiveRecord::Bitemporal::Relation do |
| 6 | + describe "#exists?" do |
| 7 | + def create_employees_with_history |
| 8 | + Employee |
| 9 | + .create!([{ name: "Jane" }, { name: "Tom" } ]) |
| 10 | + .each { |e| e.update!(updated_at: Time.current) } |
| 11 | + end |
| 12 | + |
| 13 | + # Assuming that the number of records will not reach the maximum value in tests. |
| 14 | + # employees.bitemporal_id is a 4-byte signed integer. |
| 15 | + MAX_BITEMPORAL_ID = 1 << 31 - 1 |
| 16 | + |
| 17 | + context "on BTDM" do |
| 18 | + let(:employees) { create_employees_with_history } |
| 19 | + |
| 20 | + it "finds existing bitemporal IDs" do |
| 21 | + first_employee, second_employee = *employees |
| 22 | + |
| 23 | + expect(Employee.exists?(first_employee.bitemporal_id)).to eq true |
| 24 | + expect(Employee.exists?(second_employee.bitemporal_id)).to eq true |
| 25 | + expect(Employee.exists?(MAX_BITEMPORAL_ID)).to eq false |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + context "on relation" do |
| 30 | + let(:employees) { create_employees_with_history } |
| 31 | + |
| 32 | + it "finds existing bitemporal IDs" do |
| 33 | + first_employee, second_employee = *employees |
| 34 | + |
| 35 | + expect(Employee.all.exists?(first_employee.bitemporal_id)).to eq true |
| 36 | + expect(Employee.all.exists?(second_employee.bitemporal_id)).to eq true |
| 37 | + expect(Employee.all.exists?(MAX_BITEMPORAL_ID)).to eq false |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context "on loaded relation" do |
| 42 | + let(:employees) { create_employees_with_history } |
| 43 | + |
| 44 | + it "finds existing bitemporal IDs" do |
| 45 | + first_employee, second_employee = *employees |
| 46 | + |
| 47 | + expect(Employee.all.load.exists?(first_employee.bitemporal_id)).to eq true |
| 48 | + expect(Employee.all.load.exists?(second_employee.bitemporal_id)).to eq true |
| 49 | + expect(Employee.all.load.exists?(MAX_BITEMPORAL_ID)).to eq false |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + context "with eager loading by includes" do |
| 54 | + let(:company) { |
| 55 | + Company |
| 56 | + .create!(name: "Company") |
| 57 | + .tap { |c| c.employees << create_employees_with_history } |
| 58 | + } |
| 59 | + |
| 60 | + it "finds existing bitemporal IDs" do |
| 61 | + first_employee, second_employee = *company.employees |
| 62 | + |
| 63 | + expect(Employee.includes(:company).exists?(first_employee.bitemporal_id)).to eq true |
| 64 | + expect(Employee.includes(:company).exists?(second_employee.bitemporal_id)).to eq true |
| 65 | + expect(Employee.includes(:company).exists?(MAX_BITEMPORAL_ID)).to eq false |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context "on association" do |
| 70 | + let(:company) { |
| 71 | + Company |
| 72 | + .create!(name: "Company") |
| 73 | + .tap { |c| c.employees << create_employees_with_history } |
| 74 | + } |
| 75 | + |
| 76 | + it "finds existing bitemporal IDs" do |
| 77 | + first_employee, second_employee = *company.employees |
| 78 | + |
| 79 | + expect(company.employees.reset.exists?(first_employee.bitemporal_id)).to eq true |
| 80 | + expect(company.employees.reset.exists?(second_employee.bitemporal_id)).to eq true |
| 81 | + expect(company.employees.reset.exists?(MAX_BITEMPORAL_ID)).to eq false |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | +end |
0 commit comments