Module: MarkovTwitter::TestHelperMethods

Defined in:
lib/markov_twitter/test_helper_methods.rb

Overview

Methods which are included into test case via refinement, so they don’t interfere with application code and don’t require a namespace.

Constant Summary collapse

Authenticator =

alias

MarkovTwitter::Authenticator

Instance Method Summary collapse

Instance Method Details

#build_invalid_authenticatorAuthenticator

Builds an authenticator instance with invalid credentials. Should raise errors on subsequent operations.

Returns:



23
24
25
# File 'lib/markov_twitter/test_helper_methods.rb', line 23

def build_invalid_authenticator
  Authenticator.new(api_key: "", secret_key: "")
end

#build_valid_authenticatorAuthenticator

Builds an authenticator instance with valid credentials. Will raise an error unless the expected ENV vars are defined.

Returns:



13
14
15
16
17
18
# File 'lib/markov_twitter/test_helper_methods.rb', line 13

def build_valid_authenticator
  Authenticator.new(
    api_key: ENV.fetch("TWITTER_API_KEY"),
    secret_key: ENV.fetch("TWITTER_SECRET_KEY")
  )
end

#get_invalid_usernameString

This user should raise an error when the twitter gem looks them up.

Returns:

  • (String)


68
69
70
# File 'lib/markov_twitter/test_helper_methods.rb', line 68

def get_invalid_username
  "3u9r4j8fjniecn875jdpwqk32mdiy4584vuniwcoekpd932"
end

#get_many_tweets_usernameString

A twitter user which has many tweets. Used to test pagination of search results.

Returns:

  • (String)


75
76
77
# File 'lib/markov_twitter/test_helper_methods.rb', line 75

def get_many_tweets_username
  "SFist"
end

#get_sample_phrase_1String

A sample phrase used to test manipulations on the markov chain.

Returns:

  • (String)


159
160
161
# File 'lib/markov_twitter/test_helper_methods.rb', line 159

def get_sample_phrase_1
  "the cat in the hat"
end

#get_sample_phrase_2String

A sample phrase used to test manipulations on the markov chain.

Returns:

  • (String)


165
166
167
# File 'lib/markov_twitter/test_helper_methods.rb', line 165

def get_sample_phrase_2
  "the bat in the flat"
end

#get_sample_user_tweetsArray<String>

The expected tweets of the user I manually posted to on Twitter.

Returns:

  • (Array<String>)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/markov_twitter/test_helper_methods.rb', line 36

def get_sample_user_tweets
  [
    "don't ever change",
    "A long-term goal of mine is to create a water-based horror game. I've done some work on building this in Unity already.",
    "Many amazing looking animals can be kept in reasonably simple environments, but some require elaborate setups .",
    "I enjoy creating terrariums but it's a lot of work to keep everything balanced so that all the critters survive .",
    "Although I haven't had a cat myself, I have had aquariums, terrariums, and rodents at different points .",
    "i have personally never owned a pet cat, and I'm a bit allergic, but I still enjoy their company .",
    "carnivorous by nature, cats hunt many other wild animals such as gophers and mice. As a result, some people would prefer less outdoor cats .",
    "you have now unsubscribed to cat facts. respond with UNSUBSCRIBE to unsubscribe .",
    "egyption hairless cats are less allergenic than most other cats. they don't have hair and are probably less oily .",
    "the cat in the hat ate and sat. it got fat and couldn't catch a rat ."
  ]
end

#get_sample_usernameString

This is a twitter user I’ve created that has a fixed set of tweets. It’s there to make sure that fetching tweets works correctly.

Returns:

  • (String)


30
31
32
# File 'lib/markov_twitter/test_helper_methods.rb', line 30

def get_sample_username
  "max_p_sample"
end

#get_stubbed_many_tweets_user_tweetsArray<String>

Sample tweets for which the content does not matter. these are only used to test the pagination of Twitter results.

Returns:

  • (Array<String>)


62
63
64
# File 'lib/markov_twitter/test_helper_methods.rb', line 62

def get_stubbed_many_tweets_user_tweets
  20.times.map &:to_s
end

#stub_twitter_token_request_with_invalid_credentialsvoid

This method returns an undefined value.

Makes twitter’s oauth request fail. Returns without doing anything if DisableWebmock=true in ENV.



