<commit_before>require 'erubis'
require 'uri'
require 'net/http'
require 'w3c_validators'

include W3CValidators

def insert_title_with_owners(number_proprietors = 1, closure_status = 'OPEN')
  @title = create_title_hash('DN1000', closure_status)
  @title[:proprietors] = create_proprietors(number_proprietors)
  process_title_template(@title)
end

def update_title_with_new_owners(new_proprietor_name)
  @title = create_title_hash('DN1000')
  @title[:proprietors] = new_proprietor(new_proprietor_name)
  process_title_template(@title)
end

def insert_title_with_owners_different_title(number_proprietors = 1)
  @title = create_title_hash('DN1001')
  @title[:proprietors] = create_proprietors(number_proprietors)
  process_title_template(@title)
end

def create_title_hash(title_number, closure_status = 'OPEN')
  house_number = rand(1..500).to_s
  {
    title_number: title_number,
    street_name: 'Test Street',
    postcode: 'PL9 8TB',
    house_no: house_number,
    town: 'Plymouth',
    last_changed: '02 July 1996 at 00:59:59',
    address_string: "#{house_number} Test Street, Plymouth, PL9 8TB",
    uprn: rand(1000..99_999),
    closure_status: closure_status
  }
end

def process_title_template(title)
  file = File.read('./data/title_template.erb')
  eruby = Erubis::Eruby.new(file)
  File.write('./data/test-generated/title.json', eruby.result(binding))
  process_titles_in_directory('test-generated')
  # Give ElasticSearch time to rerun an election
  sleep($ELASTICSEARCH_SLEEP.to_i)
  title
end

# connect to the database and execute the sql (that deletes everything)
def delete_all_titles
  $db_connection.exec('DROP TABLE IF EXISTS title_register_data;')
  $db_connection.exec('DROP TABLE IF EXISTS title_numbers_uprns;')
end

def delete_all_users
  $user_db_connection.exec('DROP TABLE IF EXISTS users;')
end

def create_elasticsearch_index
  `sh set_up_elasticsearch.sh`
end

def delete_all_titles_from_elasticsearch
  host = "http://#{$ELASTICSEARCH_HOST}:#{$ELASTICSEARCH_PORT}"
  `curl -XDELETE #{host}/landregistry/property_by_postcode`
end

def create_register_tables
  delete_all_titles
  delete_all_users
  create_title_register_data_table
  create_title_numbers_uprns_table
  create_users_table
end

def create_title_register_data_table
  $db_connection.exec("
CREATE TABLE \"title_register_data\" (
  \"title_number\" character(10),
  \"register_data\" json,
  \"geometry_data\" json
);")
end

def create_title_numbers_uprns_table
  $db_connection.exec("
CREATE TABLE \"title_numbers_uprns\" (
\"title_number\" character (10),
\"uprn\" character(10)
);
CREATE INDEX index_uprn ON title_numbers_uprns(uprn);
CREATE INDEX index_title_number ON title_numbers_uprns(title_number);")
end

