Skip to content

Instantly share code, notes, and snippets.

@mlh758
Created April 9, 2018 20:04
Show Gist options
  • Save mlh758/58b9a2e7863d3c81b68ec7b261c3d94a to your computer and use it in GitHub Desktop.
Save mlh758/58b9a2e7863d3c81b68ec7b261c3d94a to your computer and use it in GitHub Desktop.
ActiveRecord Primary Key Test Case
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails', '5.1.6'
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :samples, primary_key: 'sample_id', force: true do |t|
t.string 'comment'
end
end
class Sample < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_association_stuff
Sample.find_or_create_by(sample_id: 1)
ActiveRecord::Base.connection.add_column :samples, :id, :integer
Sample.reset_column_information
record = Sample.first
assert_equal 1, record.sample_id
assert_nil Sample.first.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment