Sha256: 27c7d06d1d91a4218f6dab9b451baa5184e2cc710136c355aebb8dc625a8c5ee

Contents?: true

Size: 1.95 KB

Versions: 27

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true
require "spec_helper"

if defined?(GlobalID)
  GlobalID.app = "graphql-ruby-test"

  class GlobalIDUser
    include GlobalID::Identification

    attr_reader :id

    def initialize(id)
      @id = id
    end

    def self.find(id)
      GlobalIDUser.new(id)
    end

    def ==(that)
      self.id == that.id
    end
  end
end

describe GraphQL::Subscriptions::Serialize do
  def serialize_dump(v)
    GraphQL::Subscriptions::Serialize.dump(v)
  end

  def serialize_load(v)
    GraphQL::Subscriptions::Serialize.load(v)
  end

  if defined?(GlobalID)
    it "should serialize GlobalID::Identification in Array/Hash" do
      user_a = GlobalIDUser.new("a")
      user_b = GlobalIDUser.new("b")

      str_a = serialize_dump(["first", 2, user_a])
      str_b = serialize_dump({"first" => 'first', "second" => 2, "user" => user_b})

      assert_equal str_a, '["first",2,{"__gid__":"Z2lkOi8vZ3JhcGhxbC1ydWJ5LXRlc3QvR2xvYmFsSURVc2VyL2E"}]'
      assert_equal str_b, '{"first":"first","second":2,"user":{"__gid__":"Z2lkOi8vZ3JhcGhxbC1ydWJ5LXRlc3QvR2xvYmFsSURVc2VyL2I"}}'
    end

    it "should deserialize GlobalID::Identification in Array/Hash" do
      user_a = GlobalIDUser.new("a")
      user_b = GlobalIDUser.new("b")

      str_a = '["first",2,{"__gid__":"Z2lkOi8vZ3JhcGhxbC1ydWJ5LXRlc3QvR2xvYmFsSURVc2VyL2E"}]'
      str_b = '{"first":"first","second":2,"user":{"__gid__":"Z2lkOi8vZ3JhcGhxbC1ydWJ5LXRlc3QvR2xvYmFsSURVc2VyL2I"}}'

      parsed_obj_a = serialize_load(str_a)
      parsed_obj_b = serialize_load(str_b)

      assert_equal parsed_obj_a, ["first", 2, user_a]
      assert_equal parsed_obj_b, {'first' => 'first', 'second' => 2, 'user' => user_b}
    end
  end

  it "can deserialize symbols" do
    value = { a: :a, "b" => 2 }

    dumped = serialize_dump(value)
    expected_dumped = '{"a":{"__sym__":"a"},"b":2,"__sym_keys__":["a"]}'
    assert_equal expected_dumped, dumped
    loaded = serialize_load(dumped)
    assert_equal value, loaded
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
graphql-1.8.6 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.5 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.4 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.3 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.2 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.1 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.0 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.0.pre11 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.0.pre10 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.7.14 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.0.pre9 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.0.pre8 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.7.13 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.0.pre7 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.7.12 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.7.11 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.7.10 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.0.pre6 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.8.0.pre5 spec/graphql/subscriptions/serialize_spec.rb
graphql-1.7.9 spec/graphql/subscriptions/serialize_spec.rb