module ViewModel::TestHelpers

def assert_model_represents_database(model, been_there: Set.new)

def assert_model_represents_database(model, been_there: Set.new)
  return if been_there.include?(model)
  been_there << model
  refute(model.new_record?, 'model represents database entity')
  refute(model.changed?, 'model is fully persisted')
  database_model = model.class.find(model.id)
  assert_equal(database_model.attributes,
               model.attributes,
               'in memory attributes match database attributes')
  model.class.reflections.each do |_, reflection|
    association = model.association(reflection.name)
    next unless association.loaded?
    case
    when association.target.nil?
      assert_nil(database_model.association(reflection.name).target,
                 'in memory nil association matches database')
    when reflection.collection?
      association.target.each do |associated_model|
        assert_model_represents_database(associated_model, been_there: been_there)
      end
    else
      assert_model_represents_database(association.target, been_there: been_there)
    end
  end
end