|
3 | 3 | require "abstract_unit"
|
4 | 4 | require "fixtures/person"
|
5 | 5 |
|
| 6 | +require "active_record" |
| 7 | + |
| 8 | +ENV["DATABASE_URL"] = "sqlite3::memory:" |
| 9 | + |
| 10 | +ActiveRecord::Base.establish_connection |
| 11 | + |
| 12 | +ActiveRecord::Schema.define do |
| 13 | + create_table :users, force: true do |t| |
| 14 | + t.text :person_text |
| 15 | + t.json :person_json |
| 16 | + |
| 17 | + t.check_constraint <<~SQL, name: "person_json_is_object" |
| 18 | + JSON_TYPE(person_json) = 'object' |
| 19 | + SQL |
| 20 | + end |
| 21 | + |
| 22 | + create_table :teams, force: true do |t| |
| 23 | + t.text :people_text |
| 24 | + t.text :paginated_people_text |
| 25 | + t.json :people_json |
| 26 | + t.json :paginated_people_json |
| 27 | + |
| 28 | + t.check_constraint <<~SQL, name: "people_json_is_object" |
| 29 | + JSON_TYPE(people_json) = 'array' |
| 30 | + SQL |
| 31 | + t.check_constraint <<~SQL, name: "paginated_people_json_is_object" |
| 32 | + JSON_TYPE(paginated_people_json) = 'object' |
| 33 | + SQL |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +class User < ActiveRecord::Base |
| 38 | + if ActiveSupport::VERSION::MAJOR < 8 && ActiveSupport::VERSION::MINOR < 1 |
| 39 | + serialize :person_text, Person |
| 40 | + serialize :person_json, ActiveResource::Coder.new(Person, :serializable_hash) |
| 41 | + else |
| 42 | + serialize :person_text, coder: Person |
| 43 | + serialize :person_json, coder: ActiveResource::Coder.new(Person, :serializable_hash) |
| 44 | + end |
| 45 | +end |
| 46 | + |
6 | 47 | class SerializationTest < ActiveSupport::TestCase
|
| 48 | + include ActiveRecord::TestFixtures |
| 49 | + |
| 50 | + test "dumps to a text column" do |
| 51 | + resource = Person.new({ id: 1, name: "Matz" }, true) |
| 52 | + |
| 53 | + User.create!(person_text: resource) |
| 54 | + |
| 55 | + user = User.sole |
| 56 | + assert_equal resource.to_json, user.person_text_before_type_cast |
| 57 | + end |
| 58 | + |
| 59 | + test "dumps to a json column" do |
| 60 | + resource = Person.new({ id: 1, name: "Matz" }, true) |
| 61 | + |
| 62 | + User.create!(person_json: resource) |
| 63 | + |
| 64 | + user = User.sole |
| 65 | + assert_equal resource.serializable_hash.to_json, user.person_json_before_type_cast |
| 66 | + end |
| 67 | + |
| 68 | + test "loads from a text column" do |
| 69 | + resource = Person.new(id: 1, name: "Matz") |
| 70 | + |
| 71 | + User.connection.execute(<<~SQL) |
| 72 | + INSERT INTO users(person_text) |
| 73 | + VALUES ('#{resource.encode}') |
| 74 | + SQL |
| 75 | + |
| 76 | + user = User.sole |
| 77 | + assert_predicate user.person_text, :persisted? |
| 78 | + assert_equal resource, user.person_text |
| 79 | + assert_equal resource.attributes, user.person_text.attributes |
| 80 | + end |
| 81 | + |
| 82 | + test "loads from a json column" do |
| 83 | + resource = Person.new(id: 1, name: "Matz") |
| 84 | + |
| 85 | + User.connection.execute(<<~SQL) |
| 86 | + INSERT INTO users(person_json) |
| 87 | + VALUES ('#{resource.encode}') |
| 88 | + SQL |
| 89 | + |
| 90 | + user = User.sole |
| 91 | + assert_predicate user.person_json, :persisted? |
| 92 | + assert_equal resource, user.person_json |
| 93 | + assert_equal resource.attributes, user.person_json.attributes |
| 94 | + end |
7 | 95 | test ".load delegates to the .coder" do
|
8 | 96 | resource = Person.new(id: 1, name: "Matz")
|
9 | 97 |
|
|
0 commit comments