|
| 1 | +require File.expand_path('../../../test_helper', __FILE__) |
| 2 | + |
| 3 | +class QuoteMethodTest < ActiveSupport::TestCase |
| 4 | + def test_quote_method_does_not_hardcode_double_quotes |
| 5 | + # The quote method should NOT hardcode double quotes |
| 6 | + # It should delegate to the database adapter's quote_column_name |
| 7 | + # This test verifies the implementation uses the adapter, not hardcoded quotes |
| 8 | + field_name = 'test_field' |
| 9 | + |
| 10 | + # Get the actual implementation's result |
| 11 | + actual = PostResource.send(:quote, field_name) |
| 12 | + |
| 13 | + # The implementation should use connection.quote_column_name, not hardcode "\"#{field}\"" |
| 14 | + # We verify this by checking if the method delegates to the connection |
| 15 | + connection = PostResource._model_class.connection |
| 16 | + expected = connection.quote_column_name(field_name) |
| 17 | + |
| 18 | + assert_equal expected, actual, |
| 19 | + "quote method should delegate to connection.quote_column_name, not hardcode double quotes" |
| 20 | + end |
| 21 | + |
| 22 | + def test_quote_uses_mysql_style_backticks_with_mysql_adapter |
| 23 | + # This test verifies the quote method respects the database adapter's quoting style |
| 24 | + # MySQL uses backticks, PostgreSQL/SQLite use double quotes |
| 25 | + |
| 26 | + field_name = 'test_field' |
| 27 | + |
| 28 | + # Create a mock connection that uses MySQL-style backticks |
| 29 | + mock_connection = Minitest::Mock.new |
| 30 | + mock_connection.expect(:quote_column_name, "`#{field_name}`", [field_name.to_s]) |
| 31 | + |
| 32 | + # Stub the connection method to return our mock |
| 33 | + PostResource._model_class.stub(:connection, mock_connection) do |
| 34 | + actual = PostResource.send(:quote, field_name) |
| 35 | + assert_equal "`#{field_name}`", actual, |
| 36 | + "quote method should use backticks when MySQL adapter is used" |
| 37 | + end |
| 38 | + |
| 39 | + mock_connection.verify |
| 40 | + end |
| 41 | + |
| 42 | + def test_quote_uses_connection_adapter |
| 43 | + # The quote method should delegate to the database adapter's quote_column_name |
| 44 | + # This ensures proper quoting for different databases: |
| 45 | + # - SQLite/PostgreSQL: double quotes (") |
| 46 | + # - MySQL: backticks (`) |
| 47 | + field_name = 'test_field' |
| 48 | + expected = PostResource._model_class.connection.quote_column_name(field_name) |
| 49 | + actual = PostResource.send(:quote, field_name) |
| 50 | + |
| 51 | + assert_equal expected, actual, |
| 52 | + "quote method should use connection adapter's quote_column_name" |
| 53 | + end |
| 54 | + |
| 55 | + def test_quote_with_symbol_field |
| 56 | + field_name = :test_field |
| 57 | + expected = PostResource._model_class.connection.quote_column_name(field_name.to_s) |
| 58 | + actual = PostResource.send(:quote, field_name) |
| 59 | + |
| 60 | + assert_equal expected, actual, |
| 61 | + "quote method should handle symbol field names" |
| 62 | + end |
| 63 | + |
| 64 | + def test_concat_table_field_quoted |
| 65 | + table = 'posts' |
| 66 | + field = 'title' |
| 67 | + connection = PostResource._model_class.connection |
| 68 | + expected = "#{connection.quote_column_name(table)}.#{connection.quote_column_name(field)}" |
| 69 | + actual = PostResource.send(:concat_table_field, table, field, true) |
| 70 | + |
| 71 | + assert_equal expected, actual, |
| 72 | + "concat_table_field with quoted=true should use proper database quoting" |
| 73 | + end |
| 74 | + |
| 75 | + def test_alias_table_field_quoted |
| 76 | + table = 'posts' |
| 77 | + field = 'title' |
| 78 | + connection = PostResource._model_class.connection |
| 79 | + expected = connection.quote_column_name("#{table}_#{field}") |
| 80 | + actual = PostResource.send(:alias_table_field, table, field, true) |
| 81 | + |
| 82 | + assert_equal expected, actual, |
| 83 | + "alias_table_field with quoted=true should use proper database quoting" |
| 84 | + end |
| 85 | + |
| 86 | + def test_sql_field_with_alias_uses_proper_quoting |
| 87 | + table = 'posts' |
| 88 | + field = 'title' |
| 89 | + connection = PostResource._model_class.connection |
| 90 | + quoted_table_field = "#{connection.quote_column_name(table)}.#{connection.quote_column_name(field)}" |
| 91 | + quoted_alias = connection.quote_column_name("#{table}_#{field}") |
| 92 | + expected_sql = "#{quoted_table_field} AS #{quoted_alias}" |
| 93 | + |
| 94 | + actual = PostResource.send(:sql_field_with_alias, table, field, true) |
| 95 | + |
| 96 | + assert actual.is_a?(Arel::Nodes::SqlLiteral), |
| 97 | + "sql_field_with_alias should return an Arel::Nodes::SqlLiteral" |
| 98 | + assert_equal expected_sql, actual.to_s, |
| 99 | + "sql_field_with_alias should use proper database quoting" |
| 100 | + end |
| 101 | +end |
0 commit comments