Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rspec Unit Tests #8

Merged
merged 37 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
57ef9ef
Add preserve_data.rb file
GeekyHacks Oct 25, 2023
4ee3920
preserve data update
ishimwepati Oct 25, 2023
92daef6
Preserved update
ishimwepati Oct 25, 2023
b17e3ce
class updated
ishimwepati Oct 25, 2023
8db9daf
Preserved file update
ishimwepati Oct 25, 2023
e2b6881
Fix linter errors
GeekyHacks Oct 26, 2023
4f8e8b2
Update preserve_data.rb
ishimwepati Oct 26, 2023
215cc6b
Add save_data and Load_data files
GeekyHacks Oct 27, 2023
43b3f77
Merge branch 'PreserveData' of https://github.com/GeekyHacks/OOPSchoo…
GeekyHacks Oct 27, 2023
4e61f8e
remove preserve_data file
GeekyHacks Oct 27, 2023
7852bcb
Fix linter errors
GeekyHacks Oct 27, 2023
41efc54
Add gem rspec
GeekyHacks Oct 27, 2023
eb8637c
Add the initialize tests for Person, Student, book
GeekyHacks Oct 27, 2023
73abef5
classroom test
ishimwepati Oct 27, 2023
fd93e68
Teacher test added
ishimwepati Oct 27, 2023
a121af9
Renta test added
ishimwepati Oct 27, 2023
2786a23
Update classroom.rb
ishimwepati Oct 27, 2023
b09e4ed
Update classroom_spec.rb
ishimwepati Oct 27, 2023
e365ee6
Update rental_spec.rb
ishimwepati Oct 27, 2023
e1d2833
Edit the book and student test cases
GeekyHacks Oct 27, 2023
53d60ae
Merge branch 'AddUnitTests' of https://github.com/GeekyHacks/OOPSchoo…
GeekyHacks Oct 27, 2023
7434e7d
Update rental_spec.rb
ishimwepati Oct 27, 2023
90248aa
Edit the rental test file
GeekyHacks Oct 27, 2023
f3c0beb
Merge branch 'AddUnitTests' of https://github.com/GeekyHacks/OOPSchoo…
GeekyHacks Oct 27, 2023
cff89e2
Update rental_spec.rb
ishimwepati Oct 27, 2023
f2c2217
Update rental_spec.rb
ishimwepati Oct 27, 2023
2c0f1c6
Fix linter errors
GeekyHacks Oct 27, 2023
401973e
Merge branches 'AddUnitTests' and 'AddUnitTests' of https://github.co…
GeekyHacks Oct 27, 2023
3df14cc
Fix linter errors
GeekyHacks Oct 27, 2023
d201e19
Edit Readme file accordingly
GeekyHacks Oct 27, 2023
ad056d9
Add more test cases to person and classroom
GeekyHacks Oct 28, 2023
6864651
Add the required test cases
GeekyHacks Oct 28, 2023
f4ec053
Test correct_name method
GeekyHacks Oct 28, 2023
7a4f740
Add required changes
GeekyHacks Oct 28, 2023
73d444c
fix linter errors
GeekyHacks Oct 28, 2023
dee6453
fix linter errors, add default name test case
GeekyHacks Oct 28, 2023
db6cae0
fix linter errors
GeekyHacks Oct 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
source 'https://rubygems.org'

gem 'rubocop', '>= 1.0', '< 2.0'

gem 'json'

gem 'rspec'
16 changes: 16 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ GEM
specs:
ast (2.4.2)
base64 (0.1.1)
diff-lcs (1.5.0)
json (2.6.3)
language_server-protocol (3.17.0.3)
parallel (1.23.0)
Expand All @@ -13,6 +14,19 @@ GEM
rainbow (3.1.1)
regexp_parser (2.8.2)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rubocop (1.57.1)
base64 (~> 0.1.1)
json (~> 2.3)
Expand All @@ -34,6 +48,8 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
json
rspec
rubocop (>= 1.0, < 2.0)

BUNDLED WITH
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ In this project I will be making a calculator app using react
- Add new students or teachers
- Add new books
- Save records of who borrowed a given book and when it was borrowed
- Store the data interred by the user when exit
- list the data stored whenever list option is selected
- Rspec test cases for person, student, teacher, book, and rental files

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down
5 changes: 5 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
require_relative 'rental'
require_relative 'creation'
require_relative 'list'
require './save_data'
require './load_data'