92
93
94
95
96
97
# File 'lib/markov_twitter/test_helper_methods.rb', line 92

def stub_twitter_token_request_with_invalid_credentials
  return if ENV["DisableWebmock"] == "true"
  stub_request(
    :post, "https://api.twitter.com/oauth2/token"
  ).to_return(status: 403, body: "", headers: {})
end

#stub_twitter_token_request_with_valid_credentialsvoid

This method returns an undefined value.

Makes twitter’s oauth request succeed. Returns without doing anything if DisableWebmock=true in ENV.



82
83
84
85
86
87
# File 'lib/markov_twitter/test_helper_methods.rb', line 82

def stub_twitter_token_request_with_valid_credentials
  return if ENV["DisableWebmock"] == "true"
  stub_request(
    :post, "https://api.twitter.com/oauth2/token"
  ).to_return(status: 200, body: "", headers: {})
end

#stub_twitter_user_lookup_request_with_invalid_username(username) ⇒ void

This method returns an undefined value.

Makes the twitter user lookup request fail. Returns without doing anything if DisableWebmock=true in ENV.

Parameters:

  • username (String)


113
114
115
116
117
118
# File 'lib/markov_twitter/test_helper_methods.rb', line 113

def stub_twitter_user_lookup_request_with_invalid_username(username)
  return if ENV["DisableWebmock"] == "true"
  stub_request( :get,
    "https://api.twitter.com/1.1/users/show.json?screen_name=#{username}"
  ).to_return(status: 404, body: {id: 0}.to_json, headers: {})    
end

#stub_twitter_user_lookup_request_with_valid_username(username) ⇒ void

This method returns an undefined value.

Makes the twitter user lookup request succeed. Returns without doing anything if DisableWebmock=true in ENV.



102
103
104
105
106
107
# File 'lib/markov_twitter/test_helper_methods.rb', line 102

def stub_twitter_user_lookup_request_with_valid_username(username)
  return if ENV["DisableWebmock"] == "true"
  stub_request( :get,
    "https://api.twitter.com/1.1/users/show.json?screen_name=#{username}"
  ).to_return(status: 200, body: {id: 0}.to_json, headers: {})
end

#stub_twitter_user_timeline_request(tweets_to_return) ⇒ void

This method returns an undefined value.

Makes the twitter user timeline request succeed. returns without doing anything if DisableWebmock=true in ENV.

Parameters:

  • tweets_to_return (Array<Hash>)
    • the hashes must have keys “id” and “text”.



125
126
127
128
129
130
131
# File 'lib/markov_twitter/test_helper_methods.rb', line 125

def stub_twitter_user_timeline_request(tweets_to_return)
  return if ENV["DisableWebmock"] == "true"
  stub_request(
    :get,
    "https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=0"
  ).to_return(status: 200, body: tweets_to_return.to_json, headers: {})
end

#to_tweet_objects(strings) ⇒ Array<Hash>

Converts strings into a specific structure that is used internally by the twitter gem. Used for stubbing with Webmock.

Parameters:

  • strings (Array<String>)

Returns:

  • (Array<Hash>)

    where each hash has :id and :text keys.



55
56
57
# File 'lib/markov_twitter/test_helper_methods.rb', line 55

def to_tweet_objects(strings)
  strings.map { |string| { id: 0, text: string } }
end

#validate_linkages(node, _next: nil, prev: nil, total_num_inputs: nil) ⇒ Object

Validates that the linkages on a node are as expected.

Parameters:

  • node (MarkovBuilder::Node)
  • _next (Hash<String,Float>) (defaults to: nil)

    mapping key to probability.

  • prev (Hash<String,Float>) (defaults to: nil)

    mapping key to probability.

  • total_num_inputs (Hash<Symbol,Integer>) (defaults to: nil)

    keyed by direction.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/markov_twitter/test_helper_methods.rb', line 138

def validate_linkages(node, _next: nil, prev: nil, total_num_inputs: nil)
  precision = 0.00001
  if _next
    node.linkages[:next].each do |name, linkage|
      expect(linkage).to be_within(precision).of(_next[name])
    end
  end
   if prev
    node.linkages[:prev].each do |name, linkage|
      expect(linkage).to be_within(precision).of(prev[name])
    end
  end
   if total_num_inputs
    expect(node.total_num_inputs).to eq(total_num_inputs)
  end
rescue
  binding.pry
end