mitchellh-rubyuw 0.7.2 → 0.99.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,87 +1,158 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- class MockedCurriculumEnrollmentTest < Test::Unit::TestCase
4
- context "fetching curriculum enrollment information" do
5
- setup do
6
- @myuw = MyUW.new
7
- @myuw.stubs(:logged_in?).returns(true)
8
- @browser = @myuw.browser
9
- @browser.follow_meta_refresh = false
10
- @curriculum = @myuw.curriculum_enrollment("CSE", "AUT+2009")
11
- end
12
-
13
- should "return a MyUW::CurriculumEnrollment object when calling MyUW.curriculum" do
14
- curric_obj = @myuw.curriculum_enrollment("CSE", "AUT+2009")
15
- assert curric_obj.kind_of?(MyUW::CurriculumEnrollment)
16
- assert_equal "CSE", curric_obj.curriculum
17
- assert_equal "AUT+2009", curric_obj.term
18
- end
19
-
20
- should "raise a NotLoggedInError if not logged in and requesting enrollment information" do
21
- @myuw.stubs(:logged_in?).returns(false)
3
+ class CurriculumEnrollmentTest < Test::Unit::TestCase
4
+ setup do
5
+ RubyUW::CurriculumEnrollment.publicize_singleton_methods
6
+ end
7
+
8
+ teardown do
9
+ RubyUW::CurriculumEnrollment.protect_singleton_methods
10
+ end
11
+
12
+ context "finding an SLN" do
13
+ context "with authentication" do
14
+ should "raise an error if not authenticated" do
15
+ RubyUW::Base.expects(:authenticated?).returns(false)
16
+ assert_raises(RubyUW::Errors::NotLoggedInError) { RubyUW::CurriculumEnrollment.find("foo", "bar") }
17
+ end
22
18
 
23
- assert_raise MyUW::NotLoggedInError do
24
- @curriculum.data_by_sln("foo", true)
19
+ should "not raise an error if authenticated" do
20
+ RubyUW::Base.expects(:authenticated?).returns(true)
21
+ assert_nothing_raised { RubyUW::CurriculumEnrollment.find("foo", "bar") }
25
22
  end
26
23
  end
27
24
 
28
- should "raise an ArgumentError if a curriculum is not set" do
29
- @curriculum.curriculum = nil
30
- assert !@curriculum.curriculum
25
+ context "with checking if a curriculum exists or not" do
26
+ should "return false if the curric does not exist string is found" do
27
+ mocked_page = RubyUW::Base.connection.get(path_to_html("curric_no_curric_found"))
28
+
29
+ assert !RubyUW::CurriculumEnrollment.curriculum_exists?(mocked_page)
30
+ end
31
31
 
32
- assert_raise ArgumentError do
33
- @curriculum.data_by_sln("foo", true)
32
+ should "return true if the string is not found" do
33
+ mocked_page = RubyUW::Base.connection.get(path_to_html("curric_courses"))
34
+
35
+ assert RubyUW::CurriculumEnrollment.curriculum_exists?(mocked_page)
34
36
  end
35
37
  end
36
38
 
37
- should "raise an ArgumentError if a term is not set" do
38
- @curriculum.term = nil
39
- assert !@curriculum.term
39
+ context "raising exceptions on failures" do
40
+ setup do
41
+ RubyUW::Base.stubs(:authenticated?).returns(true)
42
+ RubyUW::CurriculumEnrollment.stubs(:curriculum_exists?).returns(true)
43
+
44
+ page = mock('page')
45
+ RubyUW::Base.connection.stubs(:get).returns(page)
46
+
47
+ @term = "bar"
48
+ @curric = "foo"
49
+ end
40
50
 
41
- assert_raise ArgumentError do
42
- @curriculum.data_by_sln("foo", true)
51
+ should "raise CurriculumDoesNotExistError if curric does not exist" do
52
+ RubyUW::CurriculumEnrollment.expects(:curriculum_exists?).returns(false)
53
+ assert_raises(RubyUW::Errors::CurriculumDoesNotExistError) { RubyUW::CurriculumEnrollment.find(@curric, @term) }
43
54
  end
44
- end
45
-
46
- should "raise an InvalidCurriculumError if it can't find the curriculum info" do
47
- fixture_page = @browser.get(path_to_html("curric_no_curric_found"))
48
- @browser.expects(:get).returns(fixture_page)
49
55
 
