Sha256: 9e0ff286898f42a9842f1025d48972e7d2fb8f84df4f5b9d1dfd13b09730429f

Contents?: true

Size: 1.85 KB

Versions: 12

Compression:

Stored size: 1.85 KB

Contents

require 'test_helper'
require 'wovnrb/text_caches/cache_base'
require 'wovnrb/text_caches/memory_cache'
require 'wovnrb/api_data'
require 'webmock/minitest'

module Wovnrb
  class MemoryCacheTest < WovnMiniTest
    def setup
      Wovnrb::CacheBase.set_single({})
    end

    def teardown
      Wovnrb::CacheBase.reset_cache
      WebMock.reset!
    end

    def test_initialize
      Wovnrb::ApiData.new('http://wwww.example.com', Wovnrb::Store.instance)
    end

    def test_get_data
      token = 'a'
      url = 'url'
      stub_request(:get, "https://api.wovn.io/v0/values?token=#{token}&url=#{url}").
        to_return(:body => '{"test_body": "a"}')
      store = Wovnrb::Store.instance
      store.settings['user_token'] = token
      api_data = Wovnrb::ApiData.new(url, store)

      assert_equal({'test_body' => 'a'}, api_data.get_data)
    end

    def test_get_data_when_cache_exists
      token = 'a'
      url = 'url'
      stub = stub_request(:get, "https://api.wovn.io/v0/values?token=#{token}&url=#{url}").
        to_return(:body => '{"test_body": "a"}')
      store = Wovnrb::Store.instance
      store.settings['user_token'] = token
      api_data = Wovnrb::ApiData.new(url, store)

      assert_equal({'test_body' => 'a'}, api_data.get_data)
      assert_equal({'test_body' => 'a'}, api_data.get_data)
      assert_requested(stub, :times => 1)
    end

    def test_get_data_fail
      token = 'a'
      url = 'url'
      stub_request(:get, "https://api.wovn.io/v0/values?token=#{token}&url=#{url}").
        to_return(:status => [500, "Internal Server Error"])
      store = Wovnrb::Store.instance
      store.settings['user_token'] = token
      api_data = Wovnrb::ApiData.new(url, store)
      log_mock = Wovnrb::LogMock.mock_log

      assert_equal({}, api_data.get_data)
      assert(log_mock.errors[0].start_with?('API server GET request failed'))
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
wovnrb-0.2.07.1 test/lib/api_data_test.rb
wovnrb-0.2.17 test/lib/api_data_test.rb
wovnrb-0.2.16 test/lib/api_data_test.rb
wovnrb-0.2.15 test/lib/api_data_test.rb
wovnrb-0.2.14 test/lib/api_data_test.rb
wovnrb-0.2.13 test/lib/api_data_test.rb
wovnrb-0.2.12 test/lib/api_data_test.rb
wovnrb-0.2.11 test/lib/api_data_test.rb
wovnrb-0.2.10 test/lib/api_data_test.rb
wovnrb-0.2.09 test/lib/api_data_test.rb
wovnrb-0.2.08 test/lib/api_data_test.rb
wovnrb-0.2.07 test/lib/api_data_test.rb