From ccd63ecfe1ce2876408d4b321663a82c12af3f73 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 18 Mar 2019 12:09:54 +0100 Subject: [PATCH] include zip archive into tests + fix: allow multiple runs of test suite --- ...190107111749_protect_accounting_periods.rb | 2 ++ test/integration/accounting_period_test.rb | 29 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/db/migrate/20190107111749_protect_accounting_periods.rb b/db/migrate/20190107111749_protect_accounting_periods.rb index 2fc0d7a52..01ba232e0 100644 --- a/db/migrate/20190107111749_protect_accounting_periods.rb +++ b/db/migrate/20190107111749_protect_accounting_periods.rb @@ -2,6 +2,8 @@ class ProtectAccountingPeriods < ActiveRecord::Migration # PostgreSQL only def up + return if Rails.env.development? || Rails.env.test? + execute("CREATE RULE accounting_periods_del_protect AS ON DELETE TO #{AccountingPeriod.arel_table.name} DO INSTEAD NOTHING;") execute("CREATE RULE accounting_periods_upd_protect AS ON UPDATE TO #{AccountingPeriod.arel_table.name} DO INSTEAD NOTHING;") end diff --git a/test/integration/accounting_period_test.rb b/test/integration/accounting_period_test.rb index 83b10b8dd..3adc82f65 100644 --- a/test/integration/accounting_period_test.rb +++ b/test/integration/accounting_period_test.rb @@ -32,14 +32,39 @@ class AccountingPeriodTest < ActionDispatch::IntegrationTest # Check archive file was created assert FileTest.exists? accounting_period.archive_file + # Extract archive + require 'tmpdir' + require 'fileutils' + dest = "#{Dir.tmpdir}/#{accounting_period.archive_file[0..-5]}" + FileUtils.mkdir_p "#{dest}/accounting" + Zip::File.open(accounting_period.archive_file) do |zip_file| + # Handle entries one by one + zip_file.each do |entry| + # Extract to file/directory/symlink + entry.extract("#{dest}/#{entry.name}") + end + end + # Check archive matches - archive = File.read(accounting_period.archive_file) + require 'checksum' + sumfile = File.read("#{dest}/checksum.sha256").split("\t") + assert_equal sumfile[0], Checksum.file("#{dest}/#{sumfile[1]}"), 'archive checksum does not match' + + archive = File.read("#{dest}/#{sumfile[1]}") archive_json = JSON.parse(archive) invoices = Invoice.where( 'created_at >= :start_date AND created_at <= :end_date', start_date: start_at.to_datetime, end_date: end_at.to_datetime ) - assert_equal invoices.count, archive_json.count + + assert_equal invoices.count, archive_json['invoices'].count + assert_equal accounting_period.footprint, archive_json['period_footprint'] + + require 'version' + assert_equal Version.current, archive_json['software']['version'] + + # we clean up the extraction dir before quitting + FileUtils.rm_rf(dest) end end