50
- assert_raise MyUW::InvalidCurriculumError do
51
- @curriculum.get_dept_page
56
+ should "raise CurriculumDoesNotExistError if mechanize throws a response code error" do
57
+ error = mock('error')
58
+ error.stubs(:code).returns("404")
59
+ RubyUW::Base.connection.stubs(:get).raises(WWW::Mechanize::ResponseCodeError, error)
60
+ assert_raises(RubyUW::Errors::CurriculumDoesNotExistError) { RubyUW::CurriculumEnrollment.find(@curric, @term) }
52
61
  end
53
62
  end
54
- end
55
-
56
- context "extracting all course data" do
57
- setup do
58
- @myuw = MyUW.new
59
- @browser = @myuw.browser
60
- @browser.follow_meta_refresh = false
61
- @curriculum = @myuw.curriculum_enrollment("CSE", "AUT+2009")
62
- @fixture_page = @browser.get(path_to_html("curric_courses"))
63
- end
64
63
 
65
- should "raise InvalidPageError if no rows are found for extracting information" do
66
- fixture_page = @browser.get(path_to_html("curric_no_courses"))
64
+ context "extracting courses from page" do
65
+ setup do
66
+ @page = mock('page')
67
+ @page.stubs(:search).returns([])
68
+
69
+ @term = "AUT+2009"
70
+ end
71
+
72
+ should "run a search on page and run extract_node on each result" do
73
+ extract_sequence = sequence('extract')
74
+
75
+ mocks = []
76
+ 3.times do
77
+ result = mock('result')
78
+ mocks << result
79
+
80
+ RubyUW::CurriculumEnrollment.expects(:extract_course).with(result).returns({}).in_sequence(extract_sequence)
81
+ end
82
+
83
+ @page.expects(:search).returns(mocks)
84
+
85
+ RubyUW::CurriculumEnrollment.extract_courses(@page, @term)
86
+ end
87
+
88
+ should "return a hash when extracting courses" do
89
+ result = RubyUW::CurriculumEnrollment.extract_courses(@page, @term)
90
+ assert result.kind_of?(Hash)
91
+ end
67
92
 
68
- assert_raise MyUW::InvalidPageError do
69
- @curriculum.get_all_courses_data(fixture_page)
93
+ should "turn results from extract_course into a RubyUW::SLN object" do
94
+ sln = "123456"
95
+ data = { :foo => :bar }
96
+ RubyUW::CurriculumEnrollment.expects(:extract_course).returns([sln, data])
97
+ @page.expects(:search).returns([:whatever])
98
+
99
+ result = RubyUW::CurriculumEnrollment.extract_courses(@page, @term)
100
+ assert_equal 1, result.length
101
+ assert result.values[0].is_a?(RubyUW::SLN)
70
102
  end
71
103
  end
72
104
 
73
- should "call get_course_data on each node" do
74
- @curriculum.expects(:get_course_data).at_least(1).returns({
75
- :sln => "123456"
76
- })
77
- assert_nothing_raised { @curriculum.get_all_courses_data(@fixture_page) }
105
+ context "extracting a single course" do
106
+ setup do
107
+ @node = mock('node')
108
+
109
+ RubyUW::CurriculumEnrollment.send(:public, *(RubyUW::CurriculumEnrollment.protected_instance_methods))
110
+ end
111
+
112
+ should "strip out the leading > of quiz sections" do
113
+ RubyUW::Base.expects(:extract).returns({
114
+ :sln => ">foo"
115
+ })
116
+
117
+ results = RubyUW::CurriculumEnrollment.extract_course(@node)
118
+ assert_equal "foo", results[0]
119
+ end
78
120
  end
79
121
 
80
- should "successfully extract and store course data" do
81
- assert_nothing_raised { @curriculum.get_all_courses_data(@fixture_page) }
122
+ context "finding SLNs on the mocked page" do
123
+ setup do
124
+ @page = expect_get("curric_courses")
125
+ RubyUW::Base.stubs(:authenticated?).returns(true)
126
+
127
+ @curric = "foo"
128
+ @term = "bar"
129
+
130
+ @results = RubyUW::CurriculumEnrollment.find(@curric, @term)
131
+ end
82
132
 
