Skip to content

Commit 579d57c

Browse files
committed
Fix date formats in other language tests.
1 parent e530d4b commit 579d57c

File tree

4 files changed

+46
-37
lines changed

4 files changed

+46
-37
lines changed

lib/active_record/connection_adapters/sqlserver/quoting.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ def unquoted_false
4545
end
4646

4747
def quoted_date(value)
48-
if value.acts_like?(:time) && value.respond_to?(:usec)
49-
precision = (BigDecimal(value.usec.to_s) / 1_000_000).round(3).to_s.split('.').last
50-
"#{super}.#{precision}"
51-
elsif value.acts_like?(:date)
52-
value.to_s(:_sqlserver_dateformat)
53-
else
54-
super
48+
SQLServer::Utils.with_sqlserver_db_date_formats do
49+
if value.acts_like?(:time) && value.respond_to?(:usec)
50+
precision = (BigDecimal(value.usec.to_s) / 1_000_000).round(3).to_s.split('.').last
51+
"#{super}.#{precision}"
52+
elsif value.acts_like?(:date)
53+
value.to_s(:_sqlserver_dateformat)
54+
else
55+
super
56+
end
5557
end
5658
end
5759

lib/active_record/connection_adapters/sqlserver/type/datetime.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def cast_value(value)
1616
end
1717

1818
def cast_usec(value)
19-
return 0 if value.usec.zero?
19+
return 0 if !value.respond_to?(:usec) || value.usec.zero?
2020
seconds = value.usec.to_f / 1_000_000.0
2121
ss_seconds = ((seconds * (1 / second_precision)).round / (1 / second_precision)).round(3)
2222
(ss_seconds * 1_000_000).to_i

lib/active_record/connection_adapters/sqlserver/utils.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ def extract_identifiers(name)
112112
SQLServer::Utils::Name.new(name)
113113
end
114114

115+
def with_sqlserver_db_date_formats
116+
old_db_format_date = Date::DATE_FORMATS[:db]
117+
old_db_format_time = Time::DATE_FORMATS[:db]
118+
date_format = Date::DATE_FORMATS[:_sqlserver_dateformat]
119+
Date::DATE_FORMATS[:db] = "#{date_format}"
120+
Time::DATE_FORMATS[:db] = "#{date_format} %H:%M:%S"
121+
yield
122+
ensure
123+
Date::DATE_FORMATS[:db] = old_db_format_date
124+
Time::DATE_FORMATS[:db] = old_db_format_time
125+
end
126+
115127
end
116128
end
117129
end

test/cases/adapter_test_sqlserver.rb

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'cases/helper_sqlserver'
22
require 'models/topic'
3+
require 'models/task'
34

4-
# require 'models/task'
55
# require 'models/reply'
66
# require 'models/joke'
77
# require 'models/subscriber'
@@ -18,15 +18,12 @@
1818

1919
class AdapterTestSQLServer < ActiveRecord::TestCase
2020

21-
# fixtures :tasks, :posts
22-
2321
let(:connection) { ActiveRecord::Base.connection }
2422

25-
before do
26-
@basic_insert_sql = "INSERT INTO [funny_jokes] ([name]) VALUES('Knock knock')"
27-
@basic_update_sql = "UPDATE [customers] SET [address_street] = NULL WHERE [id] = 2"
28-
@basic_select_sql = "SELECT * FROM [customers] WHERE ([customers].[id] = 1)"
29-
end
23+
let(:basic_insert_sql) { "INSERT INTO [funny_jokes] ([name]) VALUES('Knock knock')" }
24+
let(:basic_update_sql) { "UPDATE [customers] SET [address_street] = NULL WHERE [id] = 2" }
25+
let(:basic_select_sql) { "SELECT * FROM [customers] WHERE ([customers].[id] = 1)" }
26+
3027

3128
it 'has basic and non-senstive information in the adpaters inspect method' do
3229
string = connection.inspect
@@ -52,40 +49,36 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
5249
assert_equal 'SQLServer', connection.adapter_name
5350
end
5451

55-
it 'support migrations' do
52+
it 'supports migrations' do
5653
assert connection.supports_migrations?
5754
end
5855

