-
Notifications
You must be signed in to change notification settings - Fork 1
/
article_spec.rb
130 lines (110 loc) · 4.35 KB
/
article_spec.rb
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#
# The goal with this test suite is not to have test coverage for every edge case in all Ruby and ActiveSupport's
# methods but rahter verify that the examples in the article are still up-to-date. The below specification could
# also be used to better understand the examples in the article.
#
# Feel free to fork and add more test cases that suites your use case. If you think your addition is benificial
# for others please consider sending a pull request!
#
# Configured time zone is "Kabul" which is +04:30 ahead of UTC.
# Time is by default frozen at "2014-03-15T23:31:11+05:45" (Kathmandu time zone)
#
describe Article, frozen: "2014-03-15T23:31:11+05:45" do
describe ActiveRecord::Base do
describe ".where" do
it "converts times to UTC database format" do
created_at = Time.zone.parse("2014-03-16T23:32:11+06:00")
article = Article.create!(created_at: created_at.in_time_zone("Adelaide"))
expect(Article.where(created_at: created_at.in_time_zone("Chatham Is.")).first!).to eq(article)
end
end
describe "#created_at" do
let!(:article) { Article.create! }
it "retuns the time the article was created in Kabul time" do
expect(article.created_at.iso8601).to eq("2014-03-15T22:16:11+04:30")
end
it "remains the same when fetch from the database" do
expect(Article.find(article.id).created_at.iso8601).to eq("2014-03-15T22:16:11+04:30")
end
it "is stored as UTC" do
expect(Article.find(article.id).created_at_before_type_cast).to eq("2014-03-15 17:46:11.000000")
end
it "can be updated with any time zone time" do
article.update_attributes!(created_at: "2014-03-16T23:32:11+06:00")
expect(Article.find(article.id).created_at.iso8601).to eq("2014-03-16T22:02:11+04:30")
end
end
end
describe ActiveSupport::TimeZone do
describe "#parse" do
it "returns the time that the string represents in Kabul time" do
expect(Time.zone.parse("2014-03-16T23:32:11+06:00").iso8601).to eq("2014-03-16T22:02:11+04:30")
end
end
describe "#now" do
it "returns what the time is now in the configured time zone" do
expect(Time.zone.now.iso8601).to eq("2014-03-15T22:16:11+04:30")
end
end
describe "#today" do
it "returns what the date is now in the configured time zone" do
expect(Time.zone.today.iso8601).to eq("2014-03-15")
end
end
end
describe ActiveSupport::Duration do
describe "#ago" do
it "returns the time what the time was 2 minutes ago in Kabul time" do
expect(2.minutes.ago.iso8601).to eq("2014-03-15T22:14:11+04:30")
end
end
end
describe Date do
describe "#in_time_zone" do
it "returns the date as a time in the current time zone" do
expect(Date.today.in_time_zone.iso8601).to eq("2014-03-15T00:00:00+04:30")
end
it "can return the date as a time in in a given time zone" do
expect(Date.today.in_time_zone("Adelaide").iso8601).to eq("2014-03-15T00:00:00+10:30")
end
end
end
describe Time do
describe ".strptime" do
it "parses the given time string using the supplied format" do
parsed = Time.strptime("2014-03-16T23:32:11+06:00", "%Y-%m-%dT%H:%M:%S%z")
expect(parsed.in_time_zone.iso8601).to eq("2014-03-16T22:02:11+04:30")
end
it "raises error if passed in string doesn't follow the format" do
expect { Time.strptime("201403-16T23:32:11+06:00", "%Y-%m-%dT%H:%M:%S%z") }.to raise_error(ArgumentError)
end
end
describe ".zone" do
it "returns the currently configured time zone" do
expect(Time.zone.name).to eq("Kabul")
end
end
describe ".use_zone" do
it "changes the default time zone within the block" do
Time.use_zone("Adelaide") do
expect(Time.zone.name).to eq("Adelaide")
end
end
end
describe "#current" do
it "returns what the time is now in the configured time zone" do
expect(Time.current.iso8601).to eq("2014-03-15T22:16:11+04:30")
end
end
describe "#utc" do
it "returns the time in the UTC time zone" do
expect(Time.current.utc.iso8601).to eq("2014-03-15T17:46:11Z")
end
end
describe "#strptime" do
it "doesn't exist" do
expect { Time.current.strptime }.to raise_error(NoMethodError)
end
end
end
end