83
- # Random test
84
- assert_equal "54", @curriculum.data["11633"][:current_enrollment]
133
+ should "get the correct number of courses" do
134
+ assert @results.is_a?(Hash) # sanity
135
+ assert_equal 226, @results.length
136
+ end
137
+
138
+ should "properly extract courses with all columns filled" do
139
+ course = @results["11635"]
140
+ assert !course.nil?
141
+ assert course.is_a?(RubyUW::SLN) # sanity
142
+
143
+ course_info = {
144
+ :course => "CHEM 110",
145
+ :section => "AB",
146
+ :type => "QZ",
147
+ :title => "INTRO TO GEN CHEM",
148
+ :room_capacity => "50",
149
+ :space_available => "--"
150
+ }
151
+
152
+ course_info.each do |k,v|
153
+ assert_equal v, course.data(k)
154
+ end
155
+ end
85
156
  end
86
157
  end
87
158
  end
@@ -1,181 +1,145 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- class MockedSlnTest < Test::Unit::TestCase
4
- context "fetching SLN information" do
3
+ class SLNTest < Test::Unit::TestCase
4
+ setup do
5
+ RubyUW::SLN.publicize_singleton_methods
6
+ end
7
+
8
+ teardown do
9
+ RubyUW::SLN.protect_singleton_methods
10
+ end
11
+
12
+ context "SLN instance" do
5
13
  setup do
6
- @myuw = MyUW.new
7
- @myuw.stubs(:logged_in?).returns(true)
8
- @browser = @myuw.browser
9
- @browser.follow_meta_refresh = false
10
- @sln = @myuw.sln("123456", "AUT+2009")
11
- end
12
-
13
- should "return a MyUW::SLNInfo object when calling MyUW.sln" do
14
- sln_obj = @myuw.sln("123456", "AUT+2009")
15
- assert sln_obj.kind_of?(MyUW::SLNInfo)
16
- assert_equal sln_obj.sln, "123456"
17
- assert_equal sln_obj.term, "AUT+2009"
18
- end
19
-
20
- should "raise a NotLoggedInError if not logged in and requesting an SLN" do
21
- @myuw.stubs(:logged_in?).returns(false)
14
+ @sln = "foo"
15
+ @term = "bar"
16
+ @data = {
17
+ :one => "1",
18
+ :two => "2"
19
+ }
22
20
 
23
- assert_raise MyUW::NotLoggedInError do
24
- @sln.course
25
- end
21
+ @sln_obj = RubyUW::SLN.new(@sln, @term, @data)
26
22
  end
27
23
 
28
- should "raise a ArgumentError if there is no SLN set" do
29
- @sln.sln = nil
30
- assert @sln.term # Sanity
31
-
32
- assert_raise ArgumentError do
33
- @sln.course
24
+ should "return data values from the field method" do
25
+ @data.each do |k,v|
26
+ assert_equal v, @sln_obj.data(k)
34
27
  end
35
28
  end
36
29
 
37
- should "raise a ArgumentError if there is no term set" do
38
- @sln.term = nil
39
- assert @sln.sln
40
-
41
- assert_raise ArgumentError do
42
- @sln.course
30
+ should "convert to symbols so accessing by string works too" do
31
+ @data.each do |k,v|
32
+ assert_equal v, @sln_obj.data(k.to_s)
43
33
  end
44
34
  end
45
-
46
- should "raise a InvalidSLNError if an invalid SLN is requested" do
47
- fixture_page = @browser.get(path_to_html("sln_does_not_exist"))
48
- @browser.expects(:get).returns(fixture_page)
35
+ end
36
+
37
+ context "finding an SLN" do
38
+ context "with authentication" do
39
+ setup do
40
+ expect_get("bad_request", nil, :stubs)
41
+ RubyUW::SLN.stubs(:extract_data_from_page).returns({})
42
+ end
49
43
 
50
- assert_raise MyUW::InvalidSLNError do
51
- @sln.get_sln_page
44
+ should "raise an error if not authenticated" do
45
+ RubyUW::Base.expects(:authenticated?).returns(false)
46
+ assert_raises(RubyUW::Errors::NotLoggedInError) { RubyUW::SLN.find("foo", "bar") }
52
47
  end
