<commit_before># encoding: utf-8
require "cases/helper"
require 'models/owner'
require 'tempfile'

module ActiveRecord
  module ConnectionAdapters
    class SQLite3AdapterTest < ActiveRecord::TestCase
      self.use_transactional_fixtures = false

      class DualEncoding < ActiveRecord::Base
      end

      def setup
        @conn = Base.sqlite3_connection :database => ':memory:',
                                       :adapter => 'sqlite3',
                                       :timeout => 100
        @conn.execute <<-eosql
          CREATE TABLE items (
            id integer PRIMARY KEY AUTOINCREMENT,
            number integer
          )
        eosql

        @subscriber = SQLSubscriber.new
        ActiveSupport::Notifications.subscribe('sql.active_record', @subscriber)
      end

      def test_bad_connection
        assert_raise ActiveRecord::NoDatabaseError do
          connection = ActiveRecord::Base.sqlite3_connection(adapter: "sqlite3", database: "/tmp/should/_not/_exist/-cinco-dog.db")
          connection.exec_query('drop table if exists ex')
        end
      end

      def test_connect_with_url
        original_connection = ActiveRecord::Base.remove_connection
        tf = Tempfile.open 'whatever'
        url = "sqlite3://#{tf.path}"
        ActiveRecord::Base.establish_connection(url)
        assert ActiveRecord::Base.connection
      ensure
        tf.close
        tf.unlink
        ActiveRecord::Base.establish_connection(original_connection)
      end

      def test_connect_memory_with_url
        original_connection = ActiveRecord::Base.remove_connection
        url = "sqlite3:///:memory:"
        ActiveRecord::Base.establish_connection(url)
        assert ActiveRecord::Base.connection
      ensure
        ActiveRecord::Base.establish_connection(original_connection)
      end

      def test_valid_column
        column = @conn.columns('items').find { |col| col.name == 'id' }
        assert @conn.valid_type?(column.type)
      end

      # sqlite databases should be able to support any type and not
      # just the ones mentioned in the native_database_types.<commit_msg>don't establish a new connection when testing with `sqlite3_mem`.<commit_after># encoding: utf-8
require "cases/helper"
require 'models/owner'
require 'tempfile'

module ActiveRecord
  module ConnectionAdapters
    class SQLite3AdapterTest < ActiveRecord::TestCase
      self.use_transactional_fixtures = false

      class DualEncoding < ActiveRecord::Base
      end

      def setup
        @conn = Base.sqlite3_connection :database => ':memory:',
                                       :adapter => 'sqlite3',
                                       :timeout => 100
        @conn.execute <<-eosql
          CREATE TABLE items (
            id integer PRIMARY KEY AUTOINCREMENT,
            number integer
          )
        eosql

        @subscriber = SQLSubscriber.new
        ActiveSupport::Notifications.subscribe('sql.active_record', @subscriber)
      end

      def test_bad_connection
        assert_raise ActiveRecord::NoDatabaseError do
          connection = ActiveRecord::Base.sqlite3_connection(adapter: "sqlite3", database: "/tmp/should/_not/_exist/-cinco-dog.db")
          connection.exec_query('drop table if exists ex')
        end
      end

      def test_connect_with_url
        skip "can't establish new connection when using memory db" if in_memory_db?
        begin
          original_connection = ActiveRecord::Base.remove_connection
          tf = Tempfile.open 'whatever'
          url = "sqlite3://#{tf.path}"
          ActiveRecord::Base.establish_connection(url)
          assert ActiveRecord::Base.connection
        ensure
          tf.close
          tf.unlink
          ActiveRecord::Base.establish_connection(original_connection)
        end
      end

      def test_connect_memory_with_url
        skip "can't establish new connection when using memory db" if in_memory_db?
        begin
          original_connection = ActiveRecord::Base.remove_connection
          url = "sqlite3:///:memory:"
          ActiveRecord::Base.establish_connection(url)
          assert ActiveRecord::Base.connection
        ensure
          ActiveRecord::Base.establish_connection(original_connection)
        end
      end

      def test_valid_column
        column = @conn.columns('items').find { |col| col.name == 'id' }
        assert @conn.valid_type?(column.type)
      end

      # sqlite databases should be able to support any type and not
      # just the ones mentioned in the native_database_types.
{'fraction_non_alphanumeric': 0.0797895659798334, 'fraction_numerical': 0.005041648399824638, 'mean_word_length': 2.3551470588235293, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5562895', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1160, 'n_words': 281}