Skip to content

Commit

Permalink
Reduce offence seed data for cucumber tests
Browse files Browse the repository at this point in the history
When the cucumber test suite starts, a large volume of production seed
data is loaded into the database. This includes a full list of offences
for nine fee schemes, however only ten of those offences are used in
tests.

In an attempt to speed up the running of the test suite, this amends
`features/support/hooks.rb` to load a bespoke cucumber-only version of
the offence data for AGFS fee scheme 11, containing only those ten offences.
This offence data is copied for subsequent AGFS fee schemes so the total
number of records being created is reduced from ~6,500 to 50.

* We could consider doing something similar for other seed data.
* We could consider removing seed data altogether and use factories to
generate required data.
  • Loading branch information
mpw5 committed Dec 21, 2023
1 parent 738d49f commit e1e4a46
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
61 changes: 61 additions & 0 deletions db/seeds/scheme_11_cucumber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'csv'
require Rails.root.join('db','seed_helper')
agfs_fee_scheme_ten = FeeScheme.find_or_create_by(name: 'AGFS', version: 10)
agfs_fee_scheme_ten.update(end_date: Settings.agfs_scheme_11_release_date.end_of_day-1.day)
agfs_fee_scheme_eleven = FeeScheme.find_or_create_by(name: 'AGFS', version: 11, start_date: Settings.agfs_scheme_11_release_date.beginning_of_day)

# Reset the ID so that ids of >=3000 will be scheme 11 offences
# This will allow us to add extra scheme 10 offences in an emergency while also providing an obvious break point
# for software vendors
ActiveRecord::Base.connection.set_pk_sequence!('offences', 3000) if Offence.order(:id).last.id < 3000

# create new offences (from csv)
file_path = Rails.root.join('lib', 'assets', 'data', 'scheme_11_offences_cucumber.csv')
csv_file = File.open(file_path, 'r:ISO-8859-1')
csv = CSV.parse(csv_file, headers: true)

module OffenceCSVRowExtensions
def description
self['description'].strip
end

def offence_category
OffenceCategory.find_by(number: self['category'])
end

def offence_band
OffenceBand.find_by(offence_category: offence_category, description: self['band'])
end

def contrary_to
self['contrary_to']
end

def year_chapter
self['year_chapter']
end
end

class CSV::Row
include OffenceCSVRowExtensions
end

csv.each do |row|
SeedHelper.find_or_create_scheme_11_offence!(
{
offence_band: row.offence_band,
description: row.description,
contrary: row.contrary_to,
year_chapter: row.year_chapter
},
agfs_fee_scheme_eleven
)
end

# regenerate unique codes based on offence description and band where order is significant
require Rails.root.join('lib','data_migrator','offence_unique_code_migrator')
offences = agfs_fee_scheme_eleven.offences.joins(:offence_band).where(offence_class: nil).unscope(:order).order(Arel.sql('offences.description COLLATE "C", offences.contrary COLLATE "C", offence_bands.description COLLATE "C"'))
migrator = DataMigrator::OffenceUniqueCodeMigrator.new(relation: offences)
migrator.migrate!


2 changes: 1 addition & 1 deletion features/support/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
load "#{Rails.root}/db/seeds/offence_classes.rb"
load "#{Rails.root}/db/seeds/offences.rb"
load "#{Rails.root}/db/seeds/scheme_10.rb"
load "#{Rails.root}/db/seeds/scheme_11.rb"
load "#{Rails.root}/db/seeds/scheme_11_cucumber.rb"
load "#{Rails.root}/db/seeds/scheme_12.rb"
load "#{Rails.root}/db/seeds/case_types.rb"
load "#{Rails.root}/db/seeds/case_stages.rb"
Expand Down
10 changes: 10 additions & 0 deletions lib/assets/data/scheme_11_offences_cucumber.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
category,band,description,contrary_to,year_chapter
3,3.4,Possession of offensive weapon on school premises,"Criminal Justice Act 1988, s.139A",1996 c. 26
3,3.5,Racially or religiously aggravated common assault,"Crime and Disorder Act 1998, s.29(1)(c ) and (3)",1998 c. 37
5,5.3,Solicitation by men,"Sexual Offences Act 1956, s.32",1956 c. 69
8,8.1,Harbouring escaped prisoner,"Criminal Justice Act 1961, s.22(2)",1961 c. 39
8,8.1,Have in possession or control identity documents with improper intent,"Identity Documents Act 2010, s.4",2010 c. 40
11,11.2,Burglary (domestic),"Theft Act 1968, s.9(3)(a)",1968 c. 60
11,11.2,Burglary (non-domestic),"Theft Act 1968, s.9(3)(b)",1968 c. 60
16,16.3,Unlicensed or unauthorised air traffic controllers,Air Navigation Order 2016,2016 No. 765
17,17.1,Absconding from lawful custody,Common Law,

0 comments on commit e1e4a46

Please sign in to comment.