class App
include Creator
include Lister
include Save
include Load
attr_accessor :books, :people, :rentals

def initialize
@books = []
@rentals = []
@people = []
load_files
end

def start
Expand Down
1 change: 1 addition & 0 deletions books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"title":"weqwewq","author":"qweqwe"},{"title":"qqqqqw","author":"wwee"}]
2 changes: 2 additions & 0 deletions classroom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ def add_student(student)
@students.push(student)
student.classroom = self
end

attr_accessor :label
attr_reader :students
end
4 changes: 2 additions & 2 deletions creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
require_relative 'student'
require_relative 'teacher'
require_relative 'list'

module Creator
include Lister

def create_person
print 'Would you like to create a student (1) or a teacher (2)? Select a number: '
choice = gets.chomp
Expand All @@ -28,7 +28,7 @@ def create_person
def create_student(name, age)
print 'Parent permission? [Y/N]: '
permission_option = gets.chomp.downcase
student = Student.new(age, name, parent_permission: permission_option == 'y')
student = Student.new(Classroom.new('Unknown'), age, name, parent_permission: permission_option == 'y')
@people << student
puts "Student created successfully. ID is #{student.id}"
end
Expand Down
1 change: 1 addition & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"people":["{\"@id\":835,\"@age\":\"eee\",\"@name\":33,\"@parent_permission\":true,\"@rentals\":[],\"@specialization\":\"IT\"}"]}
43 changes: 43 additions & 0 deletions load_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'json'
require './classroom'

module Load
def load_files
load_books
load_people
load_rentals
end

def load_books
File.exist?('./books.json') ? books_json = JSON.parse(File.read('books.json')) : return
@books = books_json.map do |book|
Book.new(book['title'], book['author'])
end
end

def load_people
File.exist?('./people.json') ? people_json = JSON.parse(File.read('people.json')) : return
@people = people_json.map { |person| identify_person(person) }
end

def load_rentals
File.exist?('./rentals.json') ? rentals_json = JSON.parse(File.read('rentals.json')) : return
@rentals = rentals_json.map do |rental|
person = identify_person(rental['person'])
Rental.new(rental['date'], Book.new(rental['book']['title'], rental['book']['author']), person)
end
end

def identify_person(person)
if person['specialization']
teacher = Teacher.new(person['specialization'], person['age'], person['name'])
teacher.id = person['id']
teacher
else
student = Student.new((person['classroom']), person['age'], person['name'],
parent_permission: person['parent_permission'])
student.id = person['id']
student
end
end
end
3 changes: 3 additions & 0 deletions main_school
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require_relative 'app'
require './save_data'


puts 'Welcome to our school library'

Expand Down Expand Up @@ -26,6 +28,7 @@ def choose_option
end

def exit_program
save_files
puts 'Thank you for using the app.'
exit!
end
Expand Down
1 change: 1 addition & 0 deletions people.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":840,"name":"qwqw","age":22,"parent_permission":true,"classroom":"Unknown"},{"id":268,"name":33,"age":"qqqq","specialization":"it"},{"id":226,"name":"wweee","age":24,"parent_permission":true,"classroom":"#<Classroom:0x000055e07a3623c8>"}]
4 changes: 2 additions & 2 deletions person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require './book'

class Person < Nameable
attr_accessor :name, :age, :rentals
attr_reader :id
attr_accessor :name, :age, :id
attr_reader :rentals, :parent_permission

def initialize(age, name, parent_permission: true)
super()
Expand Down
1 change: 1 addition & 0 deletions rentals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"book":{"title":"qqqqqw","author":"wwee"},"person":{"id":840,"name":"qwqw","age":22,"parent_permission":true,"classroom":"Unknown"},"date":"2023-10-27"}]
64 changes: 64 additions & 0 deletions save_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'json'

module Save
def save_files
save_books
save_people
save_rentals
end

def save_books
books_json = @books.map do |book|
{
title: book.title,
author: book.author
}
end
File.write('./books.json', JSON.generate(books_json))
end

def save_people
people_json = []
@people.map do |person|
person_json = {
id: person.id,
name: person.name,
age: person.age
}
append_data(person, person_json)
people_json.push(person_json)
end
File.write('./people.json', JSON.generate(people_json))
end

