From a4d279048253f991688c9835693b2c55cce18d79 Mon Sep 17 00:00:00 2001 From: HanzAkor <95639001+HanzAkor@users.noreply.github.com> Date: Fri, 15 Jul 2022 10:55:03 +0100 Subject: [PATCH 1/6] Initial commit --- Gemfile.lock | 3 +++ app.rb | 4 ++-- spec/features/test_page_spec.rb | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 7d4eb449..21826b1e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -18,6 +18,8 @@ GEM mini_mime (1.1.1) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) + nokogiri (1.12.3-arm64-darwin) + racc (~> 1.4) nokogiri (1.12.3-x86_64-darwin) racc (~> 1.4) parallel (1.20.1) @@ -83,6 +85,7 @@ GEM nokogiri (~> 1.8) PLATFORMS + arm64-darwin-21 x86_64-darwin-20 DEPENDENCIES diff --git a/app.rb b/app.rb index 2450fb92..36df66f2 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,8 @@ require 'sinatra/base' class Chitter < Sinatra::Base - get '/test' do - 'Test page' + get '/' do + 'Hello World' end run! if app_file == $0 diff --git a/spec/features/test_page_spec.rb b/spec/features/test_page_spec.rb index b65ac196..5a892660 100644 --- a/spec/features/test_page_spec.rb +++ b/spec/features/test_page_spec.rb @@ -1,6 +1,6 @@ feature 'Viewing test page' do scenario 'visiting the test page' do - visit('/test') - expect(page).to have_content "Test page" + visit('/') + expect(page).to have_content "Hello World" end end From 3a0209062592532e5511ee3048164cd0a92fb9fb Mon Sep 17 00:00:00 2001 From: HanzAkor <95639001+HanzAkor@users.noreply.github.com> Date: Fri, 15 Jul 2022 11:29:34 +0100 Subject: [PATCH 2/6] Implemented MVC pattern for Peep --- README.md | 47 +++++++++++++++++------------ app.rb | 15 +++++++-- lib/peep.rb | 5 +++ spec/features/test_page_spec.rb | 12 ++++---- spec/features/viewing_peeps_spec.rb | 9 ++++++ spec/peep_spec.rb | 11 +++++++ views/index.erb | 5 +++ 7 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 lib/peep.rb create mode 100644 spec/features/viewing_peeps_spec.rb create mode 100644 spec/peep_spec.rb create mode 100644 views/index.erb diff --git a/README.md b/README.md index f9638b66..b502331c 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ ## Chitter Challenge -* Challenge time: until the end of the day -* Feel free to use google, your notes, books etc but please work on your own -* Please raise a pull request when you start this challenge, and keep pushing updates as and when you make commits throughout the day -* There is _no expectation_ to finish all or any of the user stories, please use this time to reflect on where you feel you are with the skill and what may support your learning. -* If you get blocked, please reflect on what blocked you and any strategies you adopted that helped you make progress. +- Challenge time: until the end of the day +- Feel free to use google, your notes, books etc but please work on your own +- Please raise a pull request when you start this challenge, and keep pushing updates as and when you make commits throughout the day +- There is _no expectation_ to finish all or any of the user stories, please use this time to reflect on where you feel you are with the skill and what may support your learning. +- If you get blocked, please reflect on what blocked you and any strategies you adopted that helped you make progress. We are going to write a small Twitter clone that will allow the users to post messages to a public stream. @@ -12,23 +12,24 @@ We are going to write a small Twitter clone that will allow the users to post me To setup the database: -* Connect to psql -* Create the database using the psql command `CREATE DATABASE chitter;` -* Connect to the database using the psql command `\c chitter`; -* Run the query we have saved in the file 01_create_chitter_table.sql -* Populate your table with a row by running `INSERT INTO peeps (message) values ('This is a peep!');` +- Connect to psql +- Create the database using the psql command `CREATE DATABASE chitter;` +- Connect to the database using the psql command `\c chitter`; +- Run the query we have saved in the file 01_create_chitter_table.sql +- Populate your table with a row by running `INSERT INTO peeps (message) values ('This is a peep!');` -To check you have everything set up ok, please take a look at the peeps table inside the chitter database. You should see one row in there. +To check you have everything set up ok, please take a look at the peeps table inside the chitter database. You should see one row in there. To setup the test database: -* Connect to psql -* Create the database using the psql -command `CREATE DATABASE chitter_test;`; -* Connect to the database using the psql command `\c chitter_test` -* Run the query we have saved in the file 01_create_chitter_table.sql -* `bundle install` -* `rspec` +- Connect to psql +- Create the database using the psql + command `CREATE DATABASE chitter_test;`; +- Connect to the database using the psql command `\c chitter_test` +- Run the query we have saved in the file 01_create_chitter_table.sql + +- `bundle install` +- `rspec` You should see 1 passing test. @@ -43,15 +44,22 @@ in a browser ``` As a Maker -So that I can let people know what I am doing +So that I can let people know what I am doing I want to post a message (peep) to chitter ``` +| Component | Responsibility | Refactor | +| ---------- | ------------------------------------------- | --------------------------------- | +| Model | Encapsulate logic with relevant data | Encapsulate peep data in a class | +| View | Display the result to a user | Show the peep data in a list | +| Controller | Get data from the model and put in the view | Render peep data into to the view | + ``` As a Maker So that I can see when people are doing things I want to see the date the message was posted ``` + (Hint the database table will need to change to store the date too) ``` @@ -59,6 +67,7 @@ As a Maker So that I can easily see the latest peeps I want to see a list of peeps in reverse chronological order ``` + ``` As a Maker So that I can find relevant peeps diff --git a/app.rb b/app.rb index 36df66f2..3b9006a8 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,19 @@ require 'sinatra/base' +# require 'sinatra/reloader' +require './lib/peep' class Chitter < Sinatra::Base - get '/' do - 'Hello World' + # configure :development do + # register Sinatra::Reloader + # end + + # get '/' do + # 'Hello World' + # end + + get '/peeps' do + @peeps = Peep.all + erb :index end run! if app_file == $0 diff --git a/lib/peep.rb b/lib/peep.rb new file mode 100644 index 00000000..90fda99a --- /dev/null +++ b/lib/peep.rb @@ -0,0 +1,5 @@ +class Peep + def self.all + ["This is a peep!"] + end +end diff --git a/spec/features/test_page_spec.rb b/spec/features/test_page_spec.rb index 5a892660..a90d7a81 100644 --- a/spec/features/test_page_spec.rb +++ b/spec/features/test_page_spec.rb @@ -1,6 +1,6 @@ -feature 'Viewing test page' do - scenario 'visiting the test page' do - visit('/') - expect(page).to have_content "Hello World" - end -end +# feature 'Viewing test page' do +# scenario 'visiting the test page' do +# visit('/') +# expect(page).to have_content "Hello World" +# end +# end diff --git a/spec/features/viewing_peeps_spec.rb b/spec/features/viewing_peeps_spec.rb new file mode 100644 index 00000000..0c324b11 --- /dev/null +++ b/spec/features/viewing_peeps_spec.rb @@ -0,0 +1,9 @@ +# in spec/features/viewing_peeps_spec.rb + +feature 'Viewing peeps' do + scenario 'A user can see peeps' do + visit('/peeps') + + expect(page).to have_content "This is a peep!" + end +end diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb new file mode 100644 index 00000000..f444c055 --- /dev/null +++ b/spec/peep_spec.rb @@ -0,0 +1,11 @@ +require 'peep' + +describe Peep do + describe '.all' do + it 'returns all peeps' do + bookmarks = Peep.all + + expect(bookmarks).to include("This is a peep!") + end + end +end \ No newline at end of file diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..3ff0fc80 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,5 @@ + \ No newline at end of file From 2d4b096323e5d3c2e798aaaf4d0791589c52abe3 Mon Sep 17 00:00:00 2001 From: HanzAkor <95639001+HanzAkor@users.noreply.github.com> Date: Fri, 15 Jul 2022 11:39:02 +0100 Subject: [PATCH 3/6] Peep --- README.md | 11 ++++++----- spec/peep_spec.rb | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b502331c..279ce1f3 100644 --- a/README.md +++ b/README.md @@ -42,11 +42,6 @@ I want to see all the messages (peeps) in a browser ``` -``` -As a Maker -So that I can let people know what I am doing -I want to post a message (peep) to chitter -``` | Component | Responsibility | Refactor | | ---------- | ------------------------------------------- | --------------------------------- | @@ -54,6 +49,12 @@ I want to post a message (peep) to chitter | View | Display the result to a user | Show the peep data in a list | | Controller | Get data from the model and put in the view | Render peep data into to the view | +``` +As a Maker +So that I can let people know what I am doing +I want to post a message (peep) to chitter +``` + ``` As a Maker So that I can see when people are doing things diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb index f444c055..f7b99aa0 100644 --- a/spec/peep_spec.rb +++ b/spec/peep_spec.rb @@ -3,9 +3,9 @@ describe Peep do describe '.all' do it 'returns all peeps' do - bookmarks = Peep.all + peep = Peep.all - expect(bookmarks).to include("This is a peep!") + expect(peep).to include("This is a peep!") end end end \ No newline at end of file From cf08ac36ff2c6182c9707b9616ed340726e0764f Mon Sep 17 00:00:00 2001 From: HanzAkor <95639001+HanzAkor@users.noreply.github.com> Date: Fri, 15 Jul 2022 11:43:07 +0100 Subject: [PATCH 4/6] PG connection --- lib/peep.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/peep.rb b/lib/peep.rb index 90fda99a..e7ebaf37 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -1,5 +1,7 @@ class Peep def self.all - ["This is a peep!"] + connection = PG.connect(dbname: 'chitter') + result = connection.exec("SELECT * FROM peeps;") + result.map { |peep| peep['message'] } end end From 00af64c958e49d72dfe9829563e1a2f18d3bda48 Mon Sep 17 00:00:00 2001 From: HanzAkor <95639001+HanzAkor@users.noreply.github.com> Date: Fri, 15 Jul 2022 13:10:19 +0100 Subject: [PATCH 5/6] Set up test database --- README.md | 32 +++++++++++++++++++++++++++- app.rb | 14 ++++++++++++ lib/peep.rb | 7 +++++- spec/features/creating_peeps_spec.rb | 11 ++++++++++ spec/features/viewing_peeps_spec.rb | 6 ++++++ spec/peep_spec.rb | 27 +++++++++++++++++------ spec/setup_test_database.rb | 2 ++ spec/spec_helper.rb | 3 ++- views/new.erb | 4 ++++ 9 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 spec/features/creating_peeps_spec.rb create mode 100644 views/new.erb diff --git a/README.md b/README.md index 279ce1f3..dd9cd523 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,6 @@ As a Maker So that I can see what people are doing I want to see all the messages (peeps) in a browser -``` | Component | Responsibility | Refactor | @@ -48,11 +47,20 @@ in a browser | Model | Encapsulate logic with relevant data | Encapsulate peep data in a class | | View | Display the result to a user | Show the peep data in a list | | Controller | Get data from the model and put in the view | Render peep data into to the view | +``` ``` As a Maker So that I can let people know what I am doing I want to post a message (peep) to chitter + +- Adding a new message - /peeps/new +- Typing a message inot a form on that page +- Submitting that form +- Seeing the message just submitted + + + ``` ``` @@ -74,3 +82,25 @@ As a Maker So that I can find relevant peeps I want to filter on a specific keyword ``` + + + + +# Hannah's Chitter Challenge + +Brief description of what the project is + +## Getting started + +`git clone path-to-your-repo` +`command_to_install_dependencies` (e.g. `bundle`) + +## Usage + +`command_to_start` (e.g. `rackup` or `rails s`) +Navigate to `http://localhost:4567/` + + +## Running tests + +`test_command` (e.g. `rspec`) \ No newline at end of file diff --git a/app.rb b/app.rb index 3b9006a8..ce88aff3 100644 --- a/app.rb +++ b/app.rb @@ -12,9 +12,23 @@ class Chitter < Sinatra::Base # end get '/peeps' do + # p ENV @peeps = Peep.all erb :index end + get '/peeps/new' do + erb :new + end + + post '/peeps' do + # p params + # p "Form data submitted to the /peeps route!" + message = params['message'] + connection = PG.connect(dbname: 'chitter_test') + connection.exec("INSERT INTO peeps (message) VALUES('#{message}')") + redirect '/peeps' + end + run! if app_file == $0 end diff --git a/lib/peep.rb b/lib/peep.rb index e7ebaf37..c6d81c2d 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -1,6 +1,11 @@ class Peep def self.all - connection = PG.connect(dbname: 'chitter') + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + result = connection.exec("SELECT * FROM peeps;") result.map { |peep| peep['message'] } end diff --git a/spec/features/creating_peeps_spec.rb b/spec/features/creating_peeps_spec.rb new file mode 100644 index 00000000..e851d31b --- /dev/null +++ b/spec/features/creating_peeps_spec.rb @@ -0,0 +1,11 @@ +# in spec/features/creating_peeps_spec.rb + +feature 'Adding a new peep' do + scenario 'A user can add a peep to Chitter' do + visit('/peeps/new') + fill_in('message', with: 'An example of a peep') + click_button('Submit') + + expect(page).to have_content 'An example of a peep' + end +end diff --git a/spec/features/viewing_peeps_spec.rb b/spec/features/viewing_peeps_spec.rb index 0c324b11..fd15cab7 100644 --- a/spec/features/viewing_peeps_spec.rb +++ b/spec/features/viewing_peeps_spec.rb @@ -2,8 +2,14 @@ feature 'Viewing peeps' do scenario 'A user can see peeps' do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps VALUES(1, 'This is a peep!');") + visit('/peeps') expect(page).to have_content "This is a peep!" end end + diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb index f7b99aa0..a6bd37c0 100644 --- a/spec/peep_spec.rb +++ b/spec/peep_spec.rb @@ -1,11 +1,24 @@ require 'peep' -describe Peep do - describe '.all' do - it 'returns all peeps' do - peep = Peep.all +# describe Peep do +# describe '.all' do +# it 'returns all peeps' do +# peep = Peep.all - expect(peep).to include("This is a peep!") - end +# expect(peep).to include("This is a peep!") +# end +# end +# end + +describe '.all' do + it 'returns a list of peeps' do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") + + peeps = Peep.all + + expect(peeps).to include('This is a peep!') end -end \ No newline at end of file +end diff --git a/spec/setup_test_database.rb b/spec/setup_test_database.rb index af832f7d..55fbee96 100644 --- a/spec/setup_test_database.rb +++ b/spec/setup_test_database.rb @@ -1,7 +1,9 @@ require 'pg' def setup_test_database + p "Setting up test database..." connection = PG.connect(dbname: 'chitter_test') + # Clear the peeps table connection.exec("TRUNCATE peeps;") end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74046cad..ccd83686 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -29,9 +29,10 @@ ]) SimpleCov.start -ENV['RACK_ENV'] = 'test' +# ENV['RACK_ENV'] = 'test' ENV['ENVIRONMENT'] = 'test' + # Bring in the contents of the `app.rb` file. The below is equivalent to: require_relative '../app.rb' require File.join(File.dirname(__FILE__), '..', 'app.rb') diff --git a/views/new.erb b/views/new.erb new file mode 100644 index 00000000..4c3a1fcb --- /dev/null +++ b/views/new.erb @@ -0,0 +1,4 @@ +
+ + +
\ No newline at end of file From 3a9b6a65c3dd4ff1615041478a1463590c690584 Mon Sep 17 00:00:00 2001 From: HanzAkor <95639001+HanzAkor@users.noreply.github.com> Date: Sat, 16 Jul 2022 01:23:37 +0100 Subject: [PATCH 6/6] Secured app --- README.md | 62 ++++++++++++++++++---- app.rb | 9 ++-- db/migrations/02_add_username_to_peeps.sql | 1 + lib/peep.rb | 32 ++++++++++- spec/database_helpers.rb | 8 +++ spec/features/creating_peeps_spec.rb | 3 +- spec/features/viewing_peeps_spec.rb | 19 ++++--- spec/peep_spec.rb | 24 ++++++++- views/index.erb | 4 +- views/new.erb | 3 +- 10 files changed, 136 insertions(+), 29 deletions(-) create mode 100644 db/migrations/02_add_username_to_peeps.sql create mode 100644 spec/database_helpers.rb diff --git a/README.md b/README.md index dd9cd523..8c3b7a1c 100644 --- a/README.md +++ b/README.md @@ -55,12 +55,10 @@ So that I can let people know what I am doing I want to post a message (peep) to chitter - Adding a new message - /peeps/new -- Typing a message inot a form on that page +- Typing a message into a form on that page - Submitting that form - Seeing the message just submitted - - ``` ``` @@ -83,9 +81,6 @@ So that I can find relevant peeps I want to filter on a specific keyword ``` - - - # Hannah's Chitter Challenge Brief description of what the project is @@ -93,14 +88,59 @@ Brief description of what the project is ## Getting started `git clone path-to-your-repo` -`command_to_install_dependencies` (e.g. `bundle`) -## Usage +- list gems in the Gemfile: + source 'https://rubygems.org' + +ruby '3.0.2' + +gem 'pg' +gem 'sinatra' + +group :test do +gem 'capybara' +gem 'rspec' +gem 'simplecov', require: false +gem 'simplecov-console', require: false +end + +group :development, :test do +gem 'rubocop', '1.20' +end + +- bundle install +- rspec --init +- setup basic Sinatra app in app.rb (Root directory) -`command_to_start` (e.g. `rackup` or `rails s`) -Navigate to `http://localhost:4567/` +# in app.rb + +require 'sinatra/base' +require 'sinatra/reloader' + +class Chitter < Sinatra::Base +configure :development do +register Sinatra::Reloader +end + +get '/' do +'Hello World' +end + +run! if app_file == $0 +end + +- configure 'rackup' command to run the app in app.rb via config.ru file + +# in config.ru + +require_relative "./app" + +run Chitter + +## Usage +- ruby app.rb => Navigate to `http://localhost:4567/` ## Running tests -`test_command` (e.g. `rspec`) \ No newline at end of file +- rspec diff --git a/app.rb b/app.rb index ce88aff3..349d792a 100644 --- a/app.rb +++ b/app.rb @@ -13,7 +13,7 @@ class Chitter < Sinatra::Base get '/peeps' do # p ENV - @peeps = Peep.all + p @peeps = Peep.all erb :index end @@ -24,9 +24,10 @@ class Chitter < Sinatra::Base post '/peeps' do # p params # p "Form data submitted to the /peeps route!" - message = params['message'] - connection = PG.connect(dbname: 'chitter_test') - connection.exec("INSERT INTO peeps (message) VALUES('#{message}')") + # message = params['message'] + # connection = PG.connect(dbname: 'chitter_test') + # connection.exec("INSERT INTO peeps (message) VALUES('#{message}')") + Peep.create(message: params[:message], username: params[:username]) redirect '/peeps' end diff --git a/db/migrations/02_add_username_to_peeps.sql b/db/migrations/02_add_username_to_peeps.sql new file mode 100644 index 00000000..5e2cce0f --- /dev/null +++ b/db/migrations/02_add_username_to_peeps.sql @@ -0,0 +1 @@ +ALTER TABLE peeps ADD COLUMN username VARCHAR(60); \ No newline at end of file diff --git a/lib/peep.rb b/lib/peep.rb index c6d81c2d..bc16b601 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -1,4 +1,14 @@ +require 'pg' + class Peep + attr_reader :id, :username, :message + + def initialize(id:, username:, message:) + @id = id + @username = username + @message = message + end + def self.all if ENV['ENVIRONMENT'] == 'test' connection = PG.connect(dbname: 'chitter_test') @@ -7,6 +17,26 @@ def self.all end result = connection.exec("SELECT * FROM peeps;") - result.map { |peep| peep['message'] } + result.map do + Peep.new(id: result[0]['id'], username: result[0]['username'], message: result[0]['message']) + end + end + + def self.create(username:, message:) + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + + # I've broken it on to two lines to make it a bit more readable + result = connection.exec_params( + # The first argument is our SQL query template + # The second argument is the 'params' referred to in exec_params + # $1 refers to the first item in the params array + # $2 refers to the second item in the params array + "INSERT INTO peeps (username, message) VALUES($1, $2) RETURNING id, username, message;", [username, message] + ) + Peep.new(id: result[0]['id'], username: result[0]['username'], message: result[0]['message']) end end diff --git a/spec/database_helpers.rb b/spec/database_helpers.rb new file mode 100644 index 00000000..a14472a3 --- /dev/null +++ b/spec/database_helpers.rb @@ -0,0 +1,8 @@ +# in spec/database_helpers.rb +require 'pg' + +def persisted_data(id:) + connection = PG.connect(dbname: 'chitter_test') + result = connection.query("SELECT * FROM peeps WHERE id = #{id};") + result.first +end diff --git a/spec/features/creating_peeps_spec.rb b/spec/features/creating_peeps_spec.rb index e851d31b..a62b7cdd 100644 --- a/spec/features/creating_peeps_spec.rb +++ b/spec/features/creating_peeps_spec.rb @@ -3,9 +3,10 @@ feature 'Adding a new peep' do scenario 'A user can add a peep to Chitter' do visit('/peeps/new') + fill_in('username', with: 'Username') fill_in('message', with: 'An example of a peep') click_button('Submit') - expect(page).to have_content 'An example of a peep' + expect(page).to have_content 'Username, An example of a peep' end end diff --git a/spec/features/viewing_peeps_spec.rb b/spec/features/viewing_peeps_spec.rb index fd15cab7..7bef4610 100644 --- a/spec/features/viewing_peeps_spec.rb +++ b/spec/features/viewing_peeps_spec.rb @@ -1,15 +1,18 @@ -# in spec/features/viewing_peeps_spec.rb - feature 'Viewing peeps' do scenario 'A user can see peeps' do - connection = PG.connect(dbname: 'chitter_test') + # connection = PG.connect(dbname: 'chitter_test') + + # # Add the test data + # connection.exec("INSERT INTO peeps VALUES(1, 'This is a peep!');") + + # Peep.create(username: 'User1', message: 'This is a peep!') + # Peep.create(username: 'User2', message: 'Here is another peep!') + Peep.create(username: 'User3', message: 'One more peep!') - # Add the test data - connection.exec("INSERT INTO peeps VALUES(1, 'This is a peep!');") - visit('/peeps') - expect(page).to have_content "This is a peep!" + # expect(page).to have_content("User1, This is a peep!") + # expect(page).to have_content("User2, Here is another peep!") + expect(page).to have_content("User3, One more peep!") end end - diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb index a6bd37c0..4a0a8eb1 100644 --- a/spec/peep_spec.rb +++ b/spec/peep_spec.rb @@ -1,4 +1,5 @@ require 'peep' +require 'database_helpers' # describe Peep do # describe '.all' do @@ -15,10 +16,29 @@ connection = PG.connect(dbname: 'chitter_test') # Add the test data - connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") + peep = Peep.create(username: "User1", message: "This is a peep!") + Peep.create(username: "User2", message: "Here is another peep!") + Peep.create(username: "User3", message: "One more peep!") peeps = Peep.all - expect(peeps).to include('This is a peep!') + expect(peeps.length).to eq 3 + expect(peeps.first).to be_a Peep + expect(peeps.first.id).to eq peep.id + expect(peeps.first.username).to eq 'User1' + expect(peeps.first.message).to eq 'This is a peep!' + end +end + +describe '.create' do + it 'creates a new peep message' do + peep = Peep.create(username: 'Username', message: 'An example of a peep') + + persisted_data = persisted_data(id: peep.id) + + expect(peep).to be_a Peep + expect(peep.id).to eq persisted_data['id'] + expect(peep.username).to eq 'Username' + expect(peep.message).to eq 'An example of a peep' end end diff --git a/views/index.erb b/views/index.erb index 3ff0fc80..638a2c57 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,5 +1,7 @@ \ No newline at end of file diff --git a/views/new.erb b/views/new.erb index 4c3a1fcb..47242837 100644 --- a/views/new.erb +++ b/views/new.erb @@ -1,4 +1,5 @@
- + +
\ No newline at end of file