5956
it 'support DDL in transactions' do
6057
assert connection.supports_ddl_transactions?
6158
end
6259

63-
it 'allow owner table name prefixs like dbo. to still allow table_exists? to return true' do
60+
it 'allow owner table name prefixs like dbo to still allow table exists to return true' do
6461
begin
65-
assert_equal 'tasks', Task.table_name
66-
assert Task.table_exists?
67-
Task.table_name = 'dbo.tasks'
68-
assert Task.table_exists?, 'Tasks table name of dbo.tasks should return true for exists.'
62+
assert_equal 'topics', Topic.table_name
63+
assert Topic.table_exists?
64+
Topic.table_name = 'dbo.topics'
65+
assert Topic.table_exists?, 'Tasks table name of dbo.topics should return true for exists.'
6966
ensure
70-
Task.table_name = 'tasks'
67+
Topic.table_name = 'topics'
7168
end
7269
end
7370

74-
it 'return true to #insert_sql? for inserts only' do
71+
it 'return true to insert sql query for inserts only' do
7572
assert connection.send(:insert_sql?,'INSERT...')
7673
assert connection.send(:insert_sql?, "EXEC sp_executesql N'INSERT INTO [fk_test_has_fks] ([fk_id]) VALUES (@0); SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident', N'@0 int', @0 = 0")
7774
assert !connection.send(:insert_sql?,'UPDATE...')
7875
assert !connection.send(:insert_sql?,'SELECT...')
7976
end
8077

81-
describe 'for #get_table_name' do
82-
83-
it 'return quoted table name from basic INSERT, UPDATE and SELECT statements' do
84-
assert_equal '[funny_jokes]', connection.send(:get_table_name,@basic_insert_sql)
85-
assert_equal '[customers]', connection.send(:get_table_name,@basic_update_sql)
86-
assert_equal '[customers]', connection.send(:get_table_name,@basic_select_sql)
87-
end
88-
78+
it 'return quoted table name from basic INSERT UPDATE and SELECT statements' do
79+
assert_equal '[funny_jokes]', connection.send(:get_table_name, basic_insert_sql)
80+
assert_equal '[customers]', connection.send(:get_table_name, basic_update_sql)
81+
assert_equal '[customers]', connection.send(:get_table_name, basic_select_sql)
8982
end
9083

9184
describe 'with different language' do
@@ -99,22 +92,24 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
9992
connection.send :initialize_dateformatter
10093
end
10194

102-
it 'memoize users dateformat' do
95+
it 'memos users dateformat' do
10396
connection.execute("SET LANGUAGE us_english") rescue nil
10497
dateformat = connection.instance_variable_get(:@database_dateformat)
10598
assert_equal 'mdy', dateformat
10699
end
107100

108-
it 'have a dateformatter' do
101+
it 'has a dateformatter' do
109102
assert Date::DATE_FORMATS[:_sqlserver_dateformat]
110103
assert Time::DATE_FORMATS[:_sqlserver_dateformat]
111104
end
112105

113-
it 'do a date insertion when language is german' do
106+
it 'does a datetime insertion when language is german' do
114107
connection.execute("SET LANGUAGE deutsch")
115108
connection.send :initialize_dateformatter
116109
assert_nothing_raised do
117-
Task.create(starting: Time.utc(2000, 1, 31, 5, 42, 0), ending: Date.new(2006, 12, 31))
110+
starting = Time.utc(2000, 1, 31, 5, 42, 0)
111+
ending = Date.new(2006, 12, 31)
112+
Task.create! starting: starting, ending: ending
118113
end
119114
end
120115

@@ -171,7 +166,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
171166
end
172167

173168
it 'return false to #query_requires_identity_insert? for normal SQL' do
174-
[@basic_insert_sql, @basic_update_sql, @basic_select_sql].each do |sql|
169+
[basic_insert_sql, basic_update_sql, basic_select_sql].each do |sql|
175170
assert !connection.send(:query_requires_identity_insert?,sql), "SQL was #{sql}"
176171
end
177172
end

0 commit comments

Comments
 (0)