def create_users_table
  $user_db_connection.exec("
CREATE TABLE users
(
  user_id character varying(100) NOT NULL,
  password_hash character varying(64),
  failed_logins smallint NOT NULL DEFAULT 0,
  CONSTRAINT user_id_pkey PRIMARY KEY (user_id)
);")
end

def create_proprietors(number_proprietors)
  proprietors = []
  number_proprietors.times do |i|
    proprietors << {
      name: "<NAME> #{i + 1}",
      address: "Proprietor address #{i + 1}"
    }
  end
  proprietors
end

def new_proprietor(new_proprietor_name)
  proprietors = []
  proprietors << {
    name: new_proprietor_name,
    address: 'Proprietor address'
  }
  proprietors
end

# Function to perform w3c compliance check against the provided page using the w3c_validators gem
def validate_page(page)
  validator = MarkupValidator.new
  results = validator.validate_text(page)
  if results.errors.length > 0
    results.errors.each do |err|
      raise("Error #{err.message} on page #{page.current_url}")
    end
  end
end<commit_msg>more rubocop issues with the validaor function fixed<commit_after>require 'erubis'
require 'uri'
require 'net/http'
require 'w3c_validators'

include W3CValidators

def insert_title_with_owners(number_proprietors = 1, closure_status = 'OPEN')
  @title = create_title_hash('DN1000', closure_status)
  @title[:proprietors] = create_proprietors(number_proprietors)
  process_title_template(@title)
end

def update_title_with_new_owners(new_proprietor_name)
  @title = create_title_hash('DN1000')
  @title[:proprietors] = new_proprietor(new_proprietor_name)
  process_title_template(@title)
end

def insert_title_with_owners_different_title(number_proprietors = 1)
  @title = create_title_hash('DN1001')
  @title[:proprietors] = create_proprietors(number_proprietors)
  process_title_template(@title)
end

def create_title_hash(title_number, closure_status = 'OPEN')
  house_number = rand(1..500).to_s
  {
    title_number: title_number,
    street_name: 'Test Street',
    postcode: 'PL9 8TB',
    house_no: house_number,
    town: 'Plymouth',
    last_changed: '02 July 1996 at 00:59:59',
    address_string: "#{house_number} Test Street, Plymouth, PL9 8TB",
    uprn: rand(1000..99_999),
    closure_status: closure_status
  }
end

def process_title_template(title)
  file = File.read('./data/title_template.erb')
  eruby = Erubis::Eruby.new(file)
  File.write('./data/test-generated/title.json', eruby.result(binding))
  process_titles_in_directory('test-generated')
  # Give ElasticSearch time to rerun an election
  sleep($ELASTICSEARCH_SLEEP.to_i)
  title
end

# connect to the database and execute the sql (that deletes everything)
def delete_all_titles
  $db_connection.exec('DROP TABLE IF EXISTS title_register_data;')
  $db_connection.exec('DROP TABLE IF EXISTS title_numbers_uprns;')
end

def delete_all_users
  $user_db_connection.exec('DROP TABLE IF EXISTS users;')
end

def create_elasticsearch_index
  `sh set_up_elasticsearch.sh`
end

def delete_all_titles_from_elasticsearch
  host = "http://#{$ELASTICSEARCH_HOST}:#{$ELASTICSEARCH_PORT}"
  `curl -XDELETE #{host}/landregistry/property_by_postcode`
end

def create_register_tables
  delete_all_titles
  delete_all_users
  create_title_register_data_table
  create_title_numbers_uprns_table
  create_users_table
end

def create_title_register_data_table
  $db_connection.exec("
CREATE TABLE \"title_register_data\" (
  \"title_number\" character(10),
  \"register_data\" json,
  \"geometry_data\" json
);")
end

def create_title_numbers_uprns_table
  $db_connection.exec("
CREATE TABLE \"title_numbers_uprns\" (
\"title_number\" character (10),
\"uprn\" character(10)
);
CREATE INDEX index_uprn ON title_numbers_uprns(uprn);
CREATE INDEX index_title_number ON title_numbers_uprns(title_number);")
end

def create_users_table
  $user_db_connection.exec("
CREATE TABLE users
(
  user_id character varying(100) NOT NULL,
  password_hash character varying(64),
  failed_logins smallint NOT NULL DEFAULT 0,
  CONSTRAINT user_id_pkey PRIMARY KEY (user_id)
);")
end

def create_proprietors(number_proprietors)
  proprietors = []
  number_proprietors.times do |i|
    proprietors << {
      name: "Proprietor name #{i + 1}",
      address: "Proprietor address #{i + 1}"
    }
  end
  proprietors
end

def new_proprietor(new_proprietor_name)
  proprietors = []
  proprietors << {
    name: new_proprietor_name,
    address: 'Proprietor address'
  }
  proprietors
end

# Function to perform w3c compliance check against the provided page using the w3c_validators gem
def validate_page(page)
  validator = MarkupValidator.new
  results = validator.validate_text(page)
  return unless results.errors.length > 0
    results.errors.each do |err|
      fail("Error #{err.message} on page #{page.current_url}")
    end
  end
end
{'fraction_non_alphanumeric': 0.09797116924719701, 'fraction_numerical': 0.016550987720234916, 'mean_word_length': 5.549825174825175, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4599671', 'n_tokens_mistral': 2681, 'n_tokens_neox': 2631, 'n_words': 552}