Skip to content

Commit

Permalink
Chore / Test file encoding conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jibidus committed Sep 1, 2017
1 parent d1e7bc0 commit 38ec6aa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ $ bundle exec rake test:db:create
```
This will connect to `localhost` PostgreSQL database without user (see `config/database.postgres.yml`) and create a new database dedicated to tests.

*Warning:* database instance have to allow database creation with `UTF-8` encoding.

Finally, you can run all tests with RSpec like this:

```sh
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace :test do
when :postgres
require 'pg'
db.connect 'postgres'
ActiveRecord::Base.connection.execute "CREATE DATABASE #{db.name}"
ActiveRecord::Base.connection.execute "CREATE DATABASE #{db.name} ENCODING='UTF-8'"

when :mysql
require 'mysql2'
Expand Down
25 changes: 20 additions & 5 deletions spec/csv_fast_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@
end

describe 'with custom file encoding', skip_mysql: true do
before do
file = write_file [ %w(name id), %w(trépassé 10) ], encoding: 'ISO-8859-1'
CsvFastImporter.import file, encoding: 'ISO-8859-1'
let(:file) { write_file [ %w(name id), %w(trépassé 10) ], encoding: 'ISO-8859-1' }

context 'with CSVFastImporter custom encoding' do
before do
CsvFastImporter.import file, encoding: 'ISO-8859-1'
end

it 'must import with correct encoding' do
db.query('SELECT name FROM knights').to_s.should eql 'trépassé'
end
end

it 'must import with correct encoding' do
db.query('SELECT name FROM knights').to_s.should eql 'trépassé'
context 'with File encoding conversion' do
before do
file_with_conversion = File.new file.path, internal_encoding: 'UTF-8',
external_encoding: 'ISO-8859-1'
CsvFastImporter.import file_with_conversion
end

it 'must import with correct encoding' do
db.query('SELECT name FROM knights').to_s.should eql 'trépassé'
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/csv_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create(content, options = {})
csv << line
end
end
File.new new_file
File.new new_file, options
end

def new_temp_folder
Expand Down

0 comments on commit 38ec6aa

Please sign in to comment.