forked from ashleygwilliams/student-sinatra
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.rb
102 lines (77 loc) · 2.5 KB
/
test.rb
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
require 'fis/test'
require 'sqlite3'
require 'pry-debugger'
require_relative 'lib/concerns/persistable'
require_relative 'lib/concerns/findable'
require_relative 'lib/models/student'
include Fis::Test
assert Student.drop
assert Student.create_table
test 'should create a table' do
assert !Student.table_exists?('students')
assert Student.table_exists?('students')
end
test 'should be able to instantiate a student' do
assert Student.new
end
test 'should be able to save a student with a name' do
s = Student.new
s.name = "Avi Flombaum"
s.save
assert_equal Student.find_by_name("Avi Flombaum").name, "Avi Flombaum"
end
test 'should be able to load all students' do
s = Student.new
s.name = "Avi Flombaum"
s.save
assert Student.all.collect{|s| s.name}.include?("Avi Flombaum")
end
test 'should be able to find a student by id' do
s = Student.new
s.name = "Avi Flombaum"
s.save
assert_equal Student.find(s.id).name, "Avi Flombaum"
end
test 'should be able to update a student' do
s = Student.new
s.name = "Avi Flombaum"
s.save
s.name = "Bob Whitney"
s.save
assert_equal Student.find(s.id).name, "Bob Whitney"
end
test 'should be able to retrieve a student with a where statement' do
s = Student.new
s.name = "Jeff Baird"
s.save
assert_equal Student.where(:name => "Jeff Baird").map { |s| s.name }, [s.name]
end
test 'should be able to retrieve multiple students with the same name' do
s = Student.new
s.name = "Alice Adams"
s.save
s2 = Student.new
s2.name = "Alice Adams"
s2.save
assert_equal Student.where(:name => "Alice Adams").map {|s| s.name}, [s.name,s2.name]
end
test 'should be able to find_by any attribute' do
s = Student.new
s.name = "Avi Flombaum"
s.tagline = "Hello World"
s.bio = "Dean at Flatiron School"
s.save
assert_equal Student.find_by_tagline("Hello World").tagline, "Hello World"
assert_equal Student.find_by_bio("Dean at Flatiron School").bio, "Dean at Flatiron School"
assert_equal Student.find_by_name("Avi Flombaum").name, "Avi Flombaum"
end
test 'should return nil when find_by method fails to match' do
assert_equal Student.find(3409), nil
assert_equal Student.find_by_tagline("Hello Worl"), nil
assert_equal Student.find_by_bio("Dean at Flatiron Scho"), nil
assert_equal Student.find_by_name("Avi Flomb"), nil
end
test 'should return nil when where method fails to match & array otherwise' do
assert_equal Student.where(:name => "Avi Flombaum").class, Array
assert_equal Student.where(:name => "Avi Flombau"), nil
end