-
Notifications
You must be signed in to change notification settings - Fork 654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better number normalization fixes #2419 #4530
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,50 @@ | ||
# encoding: utf-8 | ||
require 'rspec/core' | ||
require 'rspec/expectations' | ||
require 'rspec/mocks' | ||
require_relative '../../spec_helper' | ||
|
||
require_relative '../../rspec_configuration' | ||
require_relative '../../../app/models/table/column_typecaster' | ||
|
||
describe CartoDB::ColumnTypecaster do | ||
before do | ||
quota_in_bytes = 524288000 | ||
table_quota = 500 | ||
@user = create_user( | ||
quota_in_bytes: quota_in_bytes, | ||
table_quota: table_quota | ||
) | ||
end | ||
|
||
after do | ||
@user.destroy | ||
end | ||
describe '#pg_replace_expression' do | ||
before(:all) do | ||
@column_typecaster = CartoDB::ColumnTypecaster.new({ | ||
user_database: nil, | ||
schema: nil, | ||
table_name: nil, | ||
column_name: nil, | ||
new_type: nil | ||
}) | ||
end | ||
|
||
pending 'raises NonConvertibleData when trying to cast a non-supported time format to date' do | ||
@user.in_database { |database| @db = database } | ||
it 'takes a column_name and a set of replacements and returns a postgres expression that implements them' do | ||
# e.g.: as it if where in spanish locale | ||
thousand_separator = '.' | ||
decimal_separator = ',' | ||
|
||
table_name = "test_#{rand(999)}" | ||
schema = @user.database_schema | ||
dataset = @db[table_name.to_sym] | ||
time_text = 'Mon Oct 13 1997 15:32:18 GMT+0200 (CEST)' | ||
# order matters, the innermost expression will be applied first | ||
replacements = [ | ||
[thousand_separator, ''], | ||
[decimal_separator, '.'], | ||
['$', ''] | ||
] | ||
|
||
@db.create_table?(table_name) do | ||
String :time_with_timezone | ||
@column_typecaster.send(:pg_replace_expression, 'my_column', replacements) | ||
.should == %Q{replace(replace(replace("my_column", '.', ''), ',', '.'), '$', '')} | ||
end | ||
|
||
5.times { dataset.insert(time_with_timezone: time_text) } | ||
|
||
typecaster = CartoDB::ColumnTypecaster.new( | ||
user_database: @db, | ||
schema: schema, | ||
table_name: table_name, | ||
column_name: :time_with_timezone, | ||
new_type: 'date' | ||
) | ||
it 'returns an expression quasi-equivalent to the previous implementation' do | ||
# us locale | ||
thousand_separator = ',' | ||
decimal_separator = '.' | ||
|
||
expect { typecaster.run }.to raise_error CartoDB::NonConvertibleData | ||
dataset.first.fetch(:time_with_timezone).should_not be_nil | ||
replacements = [ | ||
[thousand_separator, ''], | ||
[decimal_separator, '.'] | ||
] | ||
|
||
@db.drop_table?(table_name) | ||
@column_typecaster.send(:pg_replace_expression, 'my_column', replacements) | ||
.should == %Q{replace(replace("my_column", ',', ''), '.', '.')} | ||
end | ||
end | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I basically reused the spec file: the only test was not in the Makefile and was not recoverable so I wrote a useful one and got rid of the rest.