test

Ruby code posted
created at 02 Dec 16:52

Edit | Back
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
53
54
55
56
57
58
59
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
100
101
102
103
# frozen_string_literal: true

class Scheduler
  def self.needs_updating_ids
    # TODO: Add your resolution here
    movies = Movie.where(needs_update: true)

    response = Faraday.get('https://imdb-remote-api.com/')
    # response.body
    response_body = JSON.parse(response.body)
    response_body

    movies_for_update = []
    movies.each do |movie|
      api_movie  = response_body.find{ |var| var["id"] == movie.id }
      movies_for_update << movie.id if check_current_label(api_movie, movie)
    end

    movies_for_update
  end

  def self.schedule
    # TODO: Add your resolution here
    []
  end

  def self.check_current_label(hash_movie, movie)
    current_label_value = get_label_value(hash_movie["rating"].to_f)

    current_label_value != movie.label
  end

  def self.get_label_value(label_value)
    case label_value
    when 0..1.99
    'Poor' 
    when  2..3.99
      'Below Average'
    when 4..5.99
    'Average'
    when 6..7.99
    'Good'
    when 8..10
        'Excellent'
    end
  end
end



# frozen_string_literal: true

RSpec.describe Scheduler do
  before do
    # Seed database
    File.read(scenario_file).each_line do |line|
      id, name, label, needs_update = line.gsub("\n","").split(",")
      Movie.create!(id: id, name: name, label: label, needs_update: needs_update)
    end
  end

  context "scenario 1", vcr: { cassette_name: "scenario1" } do
    let(:scenario_file) { "./spec/db/scenario1.txt" }
    let(:needs_updating) { [1,4] }

    it "finds the IDs that need updating" do
      result = Scheduler.needs_updating_ids
      expect(result).to match_array needs_updating
    end

    it "creates the update schedule" do
      scheduled = Scheduler.schedule
      expect(scheduled).to eq([]) # replace this array with the correct batches schedule
    end
  end

  context "scenario 2", vcr: { cassette_name: "scenario2" } do
    let(:scenario_file) { "./spec/db/scenario2.txt" }
    let(:needs_updating) { [1,2,5,6,8] }

    it "finds the IDs that need updating" do
      result = Scheduler.needs_updating_ids # replace this with your result
      expect(result).to match_array needs_updating
    end

    it "creates the update schedule" do
      scheduled = Scheduler.schedule
      expect(scheduled).to eq([]) # replace this array with the correct batches schedule
    end
  end

  context "scenario 3", vcr: { cassette_name: "scenario3" } do
    let(:scenario_file) { "spec/db/scenario3.txt" }
    let(:needs_updating) { File.read("spec/db/scenario3-updates.txt").split.map(&:to_i) }

    it "finds the IDs that need updating" do
      result = Scheduler.needs_updating_ids # replace this with your result
      expect(result).to match_array needs_updating
    end

    it "creates the update schedule"
  end
end
2.8 KB in 4 ms with coderay