<commit_before>require 'tsuga/adapter/active_record/base'
require 'tsuga/adapter/active_record/cluster'
require 'tsuga/adapter/active_record/record'
require 'active_record'
require 'sqlite3'
require 'ostruct'
require 'forwardable'
require 'yaml'

module Tsuga::Adapter::ActiveRecord
  module Test
    class << self
      extend Forwardable
      delegate [:records, :clusters] => :models

      def models
        @_models ||= _build_test_models
      end

      private

      # Makes sure a connection exists
      def _db
        @_db ||= begin
          unless ActiveRecord::Base.connected?
            ActiveRecord::Base.establish_connection(adapter:'sqlite3', database:':memory:')
          end
          ActiveRecord::Base.connection
        end
      end

      def _prepare_tables
        _db.drop_table(:test_records) if _db.table_exists?(:test_records)
        _db.create_table(:test_records) do |t|
          t.float    :geohash
          t.float    :lat
          t.float    :lng
        end
        _db.add_index :test_records, :geohash

        _db.drop_table(:test_clusters) if _db.table_exists?(:test_clusters)
        _db.create_table(:test_clusters) do |t|
          t.float    :geohash
          t.float    :lat
          t.float    :lng
          t.integer  :depth
          t.integer  :parent_id
          t.string   :children_type
          t.string   :children_ids # FIXME
          t.float    :sum_lat
          t.float    :sum_lng
          t.float    :ssq_lat
          t.float    :ssq_lng
          t.integer  :weight
        end
        _db.add_index :test_clusters, [:depth, :geohash]
      end

      def _build_test_models
        _prepare_tables

        cluster_model = Class.new(ActiveRecord::Base) do
          self.table_name = 'test_clusters'<commit_msg>Improves test AR schema<commit_after>require 'tsuga/adapter/active_record/base'
require 'tsuga/adapter/active_record/cluster'
require 'tsuga/adapter/active_record/record'
require 'active_record'
require 'sqlite3'
require 'ostruct'
require 'forwardable'
require 'yaml'

module Tsuga::Adapter::ActiveRecord
  module Test
    class << self
      extend Forwardable
      delegate [:records, :clusters] => :models

      def models
        @_models ||= _build_test_models
      end

      private

      # Makes sure a connection exists
      def _db
        @_db ||= begin
          unless ActiveRecord::Base.connected?
            ActiveRecord::Base.establish_connection(adapter:'sqlite3', database:':memory:')
          end
          ActiveRecord::Base.connection
        end
      end

      def _prepare_tables
        _db.drop_table(:test_records) if _db.table_exists?(:test_records)
        _db.create_table(:test_records) do |t|
          t.string   :geohash, limit:16
          t.float    :lat
          t.float    :lng
        end
        _db.add_index :test_records, :geohash

        _db.drop_table(:test_clusters) if _db.table_exists?(:test_clusters)
        _db.create_table(:test_clusters) do |t|
          t.integer  :depth
          t.string   :geohash,        limit:16
          t.float    :lat
          t.float    :lng
          t.integer  :parent_id
          t.string   :children_type
          t.string   :children_ids # FIXME
          t.float    :sum_lat
          t.float    :sum_lng
          t.float    :ssq_lat
          t.float    :ssq_lng
          t.integer  :weight
        end
        _db.add_index :test_clusters, [:depth, :geohash]
      end

      def _build_test_models
        _prepare_tables

        cluster_model = Class.new(ActiveRecord::Base) do
          self.table_name = 'test_clusters'
{'fraction_non_alphanumeric': 0.08671637669152168, 'fraction_numerical': 0.0022093344380005524, 'mean_word_length': 2.0878090366581414, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4426599', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1112, 'n_words': 232}