Sha256: cb2aa63d79f3683911b0f2b4de202d16eeec26621a7e1edb9b4bade39c029956

Contents?: true

Size: 1.3 KB

Versions: 11

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true
require "test_helper"

class Measured::Cache::JsonTest < ActiveSupport::TestCase
  setup do
    @cache = Measured::Cache::Json.new("test.json")
    @table_json = { "a" => { "b" => { "numerator" => 2, "denominator" => 3 } } }.to_json
    @table_hash = { "a" => { "b" => Rational(2, 3) } }
  end

  test "#initialize sets the filename and path" do
    assert_equal "test.json", @cache.filename
    assert_match(/.+\/cache\/test\.json$/, @cache.path.to_s)
    refute_match "../", @cache.path.to_s
  end

  test "#exist? returns false if the file does not exist" do
    File.expects(:exist?).with(@cache.path).returns(false)
    refute_predicate @cache, :exist?
  end

  test "#exist? returns true if the file exists" do
    File.expects(:exist?).with(@cache.path).returns(true)
    assert_predicate @cache, :exist?
  end

  test "#read returns nil if the file does not exist" do
    File.expects(:exist?).with(@cache.path).returns(false)
    assert_nil @cache.read
  end

  test "#read loads the file if it exists" do
    File.expects(:exist?).with(@cache.path).returns(true)
    File.expects(:read).with(@cache.path).returns(@table_json)
    assert_equal @table_hash, @cache.read
  end

  test "#write raises not implemented" do
    assert_raises(ArgumentError) do
      @cache.write({})
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
measured-3.2.0 test/cache/json_test.rb
measured-3.1.0 test/cache/json_test.rb
measured-3.0.0 test/cache/json_test.rb
measured-2.8.2 test/cache/json_test.rb
measured-2.8.1 test/cache/json_test.rb
measured-2.8.0 test/cache/json_test.rb
measured-2.7.1 test/cache/json_test.rb
measured-2.7.0 test/cache/json_test.rb
measured-2.6.0 test/cache/json_test.rb
measured-2.5.2 test/cache/json_test.rb
measured-2.5.1 test/cache/json_test.rb