def save_rentals
rentals_json = []
@rentals.map do |rental|
rental_json = {
book: {
title: rental.book.title,
author: rental.book.author
},
person: {
id: rental.person.id,
name: rental.person.name,
age: rental.person.age
},
date: rental.date
}
append_data(rental.person, rental_json[:person])
rentals_json.push(rental_json)
end
File.write('./rentals.json', JSON.generate(rentals_json))
end

def append_data(person, person_json)
if person.instance_of?(::Teacher)
person_json[:specialization] = person.specialization
else
person_json[:parent_permission] = person.parent_permission
person_json[:classroom] = person.classroom
end
person_json
end
end
35 changes: 35 additions & 0 deletions spec/book_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require './book'
require './rental'

describe Book do
before(:each) do
@book = Book.new('title', 'author')
@rentals = []
end

describe '#new' do
it 'returns a new book object' do
expect(@book).to be_an_instance_of(Book)
end
end

describe '#add_rental' do
after(:each) do
@rentals = [@rental]
end

it 'returns a new rental object' do
person = double('Person', rentals: [])
@rental = Rental.new('2020-10-10', @book, person)

expect(@rental).to be_an_instance_of(Rental)
end

it 'adds rental to person rentals list when add_rental method is called' do
person = double('Person', rentals: [])
@rental = Rental.new('2020-10-10', @book, person)

expect(person.rentals).to include(@rental)
end
end
end
24 changes: 24 additions & 0 deletions spec/classroom_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require './classroom'
require './student'

describe Classroom do
subject(:classroom) { described_class.new('Math Classroom') }

it { is_expected.to have_attributes(label: 'Math Classroom') }

it 'initially has no students' do
expect(classroom.students).to be_empty
end

it 'can add students' do
student1 = Student.new(classroom, 18, 'Alice')
student2 = Student.new(classroom, 19, 'Bob')

classroom.add_student(student1)
classroom.add_student(student2)

expect(classroom.students).to include(student1, student2)
expect(student1.classroom).to eq(classroom)
expect(student2.classroom).to eq(classroom)
end
end
7 changes: 7 additions & 0 deletions spec/person_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require './person'

describe Person.new 23, 'Abdu' do
it { is_expected.to have_attributes(age: 23) }
it { is_expected.to have_attributes(name: 'Abdu') }
it { is_expected.to have_attributes(parent_permission: true) }
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Good work writing your unit tests, but you forgot to test methods in the class, methods like can_use_services, correct_name, add_rental also test default value for the optional name parameter if it is not provided

17 changes: 17 additions & 0 deletions spec/rental_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require './rental'
require './book'
require './person'

describe Rental do
let(:book) { Book.new('Book Title', 'Author') }
let(:person) { Person.new(25, 'John Doe') }

subject(:rental) { described_class.new('2023-10-26', book, person) }

it { is_expected.to have_attributes(date: '2023-10-26', book: book, person: person) }

it 'associates the rental with the book and person' do
expect(book.rentals).to include(rental)
expect(person.rentals).to include(rental)
end
end
9 changes: 9 additions & 0 deletions spec/student_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require './student'

describe Student.new 'IT', 24, 'Abdu' do
it { is_expected.to be_a(Person) }
it { is_expected.to have_attributes(classroom: 'IT') }
it { is_expected.to have_attributes(age: 24) }
it { is_expected.to have_attributes(name: 'Abdu') }
it { is_expected.to have_attributes(parent_permission: true) }
end
12 changes: 12 additions & 0 deletions spec/teacher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require './teacher'

describe Teacher do
subject(:teacher) { described_class.new('Math', 35, 'John Doe') }

it { is_expected.to be_a(Person) }
it { is_expected.to have_attributes(specialization: 'Math', age: 35, name: 'John Doe') }

it 'can use services' do
expect(teacher.can_use_services?).to be(true)
end
end
6 changes: 1 addition & 5 deletions student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
class Student < Person
attr_reader :classroom

def initialize(age, name, parent_permission: true)
def initialize(classroom, age, name, parent_permission: true)
super(age, name, parent_permission: parent_permission)
@classroom = classroom
end

def play_hooky
'¯\\_(ツ)_/¯'
end

def classroom=(classroom)
@classroom = classroom
classroom.students.push(self) unless classroom.students.include?(self)
Expand Down
2 changes: 2 additions & 0 deletions teacher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require_relative 'person'

class Teacher < Person
attr_reader :specialization

def initialize(specialization, age, name)
super(age, name)
@specialization = specialization
Expand Down
Loading