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.
- data/.gitignore +3 -1
- data/{README.markdown → README.md} +6 -11
- data/Rakefile +12 -2
- data/VERSION +1 -1
- data/lib/rubyuw.rb +8 -0
- data/lib/rubyuw/base.rb +122 -0
- data/lib/rubyuw/connection.rb +145 -0
- data/lib/rubyuw/curriculum_enrollment.rb +75 -0
- data/lib/rubyuw/errors.rb +16 -0
- data/lib/rubyuw/sln.rb +108 -0
- data/test/live/curriculum_enrollment_test.rb +47 -0
- data/test/live/sln_test.rb +16 -26
- data/test/live/test_helper.rb +15 -2
- data/test/mocked/base_test.rb +171 -0
- data/test/mocked/connection_test.rb +178 -0
- data/test/mocked/curriculum_enrollment_test.rb +132 -61
- data/test/mocked/sln_test.rb +106 -142
- data/test/mocked/test_helper.rb +57 -1
- metadata +20 -29
- data/lib/myuw.rb +0 -67
- data/lib/myuw/curriculum_enrollment.rb +0 -110
- data/lib/myuw/errors.rb +0 -18
- data/lib/myuw/registration.rb +0 -98
- data/lib/myuw/schedule.rb +0 -74
- data/lib/myuw/session.rb +0 -91
- data/lib/myuw/sln.rb +0 -131
- data/rubyuw.gemspec +0 -102
- data/test/live/registration_test.rb +0 -46
- data/test/live/schedule_test.rb +0 -28
- data/test/live/session_test.rb +0 -34
- data/test/mocked/myuw_test.rb +0 -39
- data/test/mocked/registration_test.rb +0 -105
- data/test/mocked/schedule_test.rb +0 -132
- data/test/mocked/session_test.rb +0 -96
@@ -1,87 +1,158 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
2
|
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
24
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
33
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
42
|
-
|
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
|
-
|
51
|
-
|
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
|
-
|
66
|
-
|
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
|
-
|
69
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
81
|
-
|
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
|
-
|
84
|
-
|
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
|
data/test/mocked/sln_test.rb
CHANGED
@@ -1,181 +1,145 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
2
|
|
3
|
-
class
|
4
|
-
|
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
|
-
@
|
7
|
-
@
|
8
|
-
@
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
24
|
-
@sln.course
|
25
|
-
end
|
21
|
+
@sln_obj = RubyUW::SLN.new(@sln, @term, @data)
|
26
22
|
end
|
27
23
|
|
28
|
-
should "
|
29
|
-
@
|
30
|
-
|
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 "
|
38
|
-
@
|
39
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
51
|
-
|
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
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
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
|
-
|
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
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
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
|
-
|
102
|
-
|
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
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
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
|
-
|
132
|
-
|
133
|
-
|
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
|
-
|
152
|
-
|
153
|
-
|
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
|
-
|
160
|
-
|
161
|
-
|
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
|
-
|
170
|
-
|
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
|