|
6 | 6 | RSpec.describe 'Export action', type: :request do |
7 | 7 | subject { page } |
8 | 8 |
|
9 | | - before do |
10 | | - Comment.all.collect(&:destroy) # rspec bug => doesn't get destroyed with transaction |
11 | | - |
12 | | - @players = FactoryBot.create_list(:player, 4) |
13 | | - @player = @players.first |
14 | | - @player.team = FactoryBot.create :team |
15 | | - @player.draft = FactoryBot.create :draft |
16 | | - @player.comments = (@comments = Array.new(2) { FactoryBot.create(:comment) }) |
17 | | - @player.save |
18 | | - |
19 | | - @abstract_model = RailsAdmin::AbstractModel.new(Player) |
20 | | - |
21 | | - # removed schema=>only=>created_at |
22 | | - @non_default_schema = { |
23 | | - 'only' => [PK_COLUMN.to_s, 'updated_at', 'deleted_at', 'name', 'position', 'number', 'retired', 'injured', 'born_on', 'notes', 'suspended'], |
24 | | - 'include' => { |
25 | | - 'team' => {'only' => [PK_COLUMN.to_s, 'created_at', 'updated_at', 'name', 'logo_url', 'manager', 'ballpark', 'mascot', 'founded', 'wins', 'losses', 'win_percentage', 'revenue', 'color']}, |
26 | | - 'draft' => {'only' => [PK_COLUMN.to_s, 'created_at', 'updated_at', 'date', 'round', 'pick', 'overall', 'college', 'notes']}, |
27 | | - 'comments' => {'only' => [PK_COLUMN.to_s, 'content', 'created_at', 'updated_at']}, |
28 | | - }, |
29 | | - } |
30 | | - end |
31 | | - |
32 | | - it 'allows to export to CSV with associations and default schema, containing properly translated header and follow configuration' do |
33 | | - RailsAdmin.config do |c| |
34 | | - c.model Player do |
35 | | - include_all_fields |
36 | | - field :name do |
37 | | - export_value do |
38 | | - "#{value} exported" |
39 | | - end |
40 | | - end |
41 | | - |
42 | | - field :json_field, :json do |
43 | | - formatted_value do |
44 | | - '{}' |
45 | | - end |
46 | | - end |
47 | | - end |
48 | | - end |
| 9 | + let!(:player) { FactoryBot.create(:player) } |
49 | 10 |
|
| 11 | + it 'exports to CSV' do |
50 | 12 | visit export_path(model_name: 'player') |
51 | | - is_expected.to have_content 'Select fields to export' |
52 | | - select "<comma> ','", from: 'csv_options_generator_col_sep' |
53 | | - click_button 'Export to csv' |
54 | | - csv = CSV.parse page.driver.response.body.force_encoding('utf-8') # comes through as us-ascii on some platforms |
55 | | - expect(csv[0]).to match_array ['Id', 'Created at', 'Updated at', 'Deleted at', 'Name', 'Position', |
56 | | - 'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Json field', 'Id [Team]', 'Created at [Team]', |
57 | | - 'Updated at [Team]', 'Name [Team]', 'Logo url [Team]', 'Team Manager [Team]', 'Ballpark [Team]', |
58 | | - 'Mascot [Team]', 'Founded [Team]', 'Wins [Team]', 'Losses [Team]', 'Win percentage [Team]', |
59 | | - 'Revenue [Team]', 'Color [Team]', 'Custom field [Team]', 'Main Sponsor [Team]', 'Id [Draft]', 'Created at [Draft]', |
60 | | - 'Updated at [Draft]', 'Date [Draft]', 'Round [Draft]', 'Pick [Draft]', 'Overall [Draft]', |
61 | | - 'College [Draft]', 'Notes [Draft]', 'Id [Comments]', 'Content [Comments]', 'Created at [Comments]', |
62 | | - 'Updated at [Comments]'] |
63 | | - expect(csv.flatten).to include("#{@player.name} exported") |
64 | | - expect(csv.flatten).to include(@player.team.name) |
65 | | - expect(csv.flatten).to include(@player.draft.college) |
66 | | - |
67 | | - expect(csv.flatten.join(' ')).to include(@player.comments.first.content.split("\n").first.strip) |
68 | | - expect(csv.flatten.join(' ')).to include(@player.comments.second.content.split("\n").first.strip) |
| 13 | + click_button 'Export to json' |
| 14 | + is_expected.to have_content player.name |
69 | 15 | end |
70 | 16 |
|
71 | | - it 'allows to export to JSON' do |
| 17 | + it 'exports to JSON' do |
72 | 18 | visit export_path(model_name: 'player') |
73 | 19 | click_button 'Export to json' |
74 | | - is_expected.to have_content @player.team.name |
| 20 | + is_expected.to have_content player.name |
75 | 21 | end |
76 | 22 |
|
77 | | - it 'allows to export to XML' do |
| 23 | + it 'exports to XML' do |
78 | 24 | pending "Mongoid does not support to_xml's :include option" if CI_ORM == :mongoid |
79 | 25 | visit export_path(model_name: 'player') |
80 | 26 | click_button 'Export to xml' |
81 | 27 |
|
82 | | - is_expected.to have_content @player.team.name |
| 28 | + is_expected.to have_content player.name |
83 | 29 | end |
84 | 30 |
|
85 | 31 | it 'works with Turbo Drive enabled', js: true do |
|
88 | 34 | expect { find_button('Export to csv').trigger('click') }.not_to raise_error |
89 | 35 | end |
90 | 36 |
|
91 | | - it 'exports polymorphic fields the easy way for now' do |
92 | | - visit export_path(model_name: 'comment') |
93 | | - select "<comma> ','", from: 'csv_options_generator_col_sep' |
94 | | - click_button 'Export to csv' |
95 | | - csv = CSV.parse page.driver.response.body |
96 | | - expect(csv[0]).to match_array ['Id', 'Commentable', 'Commentable type', 'Content', 'Created at', 'Updated at'] |
97 | | - csv[1..].each do |line| |
98 | | - expect(line[csv[0].index('Commentable')]).to eq(@player.id.to_s) |
99 | | - expect(line[csv[0].index('Commentable type')]).to eq(@player.class.to_s) |
100 | | - end |
101 | | - end |
102 | | - |
103 | 37 | it 'does not break when nothing is checked' do |
104 | 38 | visit export_path(model_name: 'comment') |
105 | 39 | all('input[type="checkbox"]').each(&:uncheck) |
106 | 40 | expect { click_button 'Export to csv' }.not_to raise_error |
107 | 41 | end |
108 | 42 |
|
109 | | - context 'with csv format' do |
110 | | - it 'exports with modified schema' do |
111 | | - page.driver.post(export_path(model_name: 'player', schema: @non_default_schema, csv: true, all: true, csv_options: {generator: {col_sep: ','}})) |
| 43 | + describe 'with associations' do |
| 44 | + let!(:players) { FactoryBot.create_list(:player, 3) } |
| 45 | + let(:team) { FactoryBot.create :team } |
| 46 | + let(:draft) { FactoryBot.create :draft } |
| 47 | + let(:comments) { FactoryBot.create_list(:comment, 2) } |
| 48 | + |
| 49 | + before do |
| 50 | + player.team = team |
| 51 | + player.draft = draft |
| 52 | + player.comments = comments |
| 53 | + player.save |
| 54 | + end |
| 55 | + |
| 56 | + it 'exports to CSV with default schema, containing properly translated header and follow configuration' do |
| 57 | + RailsAdmin.config do |c| |
| 58 | + c.model Player do |
| 59 | + include_all_fields |
| 60 | + field :name do |
| 61 | + export_value do |
| 62 | + "#{value} exported" |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + field :json_field, :json do |
| 67 | + formatted_value do |
| 68 | + '{}' |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + visit export_path(model_name: 'player') |
| 75 | + is_expected.to have_content 'Select fields to export' |
| 76 | + select "<comma> ','", from: 'csv_options_generator_col_sep' |
| 77 | + click_button 'Export to csv' |
| 78 | + csv = CSV.parse page.driver.response.body.force_encoding('utf-8') # comes through as us-ascii on some platforms |
| 79 | + expect(csv[0]).to match_array ['Id', 'Created at', 'Updated at', 'Deleted at', 'Name', 'Position', |
| 80 | + 'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Json field', 'Id [Team]', 'Created at [Team]', |
| 81 | + 'Updated at [Team]', 'Name [Team]', 'Logo url [Team]', 'Team Manager [Team]', 'Ballpark [Team]', |
| 82 | + 'Mascot [Team]', 'Founded [Team]', 'Wins [Team]', 'Losses [Team]', 'Win percentage [Team]', |
| 83 | + 'Revenue [Team]', 'Color [Team]', 'Custom field [Team]', 'Main Sponsor [Team]', 'Id [Draft]', 'Created at [Draft]', |
| 84 | + 'Updated at [Draft]', 'Date [Draft]', 'Round [Draft]', 'Pick [Draft]', 'Overall [Draft]', |
| 85 | + 'College [Draft]', 'Notes [Draft]', 'Id [Comments]', 'Content [Comments]', 'Created at [Comments]', |
| 86 | + 'Updated at [Comments]'] |
| 87 | + expect(csv.flatten).to include("#{player.name} exported") |
| 88 | + expect(csv.flatten).to include(player.team.name) |
| 89 | + expect(csv.flatten).to include(player.draft.college) |
| 90 | + |
| 91 | + expect(csv.flatten.join(' ')).to include(player.comments.first.content.split("\n").first.strip) |
| 92 | + expect(csv.flatten.join(' ')).to include(player.comments.second.content.split("\n").first.strip) |
| 93 | + end |
| 94 | + |
| 95 | + let(:custom_schema) do |
| 96 | + # removed schema=>only=>created_at |
| 97 | + { |
| 98 | + 'only' => [PK_COLUMN.to_s, 'updated_at', 'deleted_at', 'name', 'position', 'number', 'retired', 'injured', 'born_on', 'notes', 'suspended'], |
| 99 | + 'include' => { |
| 100 | + 'team' => {'only' => [PK_COLUMN.to_s, 'created_at', 'updated_at', 'name', 'logo_url', 'manager', 'ballpark', 'mascot', 'founded', 'wins', 'losses', 'win_percentage', 'revenue', 'color']}, |
| 101 | + 'draft' => {'only' => [PK_COLUMN.to_s, 'created_at', 'updated_at', 'date', 'round', 'pick', 'overall', 'college', 'notes']}, |
| 102 | + 'comments' => {'only' => [PK_COLUMN.to_s, 'content', 'created_at', 'updated_at']}, |
| 103 | + }, |
| 104 | + } |
| 105 | + end |
| 106 | + |
| 107 | + it 'exports to CSV with custom schema' do |
| 108 | + page.driver.post(export_path(model_name: 'player', schema: custom_schema, csv: true, all: true, csv_options: {generator: {col_sep: ','}})) |
112 | 109 | csv = CSV.parse page.driver.response.body |
113 | 110 | expect(csv[0]).not_to include('Created at') |
114 | 111 | end |
| 112 | + |
| 113 | + it 'exports polymorphic fields the easy way for now' do |
| 114 | + visit export_path(model_name: 'comment') |
| 115 | + select "<comma> ','", from: 'csv_options_generator_col_sep' |
| 116 | + click_button 'Export to csv' |
| 117 | + csv = CSV.parse page.driver.response.body |
| 118 | + expect(csv[0]).to match_array ['Id', 'Commentable', 'Commentable type', 'Content', 'Created at', 'Updated at'] |
| 119 | + csv[1..].each do |line| |
| 120 | + expect(line[csv[0].index('Commentable')]).to eq(player.id.to_s) |
| 121 | + expect(line[csv[0].index('Commentable type')]).to eq(player.class.to_s) |
| 122 | + end |
| 123 | + end |
115 | 124 | end |
116 | 125 |
|
117 | 126 | context 'on cancel' do |
|
0 commit comments