Skip to content

Commit 004dde7

Browse files
authored
MONGOID-5262 Implement local time zone override for tests (#5480)
1 parent e9674a2 commit 004dde7

File tree

7 files changed

+29
-121
lines changed

7 files changed

+29
-121
lines changed

spec/mongoid/criteria/queryable/extensions/string_spec.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@
7373

7474
context "when using active support's time zone" do
7575
config_override :use_activesupport_time_zone, true
76-
77-
before do
78-
::Time.zone = "Tokyo"
79-
end
76+
time_zone_override "Tokyo"
8077

8178
let(:date) do
8279
"2010-01-01 5:00:00"
@@ -93,10 +90,7 @@
9390

9491
context "when not using active support's time zone" do
9592
config_override :use_activesupport_time_zone, false
96-
97-
before do
98-
::Time.zone = nil
99-
end
93+
time_zone_override nil
10094

10195
let(:date) do
10296
"2010-01-01 5:00:00"

spec/mongoid/extensions/time_spec.rb

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,7 @@
5050

5151
context "when using the ActiveSupport time zone" do
5252
config_override :use_activesupport_time_zone, true
53-
54-
before do
55-
Time.zone = "Stockholm"
56-
end
57-
58-
after do
59-
Time.zone = nil
60-
end
53+
time_zone_override "Stockholm"
6154

6255
context "when demongoizing a Time" do
6356

@@ -123,19 +116,12 @@
123116

124117
context "when using the ActiveSupport time zone" do
125118
config_override :use_activesupport_time_zone, true
119+
time_zone_override "Stockholm"
126120

127121
let(:time) do
128122
Time.utc(2010, 11, 19, 0, 30)
129123
end
130124

131-
before do
132-
Time.zone = "Stockholm"
133-
end
134-
135-
after do
136-
Time.zone = nil
137-
end
138-
139125
it "returns utc" do
140126
expect(Time.demongoize(time)).to eq(
141127
ActiveSupport::TimeZone['UTC'].local(2010, 11, 19, 0, 30)
@@ -855,20 +841,13 @@
855841

856842
context "when using the ActiveSupport time zone" do
857843
config_override :use_activesupport_time_zone, true
844+
# if this is actually your time zone, the following tests are useless
845+
time_zone_override "Stockholm"
858846

859847
let(:datetime) do
860848
DateTime.new(2010, 11, 19)
861849
end
862850

863-
before do
864-
# if this is actually your time zone, the following tests are useless
865-
Time.zone = "Stockholm"
866-
end
867-
868-
after do
869-
Time.zone = nil
870-
end
871-
872851
it "assumes the given time is local" do
873852
expect(Time.mongoize(datetime)).to eq(
874853
Time.utc(2010, 11, 19)
@@ -945,20 +924,13 @@
945924

946925
context "when using the ActiveSupport time zone" do
947926
config_override :use_activesupport_time_zone, true
927+
# if this is actually your time zone, the following tests are useless
928+
time_zone_override "Stockholm"
948929

949930
let(:date) do
950931
Date.new(2010, 11, 19)
951932
end
952933

953-
before do
954-
# if this is actually your time zone, the following tests are useless
955-
Time.zone = "Stockholm"
956-
end
957-
958-
after do
959-
Time.zone = nil
960-
end
961-
962934
it "assumes the given time is local" do
963935
expect(Time.mongoize(date)).to eq(Time.utc(2010, 11, 18, 23))
964936
end
@@ -977,15 +949,8 @@
977949

978950
context "when using the ActiveSupport time zone" do
979951
config_override :use_activesupport_time_zone, true
980-
981-
before do
982-
# if this is actually your time zone, the following tests are useless
983-
Time.zone = "Stockholm"
984-
end
985-
986-
after do
987-
Time.zone = nil
988-
end
952+
# if this is actually your time zone, the following tests are useless
953+
time_zone_override "Stockholm"
989954

990955
it "assumes the given time is local" do
991956
expect(Time.mongoize(array)).to eq(

spec/mongoid/extensions/time_with_zone_spec.rb

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,7 @@
5050

5151
context "when using the ActiveSupport time zone" do
5252
config_override :use_activesupport_time_zone, true
53-
54-
before do
55-
Time.zone = "Stockholm"
56-
end
57-
58-
after do
59-
Time.zone = nil
60-
end
53+
time_zone_override "Stockholm"
6154

6255
it "returns an ActiveSupport::TimeWithZone" do
6356
expect(ActiveSupport::TimeWithZone.demongoize(time).class).to eq(ActiveSupport::TimeWithZone)
@@ -113,19 +106,12 @@
113106

114107
context "when using the ActiveSupport time zone" do
115108
config_override :use_activesupport_time_zone, true
109+
time_zone_override "Stockholm"
116110

117111
let(:time) do
118112
Time.utc(2010, 11, 19, 0, 30)
119113
end
120114

121-
before do
122-
Time.zone = "Stockholm"
123-
end
124-
125-
after do
126-
Time.zone = nil
127-
end
128-
129115
it "returns utc" do
130116
expect(ActiveSupport::TimeWithZone.demongoize(time)).to eq(
131117
ActiveSupport::TimeZone['UTC'].local(2010, 11, 19, 0, 30)
@@ -203,15 +189,8 @@
203189

204190
context "when using the ActiveSupport time zone" do
205191
config_override :use_activesupport_time_zone, true
206-
207-
before do
208-
# if this is actually your time zone, the following tests are useless
209-
Time.zone = "Stockholm"
210-
end
211-
212-
after do
213-
Time.zone = nil
214-
end
192+
# if this is actually your time zone, the following tests are useless
193+
time_zone_override "Stockholm"
215194

216195
context "when the local time is not observing daylight saving" do
217196

@@ -247,20 +226,12 @@
247226

248227
context "when using the ActiveSupport time zone" do
249228
config_override :use_activesupport_time_zone, true
229+
time_zone_override "Stockholm"
250230

251231
let(:datetime) do
252232
DateTime.new(2010, 11, 19)
253233
end
254234

255-
before do
256-
# if this is actually your time zone, the following tests are useless
257-
Time.zone = "Stockholm"
258-
end
259-
260-
after do
261-
Time.zone = nil
262-
end
263-
264235
it "assumes the given time is local" do
265236
expect(ActiveSupport::TimeWithZone.mongoize(datetime)).to eq(
266237
Time.utc(2010, 11, 19)
@@ -317,20 +288,12 @@
317288

318289
context "when using the ActiveSupport time zone" do
319290
config_override :use_activesupport_time_zone, true
291+
time_zone_override "Stockholm"
320292

321293
let(:date) do
322294
Date.new(2010, 11, 19)
323295
end
324296

325-
before do
326-
# if this is actually your time zone, the following tests are useless
327-
Time.zone = "Stockholm"
328-
end
329-
330-
after do
331-
Time.zone = nil
332-
end
333-
334297
it "assumes the given time is local" do
335298
expect(ActiveSupport::TimeWithZone.mongoize(date)).to eq(Time.utc(2010, 11, 18, 23))
336299
end
@@ -349,15 +312,7 @@
349312

350313
context "when using the ActiveSupport time zone" do
351314
config_override :use_activesupport_time_zone, true
352-
353-
before do
354-
# if this is actually your time zone, the following tests are useless
355-
Time.zone = "Stockholm"
356-
end
357-
358-
after do
359-
Time.zone = nil
360-
end
315+
time_zone_override "Stockholm"
361316

362317
it "assumes the given time is local" do
363318
expect(ActiveSupport::TimeWithZone.mongoize(array)).to eq(

spec/mongoid/fields_spec.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,14 +1082,7 @@
10821082
end
10831083

10841084
context "when reading the field" do
1085-
1086-
before do
1087-
Time.zone = "Berlin"
1088-
end
1089-
1090-
after do
1091-
Time.zone = nil
1092-
end
1085+
time_zone_override "Berlin"
10931086

10941087
it "performs the necessary time conversions" do
10951088
expect(person.lunch_time.to_s).to eq(time.getlocal.to_s)

spec/mongoid/findable_spec.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,10 +901,7 @@
901901
context 'when Mongoid is configured to use activesupport time zone' do
902902
config_override :use_utc, false
903903
config_override :use_activesupport_time_zone, true
904-
905-
before do
906-
Time.zone = "Asia/Kolkata"
907-
end
904+
time_zone_override "Asia/Kolkata"
908905

909906
let!(:time) do
910907
Time.zone.now.tap do |t|

spec/mongoid/timestamps_spec.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717

1818
let(:time_zone) { "Pacific Time (US & Canada)" }
1919

20-
around do |ex|
21-
z = Time.zone
22-
Time.zone = time_zone
23-
ex.run
24-
Time.zone = z
25-
end
20+
time_zone_override "Pacific Time (US & Canada)"
2621

2722
before do
2823
document.run_callbacks(:create)

spec/support/macros.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ def persistence_context_override(component, value)
102102
end
103103
end
104104

105+
def time_zone_override(tz)
106+
around do |example|
107+
old_tz = Time.zone
108+
Time.zone = tz
109+
example.run
110+
Time.zone = old_tz
111+
end
112+
end
113+
105114
def with_default_i18n_configs
106115
around do |example|
107116
I18n.locale = :en

0 commit comments

Comments
 (0)