Commit 5d170f07 authored by Sean McGivern's avatar Sean McGivern

Merge branch '235882-user-data-export-generator' into 'master'

Resolve "Create class for generating user data export file"

See merge request gitlab-org/gitlab!41595
parents 82450e42 be5ec153
# frozen_string_literal: true
module HistoricalUserData
class CsvService
FILESIZE_LIMIT = 15.megabytes
def initialize(historical_data_relation)
@historical_data_relation = historical_data_relation
end
def generate
header_csv + csv_builder.render(FILESIZE_LIMIT)
end
private
attr_reader :historical_data_relation
def csv_builder
@csv_builder ||= CsvBuilder.new(historical_data_relation, header_to_value_hash)
end
def header_to_value_hash
{
'Date' => -> (historical_datum) { historical_datum.date.to_s(:csv) },
'Active User Count' => 'active_user_count'
}
end
def license
@license ||= License.current
end
def header_csv
CSV.generate do |csv|
csv << ['License Key', license.data]
csv << ['Email', license.licensee['Email']]
csv << ['License Start Date', license.starts_at.to_s(:csv)]
csv << ['License End Date', license.expires_at.to_s(:csv)]
csv << ['Company', license.licensee['Company']]
csv << ['Generated At', Time.current.to_s(:csv)]
csv << ['']
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe HistoricalUserData::CsvService do
subject(:csv) { CSV.parse(csv_service.generate) }
let_it_be(:csv_service) { described_class.new(HistoricalData.all) }
let_it_be(:datetime) { DateTime.parse('2020-09-04 10:00:00') }
let_it_be(:license_email) { 'data@example.com' }
let_it_be(:license_company) { 'Widgets Inc.' }
let_it_be(:license_start_date) { datetime.to_date - 10.months }
let_it_be(:license_end_date) { datetime.to_date + 2.months }
let_it_be(:current_license) do
create_current_license(
licensee: { 'Name' => 'Test', 'Email' => license_email, 'Company' => license_company },
starts_at: license_start_date,
expires_at: license_end_date
)
end
around do |example|
travel_to(datetime) { example.run }
end
context 'License Information Header' do
context 'License Key' do
it 'shows the header title' do
expect(csv[0][0]).to eq('License Key')
end
it 'shows the license key' do
expect(csv[0][1]).to eq(current_license.data)
end
end
context 'Email' do
it 'shows the header title' do
expect(csv[1][0]).to eq('Email')
end
it 'shows the license email' do
expect(csv[1][1]).to eq(license_email)
end
end
context 'License Start Date' do
it 'shows the header title' do
expect(csv[2][0]).to eq('License Start Date')
end
it 'shows the license start date' do
expect(csv[2][1]).to eq(license_start_date.to_s(:csv))
end
end
context 'License End Date' do
it 'shows the header title' do
expect(csv[3][0]).to eq('License End Date')
end
it 'shows the license end date' do
expect(csv[3][1]).to eq(license_end_date.to_s(:csv))
end
end
context 'Company' do
it 'shows the header title' do
expect(csv[4][0]).to eq('Company')
end
it 'shows the license company' do
expect(csv[4][1]).to eq(license_company)
end
end
context 'Generated At' do
it 'shows the header title' do
expect(csv[5][0]).to eq('Generated At')
end
it 'shows the CSV generation time' do
expect(csv[5][1]).to eq(datetime.to_s(:csv))
end
end
end
context 'User Count Table' do
let_it_be(:historical_datum) do
create(:historical_data, date: license_start_date, active_user_count: 1)
end
let_it_be(:historical_datum2) do
create(:historical_data, date: license_start_date + 1.day, active_user_count: 2)
end
it 'shows the header for the user counts table' do
expect(csv[7]).to contain_exactly('Date', 'Active User Count')
end
it 'includes proper values for each column type', :aggregate_failures do
expect(csv[8]).to contain_exactly(
historical_datum.date.to_s(:db),
historical_datum.active_user_count.to_s
)
expect(csv[9]).to contain_exactly(
historical_datum2.date.to_s(:db),
historical_datum2.active_user_count.to_s
)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment