Skip to content

ruflet_record: fix six bugs the coverage tests exposed; test the untested public API - #90

Merged
AdamMusa merged 31 commits into
mainfrom
test/record-api-coverage
Nov 20, 2026
Merged

AdamMusa merged 31 commits into
mainfrom
test/record-api-coverage

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Part of the release-readiness test pass, for ruflet_record: six bug fixes, each with a regression test, plus tests for the public API that no test called.

Behavior changes

Each item is a bug fix. Every other existing call behaves as on main.

  1. Datetimes keep their instant. A bound Time is stored as UTC. Before, it was stored by its own wall clock and read back as UTC: Time.new(2026, 1, 1, 10, 0, 0, "+02:00") reloaded as 10:00 UTC, and now reloads as 08:00 UTC. A string's Z or ±HH:MM offset is now applied when cast. It was ignored. An assigned Time is kept as given in memory, as before. created_at/updated_at were always UTC and are unaffected.
    Upgrade notes: rows that earlier versions wrote from a non-UTC Time hold local wall-clock text and are not migrated. They read back as before, but where(at: that_time) no longer matches them. With TZ=Europe/Berlin, a row main wrote from Time.local(2026, 5, 1, 14) gives count 1 on main and 0 here. An app that shows a reloaded time without localtime now shows UTC: in Berlin, Event.create!(at: Time.local(2026, 5, 1, 14)) reloads as 12:00 +0000, where main showed 14:00 +0000. at.localtime gives 14:00 +0200.
  2. A model binds a Hash or Array as JSON text. This covers the values that create, update, update_columns and update_all write, and Hash conditions: where(col: hash), where.not, find_by, and the Hash or Array items of a where(col: [...]) IN list. Before, CRuby read a Hash as named parameters and raised no such bind parameter. It flattened an Array into the binds, so two or more items raised column index out of range, one item stored only that item, and an empty Array or Hash stored NULL (an update with one matched no row). The device VM stored inspect text.
    Raw SQL is unchanged. connection.execute/select_all binds and where("...", value) binds reach the driver as on main, so CRuby named parameters and Array flattening still work. bound_attributes still shows the values as given. json columns stay TEXT and are not parsed, and strings, numbers and booleans read back exactly as on main ("007", "true", true→"1", 1.0→"1.0"). json is required the first time a model binds a Hash or Array. On device that loads the VM's builtin json. With no JSON library (a bare mruby), the value is bound unchanged, as before.
    Upgrade notes (device): rows that main's VM wrote from a Hash hold inspect text, and where(col: same_hash) no longer matches them (packaged VM: count 1 on main, 0 here). The VM still binds a Hash in raw SQL as inspect text, so where("data = ?", hash) no longer matches a value a model wrote. Bind JSON.generate(hash) instead, as the README now says.
  3. Inflector. pluralize keeps a y after a vowel: survey→surveys (was surveies), and likewise day and key. singularize strips the es after ss (addresses→address, classes→class, passes→pass) and from statuses, buses and aliases. These drive table names, foreign-key targets and has_many classes, so such models could not work. soliloquies and abuses keep their old results, and so do -sse words: SSE_PLURALS and the new whole-word SSE_WORDS list 46 (mousses→mousse, crevasses, bouillabaisses, noblesses, …). They cover the 56 -sse words in /usr/share/dict/words, except eight. The seven in SSE_WORDS match only as whole words, because as suffixes they would also catch plurals such as carcasses, grasses and bosses. Over the -s, -es and -ies plurals of every word in that dictionary, none that main singularized to a dictionary word now gives a non-word. Eight change from one word to another, because the -ss word is the usual one: asses, brasses, crosses, duchesses, masses, messes, passes, princesses (main: asse, brasse, crosse, duchesse, masse, messe, passe, princesse). An app that worked around this with a surveies table or a Passe class must now set self.table_name or class_name.
  4. Post.new/create/update(user: user) uses the belongs_to writer. It raised UnknownAttributeError. This applies only to belongs_to names, on the model or a parent, that are not also columns. A column is still written directly. Any other unknown key still raises, attr_accessor names included.
  5. A model defined before its abstract parent's establish_connection uses the parent's connection. It raised ConnectionNotEstablished, or used RufletRecord.connection when one existed. establish_connection also clears the cached columns of the models that follow it, so they don't keep the previous database's columns. A model that had a connection when it was defined keeps it, and Base's direct subclasses still use RufletRecord.connection, as before.
  6. Column#null? was inverted. The adapter passed notnull as null.

