Class: DataValidation::Comparison::ComparisonBase

Inherits:
Object
  • Object
show all
Defined in:
lib/data_validation/comparison/comparison_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(web_url) ⇒ ComparisonBase



9
10
11
# File 'lib/data_validation/comparison/comparison_base.rb', line 9

def initialize(web_url)
  @web_url = web_url
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



7
8
9
# File 'lib/data_validation/comparison/comparison_base.rb', line 7

def category
  @category
end

#division_idObject (readonly)

Returns the value of attribute division_id.



7
8
9
# File 'lib/data_validation/comparison/comparison_base.rb', line 7

def division_id
  @division_id
end

#logger_nameObject (readonly)

Returns the value of attribute logger_name.



7
8
9
# File 'lib/data_validation/comparison/comparison_base.rb', line 7

def logger_name
  @logger_name
end

#seasonObject (readonly)

Returns the value of attribute season.



7
8
9
# File 'lib/data_validation/comparison/comparison_base.rb', line 7

def season
  @season
end

#web_urlObject (readonly)

Returns the value of attribute web_url.



7
8
9
# File 'lib/data_validation/comparison/comparison_base.rb', line 7

def web_url
  @web_url
end

Instance Method Details

#check_item(item, team, re_check) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/data_validation/comparison/comparison_base.rb', line 60

def check_item(item, team, re_check)
  error_info = []
  i = 0
  rank_value = nil # the value used to rank

  field_array.each do |field_name|

    if re_check && field_name == related_rank_field
      i += 1
      next
    end

    if field_name.is_a? String
      team_value = team[field_name]
    elsif field_name.is_a? Array
      team_value = team[field_name[0]][field_name[1]]
    else
      i += 1
      next
    end

    if item.length < field_array.length && empty_field && empty_field == field_name ## when the field is empty in the table
      error_info << "team #{item[0]}'s #{field_name} is not empty: #{team_value}" unless (team_value == 0 || team_value.nil?)
      next
    end


    if compare_values(item[i], team_value)

      if field_name == rank_field
        rank_value = team_value
      end
    else
      error_info << "team #{item[0]}'s #{field_name}: #{item[i]} -- #{team_value}"
    end

    i += 1
  end
  [error_info, rank_value]
end

#compareObject



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
53
54
55
56
57
58
# File 'lib/data_validation/comparison/comparison_base.rb', line 13

def compare
  if api_path.nil?
    raise 'Please define api_path'
  end

  if field_array.nil?
    raise 'Please define field_array'
  end

  data_access = DataValidation::DataAccess.new(web_url, DataValidation.api_request_host + api_path, determine_request_body_and_logger_name)

  puts 'requesting web page...'
  if web_url.match /rating[a-z]+.php/
    web_data = data_access.get_regional_rating_table_data
  else
    web_data = data_access.get_main_table_data
  end

  puts 'requesting mobile api data: ' + DataValidation.api_request_host + api_path
  api_data = data_access.get_response_from_mobile_api

  if web_data.length != api_data['results']['results']['teams'].length
    logger.error "The record size between web page and api data is not the same: #{web_data.length} -- #{api_data['results']['results']['teams'].length}"
    return
  end

  web_data.each_with_index do |item, index|
    teams = api_data['results']['results']['teams']
    team = teams[index]

    error_log = []

    error_info, rank_value = check_item(item, team, false)
    error_log << error_info

    if error_info.size > 0 && rank_value
      possible_teams = teams.select { |team| team[rank_field] == rank_value }
      possible_teams.each do |team|
        error, value = check_item(item, team, true)
        error_log << error
      end
    end

    error_log.min_by { |log| log.length }.each { |item| logger.error item }
  end
end

#compare_conf_standings(api_data, data_access) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/data_validation/comparison/comparison_base.rb', line 107

def compare_conf_standings(api_data, data_access)
  puts 'Start to compare conf ranking tables'
  conf_standing_data = data_access.conf_standing_data

  conf_standing_data.each do |standing|
    full_conf = ''
    standing.each_with_index do |record, i|
      if i == 0
        state = record[0]
        team_class = record[1]
        conf = record[2]
        full_conf = "#{state}, #{team_class}, #{conf}"

        api_data = data_access.get_response_from_mobile_api_with_params(conf_rank_api_url, %Q|{"season": "#{season}", "conference": {"category": "#{category}", "conference": "#{conf}", "divisionId": #{division_id.to_i}, "state": "#{state}", "teamClass": "#{team_class}"}, "currPage": 1, "pageSize": 1000}|)

        if api_data['results']['results']['total'] != standing.length - 1
          logger.error "standing for #{full_conf} has incorrect length: #{standing.length - 1} : #{api_data['results']['results']['total']}"
          break
        end
      else

        team_data = api_data['results']['results']['teams'][i - 1]

        record.each_with_index do |item, index|
          if index == 0 && item.to_i != team_data['rank']
          elsif index == 1 && !compare_floats(team_data['powerRating'], item)
            logger.error "team #{i} powerRating in #{full_conf}: #{item.to_f} -- #{team_data['powerRating']}"
          elsif index == 2 && item.to_i != team_data['conference']['wins']
            logger.error "team #{i} conference wins in #{full_conf}: #{item.to_i} -- #{team_data['conference']['wins']}"
          elsif index == 3 && item.to_i != team_data['conference']['losses']
            logger.error "team #{i} conference losses in #{full_conf}: #{item.to_i} -- #{team_data['conference']['losses']}"
          elsif index == 4 && item.to_i != team_data['conference']['ties']
            logger.error "team #{i} conference ties in #{full_conf}: #{item.to_i} -- #{team_data['conference']['ties']}"
          elsif index == 5 && !compare_floats(team_data['winLossConf'], item)
            logger.error "team #{i} winLossConf in #{full_conf}: #{item.to_f} -- #{team_data['winLossConf']}"
          elsif index == 6 && item.to_i != team_data['total']['wins']
            logger.error "team #{i} total wins in #{full_conf}: #{item.to_i} -- #{team_data['total']['wins']}"
          elsif index == 7 && item.to_i != team_data['total']['losses']
            logger.error "team #{i} total losses in #{full_conf}: #{item.to_i} -- #{team_data['total']['losses']}"
          elsif index == 8 && item.to_i != team_data['total']['ties']
            logger.error "team #{i} total ties in #{full_conf}: #{item.to_i} -- #{team_data['total']['ties']}"
          elsif index == 9 && !compare_floats(team_data['winLossTotal'], item)
            logger.error "team #{i} winLossTotal in #{full_conf}: #{item.to_f} -- #{team_data['winLossTotal']}"
          end
        end
      end

    end
  end
end

#compare_values(page_item, team_value) ⇒ Object



101
102
103
104
105
# File 'lib/data_validation/comparison/comparison_base.rb', line 101

def compare_values(page_item, team_value)
  (team_value.is_a?(String) && team_value.include?(page_item.strip)) ||
    (team_value.is_a?(Float) && compare_floats(team_value, page_item)) ||
    (team_value.is_a?(Integer) && team_value == page_item.to_i)
end