Sha256: 623a2729a56e7401e8b8f7d85a8f42c0a9cdcb9bb394302bd3f0cc006cc23ecb

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

require 'spec_helper'

describe DiskStore do
  let(:cache) { DiskStore.new @tmpdir }
  let(:file_contents) { SecureRandom.random_bytes(8192) }
  let(:file) { Tempfile.new("My Temp File.psd") }
  let(:key) { "doge" }

  around(:each) do |example|
    Dir.mktmpdir do |tmpdir|
      @tmpdir = tmpdir
      example.run
    end
  end

  before(:each) do
    file.binmode
    file.write file_contents
    file.flush
  end

  context "initializing" do
    subject { DiskStore.new }
    it { should respond_to(:read) }
    it { should respond_to(:write) }
    it { should respond_to(:fetch) }
    it { should respond_to(:delete) }
    it { should respond_to(:exist?) }
  end

  context "#read" do
    it "reads from disk" do
      cache.write(key, file)
      file.close
      file.unlink # ensure the tempfile is deleted
      expect(cache.read(key)).to be_an_instance_of(File)
      expect(cache.read(key).read).to eq file_contents
    end
  end

  context "#write" do
    it "writes a file to disk" do
      expect{
        cache.write(key, file)
      }.to change{ Dir[File.join(@tmpdir, "**/*")].size }.by(3)

      file_path = Dir[File.join(@tmpdir, "**/*")].last
      expect(File.binread(file_path)).to eq file_contents
    end
  end

  context "#delete" do
    it "deletes the file from disk" do
      cache.write(key, file)
      expect{
        expect(cache.delete(key)).to be_true
      }.to change{ Dir[File.join(@tmpdir, "**/*")].size }.by(-3)
    end
  end

  context "#exist?" do
    it "returns correct value" do
      expect(cache.exist?(key)).to be_false
      cache.write(key, file)
      expect(cache.exist?(key)).to be_true
      cache.delete(key)
      expect(cache.exist?(key)).to be_false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
disk_store-0.3.0 spec/disk_store_spec.rb
disk_store-0.2.2 spec/disk_store_spec.rb