Since the last review (f64a726)

  • JSON encoding moved from SQLiteAdapter#normalize_binds to the model layer (SQL.column_value, applied by create_record, update_all and Hash-condition fragments when a query runs). With the review's raw-binds probe, the output now matches main line for line. Item 2's description of main was wrong and is corrected above.
  • -sse words: SSE_PLURALS lists 39 words and the new whole-word SSE_WORDS lists 7. Of the 47 words the review found, 39 (mousses→mousse among them) give main's result again. The other eight are the ones listed in item 3.
  • New tests: raw named and flattened binds through execute, where("...") binds, bound_attributes, Hash conditions in where.not/update_all/delete_all, every listed -sse word, and a column named like a belongs_to.
  • Merged main (9202eb0, then a9b5847, 43f7582, 3cb9418, bc45ad6, 313a0b2, f9d79b6, 27e2ffa, ea3f7eb, f034e99 and 9f2ca79). 3cb9418 brings ruflet_record: open-ended ranges in where (18.., ..17, ...18) no longer match nothing #107 (open-ended ranges), which also edits sql.rb and the README, and f9d79b6 brings ruflet_record: end the transaction on return, break and throw #99 (transaction exits), which also edits sqlite_adapter.rb. Both merged without conflicts and this PR's changed lines are the same.

Tests

24 → 72 runs. api_surface_test.rb covers queries, find_or_*, joins, merge, load, schema changes, table definition types, migrations and Column casting. 35 planted mutations (reverts of each fix, plus each part of the JSON and -sse changes) all fail a test.

Verified on this head (60dc125)

Not done

  • ruflet_record is not in CI's test loop, and no meta-test enforces record API coverage.
  • json columns don't parse back into Ruby data. That would change what existing values read back as.

Datetimes are stored as UTC wall-clock text and read back with Time.utc.
The adapter formatted a bound Time with its own wall clock, so a local
Time (Time.now off UTC, or Time.new(..., "+02:00")) was stored hours
off. Column#cast also ignored the offset in a string such as
"2026-01-01T10:00:00+02:00".

Convert bound Times to UTC, keep cast Times as their UTC instant, and
apply a string's Z or +HH:MM offset (fractional seconds are accepted).
Schema's json columns were created as plain TEXT. Assigning a Hash
raised "no such bind parameter", since sqlite3 reads a Hash bind as named
parameters, and an Array bind was rejected. So a json column could only
hold strings the app encoded itself, and read back as strings.

Declare json columns as JSON, as Rails' SQLite adapter does, so
Column#type reports :json. Bind Hashes and Arrays as JSON text, and
parse the column's text back into Ruby data. Without the JSON library
(some embedded builds) binding a Hash or Array raises a clear
ArgumentError, and reads keep the text.
Table names, foreign key targets and association classes come from the
Inflector:
- pluralize turned every final y into "ies", so a Survey, Day or Key
  model looked for "surveies", "daies" or "keies" tables, and
  references(:survey, foreign_key: true) pointed at "surveies";
- singularize did not undo pluralize's "es" after ss or after status,
  bus and alias, so has_many :addresses looked for an Addresse class.

Keep a y after a vowel, and strip "es" in those cases.
belongs_to defines a writer (post.user = user), but assign_attributes
sent every key to write_attribute, which knows only columns. So
Post.create!(user: user) and post.update!(user: other) raised
UnknownAttributeError.

Use the writer for a key that is not a column but has one; other
unknown keys still raise.
…defined

inherited copied the parent's connection into each subclass when it was
defined. A model defined before ApplicationRecord.establish_connection
(models first, then the connection) therefore kept nil and raised
ConnectionNotEstablished. It also kept an old connection if its parent
reconnected.

