diff --git a/test/enumerations_test.rb b/test/enumerations_test.rb index ebb760f..24da3b7 100644 --- a/test/enumerations_test.rb +++ b/test/enumerations_test.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require_relative 'helpers/test_helper' require 'enumerations/errors' -class EnumerationsTest < Minitest::Test +class EnumerationsTest < Minitest::Test # rubocop:disable Metrics/ClassLength def test_reflect_on_all_enumerations enumerations = Post.reflect_on_all_enumerations @@ -157,4 +159,12 @@ def test_on_nil_value_assignment user = User.new(role: nil) assert_nil user.role end + + def test_type_cast_when_saving_value_to_database + client = ApiClient.new(api_client_permission: ApiClientPermission.people_first_name) + client.save + + # overriding to_s doesn't have an effect on the value saved to the database + assert_equal 'people_first_name', client[:api_client_permission] + end end diff --git a/test/helpers/database_helper.rb b/test/helpers/database_helper.rb index 0100473..cd726d5 100644 --- a/test/helpers/database_helper.rb +++ b/test/helpers/database_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'logger' ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') @@ -21,4 +23,8 @@ create_table :overridable_models, force: true do |t| t.integer :overridable_status_id end + + create_table :api_clients, force: true do |t| + t.string :api_client_permission + end end diff --git a/test/helpers/test_helper.rb b/test/helpers/test_helper.rb index b23a18a..77759cb 100644 --- a/test/helpers/test_helper.rb +++ b/test/helpers/test_helper.rb @@ -31,6 +31,19 @@ def my_custom_name end end +class ApiClientPermission < Enumerations::Base + value :people_first_name, id: 'people.first_name' + value :people_last_name, id: 'people.last_name' + + def to_s + id + end +end + +class ApiClient < ActiveRecord::Base + enumeration :api_client_permission +end + class Post < ActiveRecord::Base enumeration :status enumeration :different_status, foreign_key: :some_other_status, class_name: 'Status'