53
- end
54
-
55
- should "raise a RequestSLNTooSoonError if an SLN is requested again too soon" do
56
- fixture_page = @browser.get(path_to_html("bad_request"))
57
- fixture_page.uri = "http://www.washington.edu/students/timeschd/badrequest.html"
58
- @browser.expects(:get).with("https://sdb.admin.washington.edu/timeschd/uwnetid/sln.asp?QTRYR=#{@sln.term}&SLN=#{@sln.sln}").returns(fixture_page)
59
-
60
- assert_raise MyUW::RequestSLNTooSoonError do
61
- @sln.get_sln_page
48
+
49
+ should "not raise an error if authenticated" do
50
+ RubyUW::Base.expects(:authenticated?).returns(true)
51
+ assert_nothing_raised { RubyUW::SLN.find("foo", "bar") }
62
52
  end
63
53
  end
64
54
 
65
- should "raise an InvalidPageError if it can't find the course info" do
66
- fixture_page = @browser.get(path_to_html("sln_no_course_info"))
67
-
68
- e = assert_raise MyUW::InvalidPageError do
69
- @sln.get_course_info(fixture_page)
55
+ context "with checking if an SLN was requested too soon" do
56
+ should "return true if the URI matches the badrequest page" do
57
+ page = mock('page')
58
+ page.stubs(:uri).returns("http://www.washington.edu/students/timeschd/badrequest.html")
59
+
60
+ assert RubyUW::SLN.requested_too_soon?(page)
70
61
  end
71
62
 
72
- assert !e.page.nil?
73
- end
74
-
75
- should "raise an InvalidPageError if it can't find the enrollment info" do
76
- fixture_page = @browser.get(path_to_html("sln_no_enrollment_info"))
77
-
78
- e = assert_raise MyUW::InvalidPageError do
79
- @sln.get_enrollment_info(fixture_page)
63
+ should "convert URI to string before checking" do
64
+ uri = mock('uri')
65
+ uri.expects(:to_s).returns("http://www.washington.edu/students/timeschd/badrequest.html")
66
+
67
+ page = mock('page')
68
+ page.stubs(:uri).returns(uri)
69
+
70
+ assert RubyUW::SLN.requested_too_soon?(page)
80
71
  end
81
72
 
82
- assert !e.page.nil?
73
+ should "return false if the URI is anything else" do
74
+ page = mock('page')
75
+ page.stubs(:uri).returns("foo")
76
+
77
+ assert !RubyUW::SLN.requested_too_soon?(page)
78
+ end
83
79
  end
84
80
 
85
- should "raise an InvalidPageError if it can't find the course notes" do
86
- fixture_page = @browser.get(path_to_html("sln_no_course_notes"))
87
-
88
- e = assert_raise MyUW::InvalidPageError do
89
- @sln.get_course_notes(fixture_page)
81
+ context "with checking if an SLN exists or not" do
82
+ should "return false if the SLN does not exist string is found" do
83
+ mocked_page = RubyUW::Base.connection.get(path_to_html("sln_does_not_exist"))
84
+
85
+ assert !RubyUW::SLN.sln_exists?(mocked_page)
90
86
  end
91
87
 
92
- assert !e.page.nil?
88
+ should "return true if the string is not found" do
89
+ mocked_page = RubyUW::Base.connection.get(path_to_html("sln_status"))
90
+
91
+ assert RubyUW::SLN.sln_exists?(mocked_page)
92
+ end
93
93
  end
94
94
 
95
- should "raise a TimeScheduleClosedError if the time schedule system is closed" do
96
- fixture_page = mock()
97
- fixture_page.stubs(:uri).returns("http://www.washington.edu/students/timeschd/nots.html")
98
- fixture_page.stubs(:body).returns("")
99
- @browser.expects(:get).returns(fixture_page)
95
+ context "with checking whether time schedule is closed" do
96
+ should "return true if the URI matches the closed page" do
97
+ page = mock('page')
98
+ page.stubs(:uri).returns("http://www.washington.edu/students/timeschd/nots.html")
99
+
100
+ assert RubyUW::SLN.time_schedule_closed?(page)
101
+ end
100
102
 
101
- assert_raise MyUW::TimeScheduleClosedError do
102
- @sln.course
103
+ should "return false if the URI is anything else" do
104
+ page = mock('page')
105
+ page.stubs(:uri).returns("foo")
106
+
107
+ assert !RubyUW::SLN.time_schedule_closed?(page)
103
108
  end
104
109
  end
105
110
 