Look the connection up the class chain instead. A model's own
establish_connection still takes precedence.
The SQLite adapter passed PRAGMA table_info's notnull flag as Column's
null argument, so null? answered the opposite of the schema.
Queries, find_or_*, joins, merge, load, add_where, destroy_all, column
metadata and reset, instantiate, validators, attribute accessors,
update_columns, touch, schema changes, table definition types, migrations
(up, change, down) and Column casting.
The earlier fix declared json columns JSON and parsed every String on
assignment and read. JSON gives the column NUMERIC affinity, so values
that worked before changed: "007" read back as 7, "true" and true as 1,
1.0 as 1, a big number string as a Float, and an app's own
JSON.generate text came back as a Hash, so JSON.parse(value) raised.
Tables made before the fix stayed TEXT, so the same app also saw
different types on old and new installs.

Keep json columns TEXT and do not parse them: every value that worked
before reads back unchanged. A Hash or Array is still bound as JSON
text instead of failing (CRuby) or storing inspect text (device VM).

The device VM's framework code cannot require json at load time, so the
previous commit's guard made a Hash raise RecordNotSaved on device even
though it worked under CRuby. The adapter now requires json the first
time it binds a Hash or Array; on device the VM loads its builtin json.
Where no JSON library loads (a bare mruby), the value is bound
unchanged, as before.
The datetime fix also converted an assigned Time to UTC in memory, so
Setting.new(at: Time.new(2026, 1, 1, 10, 0, 0, "+02:00")).at changed
from 10:00 +0200 to 08:00 UTC. Binding already stores the instant as
UTC, so the conversion was not needed. Keep the Time as assigned, as
before.

Also test a fractional-seconds string with an offset and Time#to_s
text, which no test covered.
The belongs_to fix sent any key that had a public writer to that
writer. That also changed keys no association defines: with
attr_accessor :draft, Post.new(draft: true) set it instead of raising
UnknownAttributeError, and operator names matched too: "=" and "!"
called == and != and dropped the value, and "[]" raised ArgumentError.

Record the names belongs_to defines, on the model or a parent, and use
the writer only for those. Every other unknown key raises
UnknownAttributeError, as before.
… only without one

The connection fix looked every model's connection up the class chain
on each call. That changed models that had a connection when they were
defined: they moved when their parent reconnected, kept the previous
database's cached columns ("table items has no column named extra"),
and a direct Base subclass used Base's connection instead of
RufletRecord.connection after RufletRecord.establish_connection.

Copy the parent's connection at definition again, as before. Only a
model that got none (defined before its abstract parent's
establish_connection) looks it up the chain, and Base's direct
subclasses still fall back to RufletRecord.connection.
establish_connection now also clears the cached columns of the models
that follow the class, so they read the new database's columns.
The inflector fix changed words the old rules handled correctly:
pluralize("soliloquy") became "soliloquys" (qu + y is a consonant y), and
singularize turned abuses, crevasses, impasses and posses into abus,
crevass, impass and poss.

Treat "quy" like a consonant y, keep known "sse" words (crevasse,
impasse, finesse, posse, lacrosse, largesse, demitasse, wrasse), and
strip "es" from "buses" only as a whole word, so abuses stays abuse.
…fore

The adapter JSON-encoded every Hash or Array bind, so raw
connection.execute/select_all and where("...", value) changed too. On
CRuby sqlite3 reads a Hash bind as named parameters and flattens an
Array into positional binds, and both worked before; they now bound
JSON text to the first placeholder and gave silently wrong results.

Encode at the model layer instead: the attribute values that create,
update, update_columns and update_all bind, and the values of Hash
conditions (where, where.not, find_by, IN-list items). Raw SQL binds
and SQL-string fragments go to the driver unchanged, and
bound_attributes still shows the values as given.
The "sses" -> "ss" rule broke every -sse word missing from SSE_PLURALS:
mousses became mouss, so has_many :mousses looked for a Mouss class.
A diff over /usr/share/dict/words found 47 such words.

List the -sse words whose -ss form is not the usual word (mousse,
bouillabaisse, coulisse, pelisse, noblesse, politesse, ...). Those that
as a suffix would also catch -ss plurals (fosses, tasses, osses, rasses,
casses: carcasses, grasses, bosses) match only as whole words. Where
the -ss word is the usual one (passes, masses, crosses, princesses),
it wins, as for addresses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant