Skip to content

Add test coverage for ActiveModel::Type::Binary - #58335

Merged
byroot merged 1 commit into
rails:mainfrom
hammadxcm:test-activemodel-binary-type-coverage
Aug 10, 2026
Merged

byroot merged 1 commit into
rails:mainfrom
hammadxcm:test-activemodel-binary-type-coverage

Conversation

@hammadxcm

Copy link
Copy Markdown
Contributor

Summary

ActiveModel::Type::Binary's own test file covered cast and serialize only. Running line coverage over activemodel/test/cases/ showed active_model/type/binary.rb at 25/31 lines, with these unreached:

  • type and binary?
  • the Data branch of cast:
    def cast(value)
      if value.is_a?(Data)
        value.to_s   # <- unreached
  • changed_in_place?
  • Data#hex

Tests added

  • test_type_and_binary_predicate — type is :binary and binary? is true.
  • test_serialize_returns_binary_data — nil passes through; other values come back wrapped in Binary::Data.
  • test_cast_binary_data_returns_the_underlying_string — round-trips a serialized value back through cast, asserting the Data branch returns the binary-encoded string.
  • test_changed_in_place — true when the deserialized old value differs, false when it matches.
  • test_data_hex_and_string_conversion — Data#hex on non-UTF-8 bytes, plus to_s/to_str and comparison against a raw string.

No behavior changes — tests only, so no CHANGELOG entry.

active_model/type/binary.rb goes from 25/31 to full line coverage. activemodel/test/cases/: 1199 runs, 5375 assertions, 0 failures, 0 errors. RuboCop clean.

Only `cast` and `serialize` were exercised by Active Model's own suite.
Line coverage over `activemodel/test/cases/` showed `type`, `binary?`,
the `Data` branch of `cast`, `changed_in_place?`, and `Data#hex` all
unreached.

Adds coverage for:

* `type` and `binary?`.
* `serialize` returning `nil` for `nil`, and a `Binary::Data` otherwise.
* `cast` unwrapping a `Binary::Data` back to its binary-encoded string.
* `changed_in_place?` comparing the deserialized old value.
* `Data#hex`, `#to_s`/`#to_str`, and comparison against a raw string.

This brings `active_model/type/binary.rb` to full line coverage.
@byroot
byroot merged commit 1adb63b into rails:main Aug 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants