|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "cases/helper" |
| 4 | +require "models/user" |
| 5 | + |
| 6 | +module ActiveModel |
| 7 | + module Type |
| 8 | + class CollectionTest < ActiveModel::TestCase |
| 9 | + setup do |
| 10 | + @element_type = Minitest::Mock.new |
| 11 | + @serializer = Minitest::Mock.new |
| 12 | + ActiveModel::Type.stub(:lookup, @element_type) do |
| 13 | + @collection = Collection.new(element_type: :element_type, serializer: @serializer) |
| 14 | + end |
| 15 | + @my_collection = [1, 2, 3] |
| 16 | + end |
| 17 | + |
| 18 | + test "#valid_value? returns false if value is not an array" do |
| 19 | + assert_not @collection.valid_value?("Nikita") |
| 20 | + end |
| 21 | + |
| 22 | + test "#valid_value? returns false if value is not an array of valid type_objects" do |
| 23 | + @element_type.expect(:valid_value?, false, ["Nikita"]) |
| 24 | + assert_not @collection.valid_value?(["Nikita"]) |
| 25 | + end |
| 26 | + |
| 27 | + test "#assert_valid_value doesn't raise if valid_value? returns true" do |
| 28 | + assert_nothing_raised do |
| 29 | + @collection.stub(:valid_value?, true) do |
| 30 | + @collection.assert_valid_value(["Nikita"]) |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + test "#assert_valid_value raises ArgumentError if valid_value? returns false" do |
| 36 | + err = assert_raises(ArgumentError) do |
| 37 | + @collection.stub(:valid_value?, false) do |
| 38 | + @collection.assert_valid_value({ name: "Nikita" }) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + assert_equal "'{:name=>\"Nikita\"}' is not a valid collection of element_type", err.message |
| 43 | + end |
| 44 | + |
| 45 | + test "#valid_value? delegates valid_value? check to the element type" do |
| 46 | + @my_collection.each { |i| @element_type.expect(:valid_value?, true, [i]) } |
| 47 | + assert @collection.valid_value?(@my_collection) |
| 48 | + end |
| 49 | + |
| 50 | + test "#serialize delegates serialize check to the element type" do |
| 51 | + serialized_items = @my_collection.map { |i| "serialized #{i}" } |
| 52 | + @my_collection.each_with_index do |item, index| |
| 53 | + @element_type.expect(:serialize, serialized_items[index], [item]) |
| 54 | + end |
| 55 | + expected = "serialized_collection: #{serialized_items}" |
| 56 | + @serializer.expect(:encode, expected, [serialized_items]) |
| 57 | + assert_equal expected, @collection.serialize(@my_collection) |
| 58 | + end |
| 59 | + |
| 60 | + test "#deserialize delegates deserialize check to the element type" do |
| 61 | + serialized_collection = "serialized collection" |
| 62 | + @my_collection.each { |i| @element_type.expect(:deserialize, "deserialized #{i}", [i]) } |
| 63 | + expected = @my_collection.map { |i| "deserialized #{i}" } |
| 64 | + @serializer.expect(:decode, @my_collection, [serialized_collection]) |
| 65 | + |
| 66 | + assert_equal expected, @collection.deserialize(serialized_collection) |
| 67 | + end |
| 68 | + |
| 69 | + test "#serializable? delegates serializable? check to the element type" do |
| 70 | + @my_collection.each { |i| @element_type.expect(:serializable?, true, [i]) } |
| 71 | + assert @collection.serializable?(@my_collection) |
| 72 | + end |
| 73 | + |
| 74 | + test "#changed_in_place? delegates changed_in_place? check to the element type" do |
| 75 | + my_collection_raw = "my_serialized_collection" |
| 76 | + @my_collection.each do |el| |
| 77 | + @element_type.expect(:changed_in_place?, false, [el, el]) |
| 78 | + end |
| 79 | + |
| 80 | + @collection.stub(:deserialize, @my_collection, [my_collection_raw]) do |
| 81 | + assert_not @collection.changed_in_place?(my_collection_raw, @my_collection) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + test "#changed_in_place? returns true if size of new and old collections is different" do |
| 86 | + my_collection_raw = "my_serialized_collection" |
| 87 | + |
| 88 | + @collection.stub(:deserialize, @my_collection, [my_collection_raw]) do |
| 89 | + assert @collection.changed_in_place?(my_collection_raw, [1]) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + test "#changed_in_place? returns true if collections are the same size but with different elements" do |
| 94 | + new_collection = [1, "changed", 3] |
| 95 | + my_collection_raw = "my_serialized_collection" |
| 96 | + @element_type.expect(:changed_in_place?, false, [1, 1]) |
| 97 | + @element_type.expect(:changed_in_place?, true, [2, "changed"]) |
| 98 | + |
| 99 | + @collection.stub(:deserialize, @my_collection, [my_collection_raw]) do |
| 100 | + assert @collection.changed_in_place?(my_collection_raw, new_collection) |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + test "#cast returns an empty array if value is nil" do |
| 105 | + assert_equal [], @collection.cast(nil) |
| 106 | + end |
| 107 | + |
| 108 | + test "#cast wraps false value in an array" do |
| 109 | + @element_type.expect(:cast, false, [false]) |
| 110 | + assert_equal [false], @collection.cast(false) |
| 111 | + end |
| 112 | + |
| 113 | + test "#cast wraps value in an array if value is not an array" do |
| 114 | + @element_type.expect(:cast, 1, [1]) |
| 115 | + assert_equal [1], @collection.cast(1) |
| 116 | + end |
| 117 | + |
| 118 | + test "#cast delegates cast to the element type" do |
| 119 | + string_collection = ["1", "2", "3"] |
| 120 | + string_collection.each { |el| @element_type.expect(:cast, el.to_i, [el]) } |
| 121 | + |
| 122 | + assert_equal @my_collection, @collection.cast(string_collection) |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | +end |
0 commit comments