106
- should "not raise a TimeScheduleClosedError if the URI doesn't match" do
107
- fixture_page = @browser.get(path_to_html("sln_status"))
108
- @browser.expects(:get).returns(fixture_page)
109
-
110
- assert_nothing_raised do
111
- @sln.course
111
+ context "raising exceptions on failures" do
112
+ setup do
113
+ RubyUW::Base.stubs(:authenticated?).returns(true)
114
+ RubyUW::SLN.stubs(:requested_too_soon?).returns(false)
115
+ RubyUW::SLN.stubs(:sln_exists?).returns(true)
116
+ RubyUW::SLN.stubs(:time_schedule_closed?).returns(false)
117
+ RubyUW::SLN.stubs(:extract_data_from_page).returns({})
118
+
119
+ page = mock('page')
120
+ RubyUW::Base.connection.stubs(:get).returns(page)
121
+
122
+ @sln = "foo"
123
+ @term = "bar"
112
124
  end
113
- end
114
- end
115
-
116
- context "extract info from nodes" do
117
- setup do
118
- @myuw = MyUW.new
119
- @sln = @myuw.sln("123456", "AUT+2009")
120
-
121
- @keys = [:foo, :bar, :baz]
122
- @values = [
123
- stub(:inner_text => "vfoo"),
124
- stub(:inner_text => "vbar"),
125
- stub(:inner_text => "vbaz")
126
- ]
127
-
128
- @long_values = @values.dup
129
- @long_values.push(stub(:inner_text => "vbad"))
130
125
 
131
- @nil_keys = @keys.dup
132
- @nil_keys.push(nil)
133
- @nil_keys.push(:bang)
134
-
135
- @nil_values = @long_values.dup
136
- @nil_values.push(stub(:inner_text => "vbang"))
137
- end
138
-
139
- should "return false if there are no nodes" do
140
- assert !@sln.extract_info(@keys, [])
141
- end
142
-
143
- should "return true if everything is good" do
144
- assert @sln.extract_info(@keys, @values)
145
- end
146
-
147
- should "only extract as long as there are keys left" do
148
- assert @sln.extract_info(@keys, @long_values)
149
- assert_equal @keys.length, @sln.data.length
126
+ should "raise SLNRequestedTooSoonError is requested too soon" do
127
+ RubyUW::SLN.expects(:requested_too_soon?).returns(true)
128
+ assert_raises(RubyUW::Errors::SLNRequestedTooSoonError) { RubyUW::SLN.find(@sln, @term) }
129
+ end
150
130
 
151
- # It shouldn't get the 4th long value since there are only 3 keys
152
- @sln.data.each { |k,v| assert_not_equal @long_values[@keys.length].inner_text, v }
153
- end
154
-
155
- should "extract values and associate them with proper keys" do
156
- assert @sln.extract_info(@keys, @values)
157
- assert_equal @keys.length, @sln.data.length
131
+ should "raise SLNDoesNotExistError if sln does not exist" do
132
+ RubyUW::SLN.expects(:sln_exists?).returns(false)
133
+ assert_raises(RubyUW::Errors::SLNDoesNotExistError) { RubyUW::SLN.find(@sln, @term) }
134
+ end
158
135
 
159
- @sln.data.each do |k,v|
160
- assert @keys.include?(k)
161
- assert_equal @values[@keys.index(k)].inner_text, v
136
+ should "raise SLNServiceClosedError if the service is closed" do
137
+ RubyUW::SLN.expects(:time_schedule_closed?).returns(true)
138
+ assert_raises(RubyUW::Errors::SLNServiceClosedError) { RubyUW::SLN.find(@sln, @term) }
162
139
  end
163
- end
164
-
165
- should "not store values associated with nil keys" do
166
- assert @sln.extract_info(@nil_keys, @nil_values)
167
- assert_equal (@nil_keys.length - 1), @sln.data.length
168
140
 
169
- @nil_keys.each_index do |i|
170
- key = @nil_keys[i]
171
-
172
- # If the key is nil, it should NOT store the value
173
- # at that index, otherwise it should
174
- if key.nil?
175
- assert !@sln.data.has_value?(@nil_values[i].inner_text)
176
- else
177
- assert_equal @nil_values[i].inner_text, @sln.data[key]
178
- end
141
+ should "not raise any exception if all fail checks are valid" do
142
+ assert_nothing_raised { RubyUW::SLN.find(@sln, @term) }
179
143
  end
180
144
  end
181
145
  end