summaryrefslogtreecommitdiff
path: root/test/support/integration_test.rb
blob: 86553c269c5f01293bd83d5d1f0d55392285ebbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Tapicero
  class IntegrationTest < MiniTest::Test

    #
    # create a dummy record for the user
    # so that tapicero will get a new user event
    #
    def create_user(fast = false, db_name = 'users')
      result = database(db_name).save_doc :some => :content
      raise RuntimeError.new(result.inspect) unless result['ok']
      sleep 1 unless fast # allow tapicero to do its job
      @user = {'_id' => result["id"], '_rev' => result["rev"]}
    end

    def delete_user(fast = false, db_name = 'users')
      return if @user.nil? or @user['_deleted']
      result = database(db_name).delete_doc @user
      raise RuntimeError.new(result.inspect) unless result['ok']
      @user['_deleted'] = true
      sleep 1 unless fast # allow tapicero to do its job
    end

    def user_database
      host.database(config.options[:db_prefix] + @user['_id'])
    rescue RestClient::ResourceNotFound
      puts 'failed to find per user db'
    end

    def database(db_name='users')
      @database ||= host.database!(database_name_with_prefix(db_name))
    end

    def database_name_with_prefix(db_name)
      config.complete_db_name(db_name)
    end

    def host
      @host ||= CouchRest.new(config.couch_host)
    end

    def config
      Tapicero.config
    end

    def assert_database_exists(db)
      db.info
    rescue RestClient::ResourceNotFound
      assert false, "Database #{db} should exist."
    end

  end
end