Sha256: 54c0fa5fb3c3311d33c77042e14dafc7bfcceaa2b3313b948f831bec32e3447f

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

require './lib/couchpillow.rb'

require 'minitest/autorun'
require 'minitest/unit'
require 'mocha/mini_test'


class FakeCouchbaseServer

  def initialize
    @storage = {}
    @cas = {}
    @ttl = {}
  end


  def set id, data, opts = {}
    raise Couchbase::Error::KeyExists if @storage.has_key?(id) && opts[:cas] && @cas[id] != opts[:cas]
    @storage[id] = data
    @cas[id] = SecureRandom.hex(8)
    @ttl[id] = opts[:ttl] if opts[:ttl]
    data
  end


  def delete id
    @storage.delete(id)
    @cas.delete(id)
    @ttl.delete(id)
  end


  def ttl_value id
    @ttl[id]
  end


  def replace id, data, opts = {}
    raise "Document does not exist" unless @storage.has_key?(id)
    raise Couchbase::Error::KeyExists if @storage.has_key?(id) && opts[:cas] && @cas[id] != opts[:cas]

    @storage[id] = data
    @cas[id] = SecureRandom.hex(8)
  end


  def get *args
    opts = has_opts?(args) ? args.pop : {}
    ids = args
    if ids.length == 1
      id = ids.first
      opts[:extended] == true and
        retval = [@storage[id], nil, @cas[id]] or
        retval = @storage[id]
    else
      retval = ids.inject({}) do |memo, id|
        opts[:extended] == true and
          memo[id] = [@storage[id], nil, @cas[id]] or
          memo[id] = @storage[id]
        memo
      end
    end
    retval
  end

  def has_opts? args
    args.any? { |v| v.is_a?(Hash) }
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
couchpillow-0.4.12 test/helper.rb
couchpillow-0.4.11 test/helper.rb
couchpillow-0.4.10 test/helper.rb