File: time_zone_querying_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (55 lines) | stat: -rw-r--r-- 1,477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe 'Time zone querying' do
  let(:collection) { authorized_client[:time_zone_querying] }

  before do
    collection.delete_many
    collection.insert_many([
      {id: 1, created_at: Time.utc(2020, 10, 1, 23)},
      {id: 2, created_at: Time.utc(2020, 10, 2, 0)},
      {id: 3, created_at: Time.utc(2020, 10, 2, 1)},
    ])
  end

  context 'UTC time' do
    let(:time) { Time.utc(2020, 10, 1, 23, 22) }

    it 'finds correctly' do
      view = collection.find({created_at: {'$gt' => time}})
      expect(view.count).to eq(2)
      expect(view.map { |doc| doc[:id] }.sort).to eq([2, 3])
    end
  end

  context 'local time with zone' do
    let(:time) { Time.parse('2020-10-01T19:30:00-0500') }

    it 'finds correctly' do
      view = collection.find({created_at: {'$gt' => time}})
      expect(view.count).to eq(1)
      expect(view.first[:id]).to eq(3)
    end
  end

  context 'when ActiveSupport support is enabled' do
    before do
      unless SpecConfig.instance.active_support?
        skip "ActiveSupport support is not enabled"
      end
    end

    context 'ActiveSupport::TimeWithZone' do
      let(:time) { Time.parse('2020-10-01T19:30:00-0500').in_time_zone('America/New_York') }

      it 'finds correctly' do
        view = collection.find({created_at: {'$gt' => time}})
        expect(view.count).to eq(1)
        expect(view.first[:id]).to eq(3)
      end
    end
  end
end