diff --git a/classes/person.rb b/classes/person.rb index f87cd1e..b6b6fbc 100644 --- a/classes/person.rb +++ b/classes/person.rb @@ -9,12 +9,12 @@ class Person < Nameable attr_accessor :name, :age, :rentals def initialize(age: nil, name: 'Unknown', parent_permission: true) + super() @id = Random.rand(1..1000) @age = age @name = name @parent_permission = parent_permission @rentals = [] - super() end def add_rental(book, date) diff --git a/classes/student.rb b/classes/student.rb index 9b80b3b..50d8ab2 100644 --- a/classes/student.rb +++ b/classes/student.rb @@ -27,7 +27,7 @@ def to_json(*_args) 'age' => @age, 'name' => @name, 'parent_permission' => @parent_permission, - 'classroom' => @classroom + 'classroom' => @classroom.label }.to_json end end diff --git a/data/rentals.json b/data/rentals.json index ebbb66c..98ced38 100644 --- a/data/rentals.json +++ b/data/rentals.json @@ -6,9 +6,7 @@ "object_id": 100, "title": "Ruby 101", "author": "Tshepo", - "rentals": [ - - ] + "rentals": [] }, "person": { "id": 716, @@ -25,9 +23,7 @@ "object_id": 120, "title": "Computer Science", "author": "Dumisani", - "rentals": [ - - ] + "rentals": [] }, "person": { "id": 906, @@ -36,5 +32,22 @@ "parent_permission": true, "specialization": "Botany" } + }, + { + "date": "2023-11-17", + "book": { + "_class": "Book", + "object_id": 120, + "title": "Computer Science", + "author": "Dumisani", + "rentals": [] + }, + "person": { + "id": 716, + "age": "23", + "name": "MyaMya", + "parent_permission": true, + "classroom": "Sport" + } } -] \ No newline at end of file +] diff --git a/spec/book_spec.rb b/spec/book_spec.rb index a01bfd8..c9b2ded 100644 --- a/spec/book_spec.rb +++ b/spec/book_spec.rb @@ -30,7 +30,9 @@ it 'to_json should turn book into a json file' do file_path = './data/book_test.json' json = File.exist?(file_path) ? JSON.parse(File.read(file_path)) : {} - File.write(file_path, JSON.pretty_generate(book)) + json_data = JSON.parse(book.to_json) + expect(book.title).to eql(json_data['title']) + expect(book.author).to eql(json_data['author']) puts "book.title: #{book.title}" puts "json['title']: #{json['title']}" expect(book.title).to eql(json['title']) diff --git a/spec/decorator_spec.rb b/spec/decorator_spec.rb new file mode 100644 index 0000000..ab58405 --- /dev/null +++ b/spec/decorator_spec.rb @@ -0,0 +1,12 @@ +require './classes/decorator' + +RSpec.describe Decorator do + let(:nameable) { instance_double('Nameable', correct_name: 'Original Name') } + subject { described_class.new(nameable) } + + describe '#correct_name' do + it 'delegates to the correct_name method of the underlying nameable object' do + expect(subject.correct_name).to eq('Original Name') + end + end +end diff --git a/spec/nameable_spec.rb b/spec/nameable_spec.rb new file mode 100644 index 0000000..9b831ff --- /dev/null +++ b/spec/nameable_spec.rb @@ -0,0 +1,12 @@ +require 'rspec' +require './classes/nameable' + +describe Nameable do + let(:nameable_instance) { Nameable.new } + + describe '#correct_name' do + it 'raises NotImplementedError' do + expect { nameable_instance.correct_name }.to raise_error(NotImplementedError, /#{__method__}/) + end + end +end diff --git a/spec/rental_spec.rb b/spec/rental_spec.rb index ac99ee6..ad752f4 100644 --- a/spec/rental_spec.rb +++ b/spec/rental_spec.rb @@ -1,17 +1,25 @@ require 'rspec' +require 'json' require './classes/book' +require './classes/teacher' require './classes/rental' describe Book do context 'Given a Book named The Sphere by Michael Crichton' do before :each do - @person = Person.new(age: 24, name: 'Santiago') + @person = Teacher.new(specialization: 'Mathematics', age: 52, name: 'Lilsnow', parent_permission: true) @book = Book.new('Sphere', 'Michael Crichton') end - it '@rental should be instance of Rental' do + it 'to_json should return a valid JSON representation of the rental' do rental = Rental.new('2020-10-23', @book, @person) - expect(rental).to be_an_instance_of(Rental) + json_data = JSON.parse(rental.to_json) + + expect(json_data['date']).to eq('2020-10-23') + expect(json_data['book']['title']).to eq('Sphere') + expect(json_data['book']['author']).to eq('Michael Crichton') + expect(json_data['person']['age']).to eq(52) + expect(json_data['person']['name']).to eq('Lilsnow') end end end diff --git a/spec/student_spec.rb b/spec/student_spec.rb index 10318a5..10fcb7c 100644 --- a/spec/student_spec.rb +++ b/spec/student_spec.rb @@ -1,12 +1,13 @@ require 'rspec' +require 'json' require_relative '../classes/student' require_relative '../classes/classroom' describe Student do - context 'Given a Student named Santiago' do + context 'Given a Student named Lilsnow' do before :each do - @classroom = Classroom.new('Gepgraphy') - @student = Student.new(age: 24, name: 'Santiago', parent_permission: false, classroom: @classroom) + @classroom = Classroom.new('Geography') + @student = Student.new(age: 24, name: 'Lilsnow', parent_permission: false, classroom: @classroom) end it '@student be an instance of Student' do expect(@student).to be_an_instance_of(Student) @@ -24,5 +25,15 @@ it 'plays hooky' do expect(@student.play_hooky).to eq('¯\(ツ)/¯') end + + it 'to_json should return a valid JSON representation of the student' do + json_data = JSON.parse(@student.to_json) + + expect(json_data['id']).to be_instance_of(Integer) + expect(json_data['age']).to eq(24) + expect(json_data['name']).to eq('Lilsnow') + expect(json_data['parent_permission']).to be_falsy + expect(json_data['classroom']).to eq('Geography') # Adjust based on your implementation + end end end diff --git a/spec/teacher_spec.rb b/spec/teacher_spec.rb index 8752614..c399275 100644 --- a/spec/teacher_spec.rb +++ b/spec/teacher_spec.rb @@ -1,3 +1,4 @@ +require 'json' require './classes/teacher' RSpec.describe Teacher do @@ -25,4 +26,16 @@ expect(subject.can_use_services?).to be true end end + + describe '#to_json' do + it 'returns a valid JSON representation of the teacher' do + json_data = JSON.parse(subject.to_json) + + expect(json_data['id']).to be_a(Integer) + expect(json_data['age']).to eq(age) + expect(json_data['name']).to eq(name) + expect(json_data['parent_permission']).to eq(parent_permission) + expect(json_data['specialization']).to eq(specialization) + end + end end