Skip to content
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

ETQ tech: je veux publier les fichiers opendata selon les bonnes pratiques de data.gouv #11100

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
resource: user_connected_with_france_connect_by_month
  • Loading branch information
Benoit-MINT committed Dec 23, 2024
commit 3349e06263fbd2c88c680cba797236e7378ea5a0
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
# frozen_string_literal: true

class Cron::Datagouv::UserConnectedWithFranceConnectByMonthJob < Cron::CronJob
include DatagouvCronSchedulableConcern
class Cron::Datagouv::UserConnectedWithFranceConnectByMonthJob < Cron::Datagouv::BaseJob
self.schedule_expression = "every month at 3:45"
FILE_NAME = "nb_utilisateurs_connectes_france_connect_par_mois"
HEADERS = ["mois", "nb_utilisateurs_connectes_france_connect_par_mois"]
FILE_NAME = HEADERS[1]
RESOURCE = 'f688e8aa-5c21-4b61-ba03-b69e33c112f7'

def perform(*args)
GenerateOpenDataCsvService.save_csv_to_tmp(FILE_NAME, data) do |file|
begin
APIDatagouv::API.upload(file, :statistics_dataset)
ensure
FileUtils.rm(file)
end
end
def perform
csv = data_gouv_csv(RESOURCE, HEADERS)

missing_months(csv)
.map { |month| data_for(month:) }
.each { |data| csv << data }

APIDatagouv::API.upload_csv(FILE_NAME, csv, DATASET, RESOURCE)
end

def data
User.where(created_at: 1.month.ago.all_month, loged_in_with_france_connect: "particulier").count
private

def data_for(month:)
[
month.strftime(DATE_FORMAT),
User.where(created_at: month.all_month, loged_in_with_france_connect: "particulier").count
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

RSpec.describe Cron::Datagouv::UserConnectedWithFranceConnectByMonthJob, type: :job do
UserConnectedWithFranceConnectByMonthJob = Cron::Datagouv::UserConnectedWithFranceConnectByMonthJob

def format(date) = date.strftime(UserConnectedWithFranceConnectByMonthJob::DATE_FORMAT)

describe 'perform' do
before { allow(APIDatagouv::API).to receive(:existing_csv).and_return(existing_csv) }

subject(:sent_csv) do
table = nil
allow(APIDatagouv::API).to receive(:upload_csv) { |_, sent, _, _| table = sent }

UserConnectedWithFranceConnectByMonthJob.perform_now

table
end

context 'when there is no existing csv' do
let(:existing_csv) { nil }

it 'sends a csv with one row from the previous month' do
expect(sent_csv.first['mois']).to eq(format(1.month.ago.beginning_of_month.to_date))
expect(sent_csv.first['nb_utilisateurs_connectes_france_connect_par_mois']).to eq(0)
end
end

context 'when 2 months are missing' do
before { Timecop.freeze(Time.zone.parse('15/02/2024')) }

let(:existing_csv) do
csv = CSV::Table.new([], headers: UserConnectedWithFranceConnectByMonthJob::HEADERS)
csv << [format(Date.parse('01/10/2023')), 10]
csv << [format(Date.parse('01/11/2023')), 11]
end

it 'sends a csv with one row from the previous month' do
expected_csv = [
["mois", "nb_utilisateurs_connectes_france_connect_par_mois"],
["2023-10", 10],
["2023-11", 11],
["2023-12", 0],
["2024-01", 0]
]
expect(sent_csv.to_a).to eq(expected_csv)
end
end
end

describe 'data_for' do
let(:month) { Date.parse('01/01/2024') }

subject { UserConnectedWithFranceConnectByMonthJob.new.send(:data_for, month:) }

context 'when users have been france connected during the target month' do
let!(:user) { create(:user, created_at: Date.parse('15/01/2024'), loged_in_with_france_connect: "particulier") }

it { is_expected.to eq(['2024-01', 1]) }
end

context 'when users have been france connected but not during the target month' do
let!(:user) { create(:user, created_at: Date.parse('15/12/2023'), loged_in_with_france_connect: "particulier") }

it { is_expected.to eq(['2024-01', 0]) }
end

context 'when users have not been france connected' do
let!(:user) { create(:user, created_at: Date.parse('15/01/2024'), loged_in_with_france_connect: nil) }

it { is_expected.to eq(['2024-01', 0]) }
end
end
end