From b47be5b50ba637b838ff6e36147e7b6cc7956847 Mon Sep 17 00:00:00 2001 From: stephaniesomade Date: Sat, 2 Apr 2022 03:04:09 +0100 Subject: [PATCH 1/7] Setting up the databases --- Gemfile.lock | 3 +++ app.rb | 7 +++++++ spec/features/view_messages_spec.rb | 0 views/index.erb | 0 4 files changed, 10 insertions(+) create mode 100644 spec/features/view_messages_spec.rb create mode 100644 views/index.erb 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..ffdfe106 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,16 @@ require 'sinatra/base' class Chitter < Sinatra::Base + configure :development do + register Sinatra::Reloader + end + get '/test' do 'Test page' end + get '/' do + end + run! if app_file == $0 end diff --git a/spec/features/view_messages_spec.rb b/spec/features/view_messages_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..e69de29b From fed847bb73c4fd91f78f7357c893e6d48540fbcc Mon Sep 17 00:00:00 2001 From: stephaniesomade Date: Sat, 2 Apr 2022 04:32:34 +0100 Subject: [PATCH 2/7] Added all method to Messages Class --- Gemfile | 5 ++ app.rb | 15 +++-- lib/messages.rb | 12 ++++ spec/features/view_messages_spec.rb | 7 +++ spec/messages_spec.rb | 89 +++++++++++++++++++++++++++++ views/index.erb | 4 ++ 6 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 lib/messages.rb create mode 100644 spec/messages_spec.rb diff --git a/Gemfile b/Gemfile index 99d8e519..df28a014 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,11 @@ ruby '3.0.2' gem 'pg' gem 'sinatra' +gem 'thin' +gem 'puma' +gem 'reel' +gem 'http' +gem 'webrick' group :test do gem 'capybara' diff --git a/app.rb b/app.rb index ffdfe106..1d36e3dd 100644 --- a/app.rb +++ b/app.rb @@ -1,16 +1,23 @@ require 'sinatra/base' +require './lib/messages' class Chitter < Sinatra::Base - configure :development do - register Sinatra::Reloader - end + enable :sessions + configure do + enable :reloader + end get '/test' do 'Test page' end - get '/' do + get '/chitter' do + erb :index end + get '/peeps' do + 'No new messages' + end + run! if app_file == $0 end diff --git a/lib/messages.rb b/lib/messages.rb new file mode 100644 index 00000000..516d260a --- /dev/null +++ b/lib/messages.rb @@ -0,0 +1,12 @@ +class Messages + + def self.all + 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 { |message| message['message']} + end +end diff --git a/spec/features/view_messages_spec.rb b/spec/features/view_messages_spec.rb index e69de29b..180d8592 100644 --- a/spec/features/view_messages_spec.rb +++ b/spec/features/view_messages_spec.rb @@ -0,0 +1,7 @@ +feature 'Viewing messages' do + scenario 'view all messages in the browser' do + visit('/chitter') + click_button 'View all Messages' + expect(current_path).to eq '/peeps' + end +end diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb new file mode 100644 index 00000000..3013a0c3 --- /dev/null +++ b/spec/messages_spec.rb @@ -0,0 +1,89 @@ +require 'messages' +require 'pg' + +RSpec.describe Messages do + describe '.all' do + it 'returns a list of messages' do + connection = PG.connect(dbname: 'chitter_test') + + connection.exec("INSERT INTO peeps (message) VALUES ('Hello');") + connection.exec("INSERT INTO peeps (message) VALUES('Hi!');") + connection.exec("INSERT INTO peeps (message) VALUES('Hiya.');") + messages = Messages.all + + expect(messages).to include "Hello" + expect(messages).to include "Hi!" + expect(messages).to include "Hiya." + expect(messages.length).to eq 3 + end + end +end + + + +# RSpec.describe Bookmark do +# describe '.all' do +# it 'returns a list of peeps' do +# connection = PG.connect(dbname: 'bookmark_manager_test') + +# bookmark = Bookmark.create(message: "http://www.makersacademy.com", title: "Makers Academy") +# Bookmark.create(message: "http://www.destroyallsoftware.com", title: "Destroy All Software") +# Bookmark.create(message: "http://www.google.com", title: "Google") + +# peeps = Bookmark.all + +# expect(peeps.length).to eq 3 +# expect(peeps.first).to be_a Bookmark +# expect(peeps.first.id).to eq bookmark.id +# expect(peeps.first.title).to eq 'Makers Academy' +# expect(peeps.first.message).to eq 'http://www.makersacademy.com' +# end +# end + +# describe '.create' do +# it 'creates a new bookmark' do +# bookmark = Bookmark.create(message: 'http://www.example.org', title: 'Test Bookmark') +# persisted_data = persisted_data(id: bookmark.id) + +# expect(bookmark).to be_a Bookmark +# expect(bookmark.id).to eq persisted_data['id'] +# expect(bookmark.title).to eq 'Test Bookmark' +# expect(bookmark.message).to eq 'http://www.example.org' +# end +# end + +# describe '.delete' do +# it 'deletes the given bookmark' do +# bookmark = Bookmark.create(title: 'Makers Academy', message: 'http://www.makersacademy.com') + +# Bookmark.delete(id: bookmark.id) + +# expect(Bookmark.all.length). to eq 0 +# end +# end + +# describe '.update' do +# it 'updates the bookmark with the given data' do +# bookmark = Bookmark.create(title: 'Makers Academy', message: 'http://www.makersacademy.com') +# updated_bookmark = Bookmark.update(id: bookmark.id, message: 'http://www.snakersacademy.com', title: 'Snakers Academy') + +# expect(updated_bookmark).to be_a Bookmark +# expect(updated_bookmark.id).to eq bookmark.id +# expect(updated_bookmark.title).to eq 'Snakers Academy' +# expect(updated_bookmark.message).to eq 'http://www.snakersacademy.com' +# end +# end + +# describe '.find' do +# it 'returns the requested bookmark object' do +# bookmark = Bookmark.create(title: 'Makers Academy', message: 'http://www.makersacademy.com') + +# result = Bookmark.find(id: bookmark.id) + +# expect(result).to be_a Bookmark +# expect(result.id).to eq bookmark.id +# expect(result.title).to eq 'Makers Academy' +# expect(result.message).to eq 'http://www.makersacademy.com' +# end +# end +# end diff --git a/views/index.erb b/views/index.erb index e69de29b..44f941b1 100644 --- a/views/index.erb +++ b/views/index.erb @@ -0,0 +1,4 @@ +

Welcome to Chitter:

+
+ +
From 4142b0975ae937970c215317f2ac2931560ad8a6 Mon Sep 17 00:00:00 2001 From: stephaniesomade Date: Sat, 2 Apr 2022 17:05:45 +0100 Subject: [PATCH 3/7] Bookmark class in MVC --- app.rb | 3 +- lib/messages.rb | 10 ++-- spec/features/view_messages_spec.rb | 18 +++++- spec/messages_spec.rb | 90 ++++------------------------- views/allpeeps.erb | 6 ++ 5 files changed, 40 insertions(+), 87 deletions(-) create mode 100644 views/allpeeps.erb diff --git a/app.rb b/app.rb index 1d36e3dd..8d78e73c 100644 --- a/app.rb +++ b/app.rb @@ -16,7 +16,8 @@ class Chitter < Sinatra::Base end get '/peeps' do - 'No new messages' + @messages = Messages.all + erb :allpeeps end run! if app_file == $0 diff --git a/lib/messages.rb b/lib/messages.rb index 516d260a..35f610cf 100644 --- a/lib/messages.rb +++ b/lib/messages.rb @@ -1,12 +1,14 @@ +require 'pg' + class Messages def self.all if ENV['ENVIRONMENT'] == 'test' - connection = PG.connect(dbname: 'chitter_test') + connection = PG.connect(dbname: 'chitter_test') else connection = PG.connect(dbname: 'chitter') - end - result = connection.exec("SELECT * FROM peeps;") - result.map { |message| message['message']} + end + result = connection.exec('SELECT * FROM peeps;') + result.map { |message| message['message'] } end end diff --git a/spec/features/view_messages_spec.rb b/spec/features/view_messages_spec.rb index 180d8592..9999300c 100644 --- a/spec/features/view_messages_spec.rb +++ b/spec/features/view_messages_spec.rb @@ -1,7 +1,19 @@ +require 'pg' + feature 'Viewing messages' do - scenario 'view all messages in the browser' do - visit('/chitter') - click_button 'View all Messages' + scenario 'shows all messages' do + + connection = PG.connect(dbname: 'chitter_test') + + connection.exec("INSERT INTO peeps (message) VALUES ('Hello');") + connection.exec("INSERT INTO peeps (message) VALUES('Hi!');") + connection.exec("INSERT INTO peeps (message) VALUES('Hiya.');") + + visit ('/peeps') + expect(current_path).to eq '/peeps' + expect(page).to have_content 'Hello' + expect(page).to have_content 'Hi!' + expect(page).to have_content 'Hiya.' end end diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb index 3013a0c3..9c7941cf 100644 --- a/spec/messages_spec.rb +++ b/spec/messages_spec.rb @@ -1,89 +1,21 @@ require 'messages' require 'pg' -RSpec.describe Messages do +describe Messages do describe '.all' do it 'returns a list of messages' do - connection = PG.connect(dbname: 'chitter_test') + connection = PG.connect(dbname: 'chitter_test') - connection.exec("INSERT INTO peeps (message) VALUES ('Hello');") - connection.exec("INSERT INTO peeps (message) VALUES('Hi!');") - connection.exec("INSERT INTO peeps (message) VALUES('Hiya.');") - messages = Messages.all + connection.exec("INSERT INTO peeps (message) VALUES ('Hello');") + connection.exec("INSERT INTO peeps (message) VALUES('Hi!');") + connection.exec("INSERT INTO peeps (message) VALUES('Hiya.');") + + messages = Messages.all - expect(messages).to include "Hello" - expect(messages).to include "Hi!" - expect(messages).to include "Hiya." - expect(messages.length).to eq 3 + expect(messages.length).to eq 3 + expect(messages).to include "Hello" + expect(messages).to include "Hi!" + expect(messages).to include "Hiya." end end end - - - -# RSpec.describe Bookmark do -# describe '.all' do -# it 'returns a list of peeps' do -# connection = PG.connect(dbname: 'bookmark_manager_test') - -# bookmark = Bookmark.create(message: "http://www.makersacademy.com", title: "Makers Academy") -# Bookmark.create(message: "http://www.destroyallsoftware.com", title: "Destroy All Software") -# Bookmark.create(message: "http://www.google.com", title: "Google") - -# peeps = Bookmark.all - -# expect(peeps.length).to eq 3 -# expect(peeps.first).to be_a Bookmark -# expect(peeps.first.id).to eq bookmark.id -# expect(peeps.first.title).to eq 'Makers Academy' -# expect(peeps.first.message).to eq 'http://www.makersacademy.com' -# end -# end - -# describe '.create' do -# it 'creates a new bookmark' do -# bookmark = Bookmark.create(message: 'http://www.example.org', title: 'Test Bookmark') -# persisted_data = persisted_data(id: bookmark.id) - -# expect(bookmark).to be_a Bookmark -# expect(bookmark.id).to eq persisted_data['id'] -# expect(bookmark.title).to eq 'Test Bookmark' -# expect(bookmark.message).to eq 'http://www.example.org' -# end -# end - -# describe '.delete' do -# it 'deletes the given bookmark' do -# bookmark = Bookmark.create(title: 'Makers Academy', message: 'http://www.makersacademy.com') - -# Bookmark.delete(id: bookmark.id) - -# expect(Bookmark.all.length). to eq 0 -# end -# end - -# describe '.update' do -# it 'updates the bookmark with the given data' do -# bookmark = Bookmark.create(title: 'Makers Academy', message: 'http://www.makersacademy.com') -# updated_bookmark = Bookmark.update(id: bookmark.id, message: 'http://www.snakersacademy.com', title: 'Snakers Academy') - -# expect(updated_bookmark).to be_a Bookmark -# expect(updated_bookmark.id).to eq bookmark.id -# expect(updated_bookmark.title).to eq 'Snakers Academy' -# expect(updated_bookmark.message).to eq 'http://www.snakersacademy.com' -# end -# end - -# describe '.find' do -# it 'returns the requested bookmark object' do -# bookmark = Bookmark.create(title: 'Makers Academy', message: 'http://www.makersacademy.com') - -# result = Bookmark.find(id: bookmark.id) - -# expect(result).to be_a Bookmark -# expect(result.id).to eq bookmark.id -# expect(result.title).to eq 'Makers Academy' -# expect(result.message).to eq 'http://www.makersacademy.com' -# end -# end -# end diff --git a/views/allpeeps.erb b/views/allpeeps.erb new file mode 100644 index 00000000..31c59b1f --- /dev/null +++ b/views/allpeeps.erb @@ -0,0 +1,6 @@ +

All Peeps:

+
    +<% @messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
From 7498e7a9ccb21068cd8a0ff16a12ad7a5928eb5f Mon Sep 17 00:00:00 2001 From: stephaniesomade Date: Sat, 2 Apr 2022 18:17:56 +0100 Subject: [PATCH 4/7] Adding peep userstory --- app.rb | 11 +++++++++++ lib/messages.rb | 9 +++++++++ spec/features/adding_peep_spec.rb | 15 +++++++++++++++ spec/features/view_messages_spec.rb | 14 +++++++------- spec/messages_spec.rb | 14 +++++++++++--- views/index.erb | 6 +++++- views/post.erb | 5 +++++ 7 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 spec/features/adding_peep_spec.rb create mode 100644 views/post.erb diff --git a/app.rb b/app.rb index 8d78e73c..9a28e922 100644 --- a/app.rb +++ b/app.rb @@ -20,5 +20,16 @@ class Chitter < Sinatra::Base erb :allpeeps end + get '/drafts' do + erb :post + end + + post '/postpeep' do + peep = params['message'] + Messages.create(message: peep) + redirect('/chitter') + end + + run! if app_file == $0 end diff --git a/lib/messages.rb b/lib/messages.rb index 35f610cf..be690fdb 100644 --- a/lib/messages.rb +++ b/lib/messages.rb @@ -11,4 +11,13 @@ def self.all result = connection.exec('SELECT * FROM peeps;') result.map { |message| message['message'] } end + + def self.create(message:) + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + connection.exec("INSERT INTO peeps (message) VALUES('#{message}')") + end end diff --git a/spec/features/adding_peep_spec.rb b/spec/features/adding_peep_spec.rb new file mode 100644 index 00000000..ce2b63f6 --- /dev/null +++ b/spec/features/adding_peep_spec.rb @@ -0,0 +1,15 @@ +feature 'Viewing added peeps' do + scenario 'adds peeps to the list' do + visit('/chitter') + click_button('Post a Peep') + + expect(current_path).to eq '/drafts' + + fill_in('message', with: 'This is my first peep!') + click_button('Submit') + expect(current_path).to eq '/chitter' + click_button('View all Peeps') + + expect(page).to have_content "This is my first peep!" +end +end diff --git a/spec/features/view_messages_spec.rb b/spec/features/view_messages_spec.rb index 9999300c..1fad98fa 100644 --- a/spec/features/view_messages_spec.rb +++ b/spec/features/view_messages_spec.rb @@ -5,15 +5,15 @@ connection = PG.connect(dbname: 'chitter_test') - connection.exec("INSERT INTO peeps (message) VALUES ('Hello');") - connection.exec("INSERT INTO peeps (message) VALUES('Hi!');") - connection.exec("INSERT INTO peeps (message) VALUES('Hiya.');") - + Messages.create(message: 'Update - I am in Paris') + Messages.create(message: 'Just got back from the park!') + Messages.create(message: 'Hi Chitter.') + visit ('/peeps') expect(current_path).to eq '/peeps' - expect(page).to have_content 'Hello' - expect(page).to have_content 'Hi!' - expect(page).to have_content 'Hiya.' + expect(page).to have_content 'Update - I am in Paris' + expect(page).to have_content 'Just got back from the park!' + expect(page).to have_content 'Hi Chitter.' end end diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb index 9c7941cf..f5336fe8 100644 --- a/spec/messages_spec.rb +++ b/spec/messages_spec.rb @@ -6,9 +6,9 @@ it 'returns a list of messages' do connection = PG.connect(dbname: 'chitter_test') - connection.exec("INSERT INTO peeps (message) VALUES ('Hello');") - connection.exec("INSERT INTO peeps (message) VALUES('Hi!');") - connection.exec("INSERT INTO peeps (message) VALUES('Hiya.');") + Messages.create(message: 'Hello') + Messages.create(message: 'Hi!') + Messages.create(message: 'Hiya.') messages = Messages.all @@ -18,4 +18,12 @@ expect(messages).to include "Hiya." end end + + describe '.create' do + it 'creates a new posted peep' do + Messages.create(message: "This is my first peep!") + + expect(Messages.all).to include 'This is my first peep!' + end end +end \ No newline at end of file diff --git a/views/index.erb b/views/index.erb index 44f941b1..319d5331 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,4 +1,8 @@

Welcome to Chitter:

- + +
+ +
+
diff --git a/views/post.erb b/views/post.erb new file mode 100644 index 00000000..099342fc --- /dev/null +++ b/views/post.erb @@ -0,0 +1,5 @@ +

Post a Peep:

+
+ + +
From 597126329caa28e7c4bc707676c283249d4197ae Mon Sep 17 00:00:00 2001 From: stephaniesomade Date: Thu, 7 Apr 2022 19:35:23 +0100 Subject: [PATCH 5/7] Added post and view buttons --- app.rb | 4 -- spec/features/adding_peep_spec.rb | 18 +++--- spec/features/test_page_spec.rb | 6 -- spec/messages_spec.rb | 2 +- spec/spec_helper.rb | 95 +------------------------------ 5 files changed, 11 insertions(+), 114 deletions(-) delete mode 100644 spec/features/test_page_spec.rb diff --git a/app.rb b/app.rb index 9a28e922..515c867b 100644 --- a/app.rb +++ b/app.rb @@ -7,10 +7,6 @@ class Chitter < Sinatra::Base enable :reloader end - get '/test' do - 'Test page' - end - get '/chitter' do erb :index end diff --git a/spec/features/adding_peep_spec.rb b/spec/features/adding_peep_spec.rb index ce2b63f6..a37d90b4 100644 --- a/spec/features/adding_peep_spec.rb +++ b/spec/features/adding_peep_spec.rb @@ -1,15 +1,15 @@ feature 'Viewing added peeps' do scenario 'adds peeps to the list' do - visit('/chitter') - click_button('Post a Peep') + visit('/chitter') + click_button('Post a Peep') - expect(current_path).to eq '/drafts' + expect(current_path).to eq '/drafts' - fill_in('message', with: 'This is my first peep!') - click_button('Submit') - expect(current_path).to eq '/chitter' - click_button('View all Peeps') + fill_in('message', with: 'This is my first peep!') + click_button('Submit') + expect(current_path).to eq '/chitter' + click_button('View all Peeps') - expect(page).to have_content "This is my first peep!" -end + expect(page).to have_content "This is my first peep!" + end end diff --git a/spec/features/test_page_spec.rb b/spec/features/test_page_spec.rb deleted file mode 100644 index b65ac196..00000000 --- a/spec/features/test_page_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -feature 'Viewing test page' do - scenario 'visiting the test page' do - visit('/test') - expect(page).to have_content "Test page" - end -end diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb index f5336fe8..747c8d7e 100644 --- a/spec/messages_spec.rb +++ b/spec/messages_spec.rb @@ -22,7 +22,7 @@ describe '.create' do it 'creates a new posted peep' do Messages.create(message: "This is my first peep!") - + expect(Messages.all).to include 'This is my first peep!' end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74046cad..b13e4e8c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,19 +1,3 @@ -# This file was generated by the `rspec --init` command. Conventionally, all -# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. -# The generated `.rspec` file contains `--require spec_helper` which will cause -# this file to always be loaded, without a need to explicitly require it in any -# files. -# -# Given that it is always loaded, you are encouraged to keep this file as -# light-weight as possible. Requiring heavyweight dependencies from this file -# will add to the boot time of your test suite on EVERY test run, even for an -# individual file that may not need all of that loaded. Instead, consider making -# a separate helper file that requires the additional dependencies and performs -# the additional setup, and require it from the spec files that actually need -# it. -# -# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration -# Require all the testing gems require 'capybara' require 'capybara/rspec' require 'rspec' @@ -24,18 +8,15 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, - # Want a nice code coverage website? Uncomment this next line! - # SimpleCov::Formatter::HTMLFormatter + SimpleCov::Formatter::HTMLFormatter ]) SimpleCov.start 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') -# Tell Capybara to talk to Chitter Capybara.app = Chitter RSpec.configure do |config| @@ -51,87 +32,13 @@ end RSpec.configure do |config| - # rspec-expectations config goes here. You can use an alternate - # assertion/expectation library such as wrong or the stdlib/minitest - # assertions if you prefer. config.expect_with :rspec do |expectations| - # This option will default to `true` in RSpec 4. It makes the `description` - # and `failure_message` of custom matchers include text for helper methods - # defined using `chain`, e.g.: - # be_bigger_than(2).and_smaller_than(4).description - # # => "be bigger than 2 and smaller than 4" - # ...rather than: - # # => "be bigger than 2" expectations.include_chain_clauses_in_custom_matcher_descriptions = true end - # rspec-mocks config goes here. You can use an alternate test double - # library (such as bogus or mocha) by changing the `mock_with` option here. config.mock_with :rspec do |mocks| - # Prevents you from mocking or stubbing a method that does not exist on - # a real object. This is generally recommended, and will default to - # `true` in RSpec 4. mocks.verify_partial_doubles = true end - # This option will default to `:apply_to_host_groups` in RSpec 4 (and will - # have no way to turn it off -- the option exists only for backwards - # compatibility in RSpec 3). It causes shared context metadata to be - # inherited by the metadata hash of host groups and examples, rather than - # triggering implicit auto-inclusion in groups with matching metadata. config.shared_context_metadata_behavior = :apply_to_host_groups - -# The settings below are suggested to provide a good initial experience -# with RSpec, but feel free to customize to your heart's content. -=begin - # This allows you to limit a spec run to individual examples or groups - # you care about by tagging them with `:focus` metadata. When nothing - # is tagged with `:focus`, all examples get run. RSpec also provides - # aliases for `it`, `describe`, and `context` that include `:focus` - # metadata: `fit`, `fdescribe` and `fcontext`, respectively. - config.filter_run_when_matching :focus - - # Allows RSpec to persist some state between runs in order to support - # the `--only-failures` and `--next-failure` CLI options. We recommend - # you configure your source control system to ignore this file. - config.example_status_persistence_file_path = "spec/examples.txt" - - # Limits the available syntax to the non-monkey patched syntax that is - # recommended. For more details, see: - # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ - # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ - # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode - config.disable_monkey_patching! - - # This setting enables warnings. It's recommended, but in some cases may - # be too noisy due to issues in dependencies. - config.warnings = true - - # Many RSpec users commonly either run the entire suite or an individual - # file, and it's useful to allow more verbose output when running an - # individual spec file. - if config.files_to_run.one? - # Use the documentation formatter for detailed output, - # unless a formatter has already been configured - # (e.g. via a command-line flag). - config.default_formatter = "doc" - end - - # Print the 10 slowest examples and example groups at the - # end of the spec run, to help surface which specs are running - # particularly slow. - config.profile_examples = 10 - - # Run specs in random order to surface order dependencies. If you find an - # order dependency and want to debug it, you can fix the order by providing - # the seed, which is printed after each run. - # --seed 1234 - config.order = :random - - # Seed global randomization in this process using the `--seed` CLI option. - # Setting this allows you to use `--seed` to deterministically reproduce - # test failures related to randomization by passing the same `--seed` value - # as the one that triggered the failure. - Kernel.srand config.seed -=end end From 8526dfafff42463fba2aac7af9254bb134c384de Mon Sep 17 00:00:00 2001 From: stephaniesomade Date: Thu, 7 Apr 2022 20:33:53 +0100 Subject: [PATCH 6/7] Wrapped data in objects --- app.rb | 3 +++ lib/database_connection.rb | 15 +++++++++++++++ lib/messages.rb | 17 +++++++++++------ spec/databaseconnection_spec.rb | 15 +++++++++++++++ spec/db_setup_helper.rb | 8 ++++++++ spec/messages_spec.rb | 3 ++- spec/spec_helper.rb | 7 +++---- 7 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 lib/database_connection.rb create mode 100644 spec/databaseconnection_spec.rb create mode 100644 spec/db_setup_helper.rb diff --git a/app.rb b/app.rb index 515c867b..7da43055 100644 --- a/app.rb +++ b/app.rb @@ -1,5 +1,8 @@ require 'sinatra/base' require './lib/messages' +require './lib/database_connection' +require './spec/db_setup_helper' + class Chitter < Sinatra::Base enable :sessions diff --git a/lib/database_connection.rb b/lib/database_connection.rb new file mode 100644 index 00000000..3f59ee2d --- /dev/null +++ b/lib/database_connection.rb @@ -0,0 +1,15 @@ +require 'pg' + +class DatabaseConnection + def initialize + @connect + end + + def self.setup(dbname) + @connect = PG.connect(dbname: dbname) + end + + def self.query(query, params=[]) + @connect.exec_params(query, params) + end +end \ No newline at end of file diff --git a/lib/messages.rb b/lib/messages.rb index be690fdb..12bb8478 100644 --- a/lib/messages.rb +++ b/lib/messages.rb @@ -1,6 +1,12 @@ require 'pg' class Messages + attr_reader :id, :message + + def initialize(id:, message:) + @id = id + @message = message + end def self.all if ENV['ENVIRONMENT'] == 'test' @@ -13,11 +19,10 @@ def self.all end def self.create(message:) - if ENV['ENVIRONMENT'] == 'test' - connection = PG.connect(dbname: 'chitter_test') - else - connection = PG.connect(dbname: 'chitter') + result = DatabaseConnection.query( + "INSERT INTO peeps (message) VALUES ($1) RETURNING id, message;", [message] + ) + Messages.new(id: result[0]['id'],message: result[0]['message']) end - connection.exec("INSERT INTO peeps (message) VALUES('#{message}')") - end end + diff --git a/spec/databaseconnection_spec.rb b/spec/databaseconnection_spec.rb new file mode 100644 index 00000000..faa1f8e2 --- /dev/null +++ b/spec/databaseconnection_spec.rb @@ -0,0 +1,15 @@ +require 'pg' +require 'database_connection' + +describe DatabaseConnection do + it 'connects to the database and shows message' do + DatabaseConnection.query("INSERT INTO peeps (message) VALUES ($1);", ['Hi there Chitter!']) + result = DatabaseConnection.query( + "SELECT * FROM peeps;", + ) + result.map {|message| {"id" => message['id'], "message" => message['message']}} + expect(result[0]['message']).to eq 'Hi there Chitter!' + end +end + + diff --git a/spec/db_setup_helper.rb b/spec/db_setup_helper.rb new file mode 100644 index 00000000..15bee303 --- /dev/null +++ b/spec/db_setup_helper.rb @@ -0,0 +1,8 @@ +require './lib/database_connection' + +p "Reading the environment variable" +if ENV['ENVIRONMENT'] == 'test' + DatabaseConnection.setup('chitter_test') +else + DatabaseConnection.setup('chitter') +end \ No newline at end of file diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb index 747c8d7e..705eb294 100644 --- a/spec/messages_spec.rb +++ b/spec/messages_spec.rb @@ -6,13 +6,14 @@ it 'returns a list of messages' do connection = PG.connect(dbname: 'chitter_test') - Messages.create(message: 'Hello') + message1 = Messages.create(message: 'Hello') Messages.create(message: 'Hi!') Messages.create(message: 'Hiya.') messages = Messages.all expect(messages.length).to eq 3 + expect(message1).to be_a Messages expect(messages).to include "Hello" expect(messages).to include "Hi!" expect(messages).to include "Hiya." diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b13e4e8c..71994a46 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,6 @@ +p "Test Environment Initiated" +ENV['ENVIRONMENT'] = 'test' +require File.join(File.dirname(__FILE__), '..', 'app.rb') require 'capybara' require 'capybara/rspec' require 'rspec' @@ -12,10 +15,6 @@ ]) SimpleCov.start -ENV['RACK_ENV'] = 'test' -ENV['ENVIRONMENT'] = 'test' - -require File.join(File.dirname(__FILE__), '..', 'app.rb') Capybara.app = Chitter From 1347a24d8ab3487888030803bfbf95557585de36 Mon Sep 17 00:00:00 2001 From: stephaniesomade Date: Fri, 8 Apr 2022 00:11:59 +0100 Subject: [PATCH 7/7] Added user class and db --- Gemfile | 4 ++++ app.rb | 13 +++++++++++-- db/migrations/02_create_user_table.sql | 1 + lib/database_connection.rb | 4 ++-- lib/messages.rb | 14 ++++++++------ lib/user.rb | 17 +++++++++++++++++ public/styles.css | 0 spec/databaseconnection_spec.rb | 16 +++++++--------- spec/db_setup_helper.rb | 4 ++-- spec/features/adding_peep_spec.rb | 16 ++++++++-------- spec/features/registration_spec.rb | 10 ++++++++++ spec/features/view_messages_spec.rb | 3 --- spec/messages_spec.rb | 6 ++---- spec/persisted_data.rb | 7 +++++++ spec/setup_test_database.rb | 2 +- spec/spec_helper.rb | 1 - spec/user_spec.rb | 16 ++++++++++++++++ views/index.erb | 4 +++- views/users/new.erb | 5 +++++ 19 files changed, 104 insertions(+), 39 deletions(-) create mode 100644 db/migrations/02_create_user_table.sql create mode 100644 lib/user.rb create mode 100644 public/styles.css create mode 100644 spec/features/registration_spec.rb create mode 100644 spec/persisted_data.rb create mode 100644 spec/user_spec.rb create mode 100644 views/users/new.erb diff --git a/Gemfile b/Gemfile index df28a014..84aaec00 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,10 @@ ruby '3.0.2' gem 'pg' gem 'sinatra' +gem 'sinatra-contrib' +gem 'sinatra-flash' +gem 'psql' +gem 'launchy' gem 'thin' gem 'puma' gem 'reel' diff --git a/app.rb b/app.rb index 7da43055..2c0d86fe 100644 --- a/app.rb +++ b/app.rb @@ -3,14 +3,24 @@ require './lib/database_connection' require './spec/db_setup_helper' - class Chitter < Sinatra::Base enable :sessions configure do enable :reloader end + get '/newuser' do + erb :'users/new' + end + + post '/newuser' do + @user = User.create(params[:email], params[:password]) + session["email"] = params[:email] + redirect '/chitter' + end + get '/chitter' do + @email = session["email"] erb :index end @@ -29,6 +39,5 @@ class Chitter < Sinatra::Base redirect('/chitter') end - run! if app_file == $0 end diff --git a/db/migrations/02_create_user_table.sql b/db/migrations/02_create_user_table.sql new file mode 100644 index 00000000..b2899572 --- /dev/null +++ b/db/migrations/02_create_user_table.sql @@ -0,0 +1 @@ +CREATE TABLE users(id SERIAL PRIMARY KEY, email VARCHAR(60), password VARCHAR(140)); diff --git a/lib/database_connection.rb b/lib/database_connection.rb index 3f59ee2d..c2a83987 100644 --- a/lib/database_connection.rb +++ b/lib/database_connection.rb @@ -9,7 +9,7 @@ def self.setup(dbname) @connect = PG.connect(dbname: dbname) end - def self.query(query, params=[]) + def self.query(query, params = []) @connect.exec_params(query, params) end -end \ No newline at end of file +end diff --git a/lib/messages.rb b/lib/messages.rb index 12bb8478..03670f1c 100644 --- a/lib/messages.rb +++ b/lib/messages.rb @@ -11,11 +11,14 @@ def initialize(id:, message:) def self.all if ENV['ENVIRONMENT'] == 'test' connection = PG.connect(dbname: 'chitter_test') - else + else connection = PG.connect(dbname: 'chitter') - end - result = connection.exec('SELECT * FROM peeps;') - result.map { |message| message['message'] } + end + result = connection.exec("SELECT * FROM peeps") + result.map do |m| + Messages.new(id: m['id'], message: m['message']) + m['message'] + end end def self.create(message:) @@ -23,6 +26,5 @@ def self.create(message:) "INSERT INTO peeps (message) VALUES ($1) RETURNING id, message;", [message] ) Messages.new(id: result[0]['id'],message: result[0]['message']) - end + end end - diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..8b9225c6 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,17 @@ +require_relative './database_connection.rb' + +class User + attr_reader :id, :email + + def initialize(id:, email:) + @id = id + @email = email + end + + def self.create(email, password) + result = DatabaseConnection.query( + "INSERT INTO users (email, password) VALUES($1, $2) RETURNING id, email;", [email, password] + ) + User.new(id: result[0]['id'], email: result[0]['email']) + end +end \ No newline at end of file diff --git a/public/styles.css b/public/styles.css new file mode 100644 index 00000000..e69de29b diff --git a/spec/databaseconnection_spec.rb b/spec/databaseconnection_spec.rb index faa1f8e2..a0a39f06 100644 --- a/spec/databaseconnection_spec.rb +++ b/spec/databaseconnection_spec.rb @@ -3,13 +3,11 @@ describe DatabaseConnection do it 'connects to the database and shows message' do - DatabaseConnection.query("INSERT INTO peeps (message) VALUES ($1);", ['Hi there Chitter!']) - result = DatabaseConnection.query( - "SELECT * FROM peeps;", - ) - result.map {|message| {"id" => message['id'], "message" => message['message']}} - expect(result[0]['message']).to eq 'Hi there Chitter!' + DatabaseConnection.query("INSERT INTO peeps (message) VALUES ($1);", ['Hi there Chitter!']) + result = DatabaseConnection.query( + "SELECT * FROM peeps;", + ) + result.map { |message| { "id" => message['id'], "message" => message['message'] } } + expect(result[0]['message']).to eq 'Hi there Chitter!' end -end - - +end diff --git a/spec/db_setup_helper.rb b/spec/db_setup_helper.rb index 15bee303..3752224f 100644 --- a/spec/db_setup_helper.rb +++ b/spec/db_setup_helper.rb @@ -1,8 +1,8 @@ require './lib/database_connection' -p "Reading the environment variable" +p "Reading the Environment Variable" if ENV['ENVIRONMENT'] == 'test' DatabaseConnection.setup('chitter_test') else DatabaseConnection.setup('chitter') -end \ No newline at end of file +end diff --git a/spec/features/adding_peep_spec.rb b/spec/features/adding_peep_spec.rb index a37d90b4..30001768 100644 --- a/spec/features/adding_peep_spec.rb +++ b/spec/features/adding_peep_spec.rb @@ -1,15 +1,15 @@ feature 'Viewing added peeps' do scenario 'adds peeps to the list' do - visit('/chitter') - click_button('Post a Peep') + visit('/chitter') + click_button('Post a Peep') - expect(current_path).to eq '/drafts' + expect(current_path).to eq '/drafts' - fill_in('message', with: 'This is my first peep!') - click_button('Submit') - expect(current_path).to eq '/chitter' - click_button('View all Peeps') + fill_in('message', with: 'This is my first peep!') + click_button('Submit') + expect(current_path).to eq '/chitter' + click_button('View all Peeps') - expect(page).to have_content "This is my first peep!" + expect(page).to have_content "This is my first peep!" end end diff --git a/spec/features/registration_spec.rb b/spec/features/registration_spec.rb new file mode 100644 index 00000000..e6549b56 --- /dev/null +++ b/spec/features/registration_spec.rb @@ -0,0 +1,10 @@ +feature 'registration' do + scenario 'a user is able to sign up' do + visit '/newuser' + fill_in('email', with: 'test@example.com') + fill_in('password', with: 'password1') + click_button 'Submit' + expect(current_path).to eq '/chitter' + expect(page).to have_content 'Welcome to Chitter test@example.com' + end +end \ No newline at end of file diff --git a/spec/features/view_messages_spec.rb b/spec/features/view_messages_spec.rb index 1fad98fa..7d1257d7 100644 --- a/spec/features/view_messages_spec.rb +++ b/spec/features/view_messages_spec.rb @@ -2,9 +2,6 @@ feature 'Viewing messages' do scenario 'shows all messages' do - - connection = PG.connect(dbname: 'chitter_test') - Messages.create(message: 'Update - I am in Paris') Messages.create(message: 'Just got back from the park!') Messages.create(message: 'Hi Chitter.') diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb index 705eb294..f364839e 100644 --- a/spec/messages_spec.rb +++ b/spec/messages_spec.rb @@ -4,8 +4,6 @@ describe Messages do describe '.all' do it 'returns a list of messages' do - connection = PG.connect(dbname: 'chitter_test') - message1 = Messages.create(message: 'Hello') Messages.create(message: 'Hi!') Messages.create(message: 'Hiya.') @@ -25,6 +23,6 @@ Messages.create(message: "This is my first peep!") expect(Messages.all).to include 'This is my first peep!' + end end -end -end \ No newline at end of file +end diff --git a/spec/persisted_data.rb b/spec/persisted_data.rb new file mode 100644 index 00000000..fbcd2b5d --- /dev/null +++ b/spec/persisted_data.rb @@ -0,0 +1,7 @@ +require 'pg' +require 'db_setup_helper' + +def persisted_data(id:, table:) + connection = PG.connect(dbname: 'chitter_test') + connection.query("SELECT * FROM #{table} WHERE id = '#{id}';") +end diff --git a/spec/setup_test_database.rb b/spec/setup_test_database.rb index af832f7d..88c354bb 100644 --- a/spec/setup_test_database.rb +++ b/spec/setup_test_database.rb @@ -2,7 +2,7 @@ def setup_test_database connection = PG.connect(dbname: 'chitter_test') - connection.exec("TRUNCATE peeps;") + connection.exec("TRUNCATE peeps, users;") end def add_row_to_test_database diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 71994a46..905cf495 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,7 +15,6 @@ ]) SimpleCov.start - Capybara.app = Chitter RSpec.configure do |config| diff --git a/spec/user_spec.rb b/spec/user_spec.rb new file mode 100644 index 00000000..01e1d9d5 --- /dev/null +++ b/spec/user_spec.rb @@ -0,0 +1,16 @@ +require 'user' +require './lib/database_connection' +require 'persisted_data' + +RSpec.describe User do + describe '.create' do + it 'creates a User and adds them to the database' do + user = User.create("test@example.com", "password1") + persisted_data = persisted_data(id: user.id, table: 'users') + + expect(user.id).to eq persisted_data.first['id'] + expect(user.email).to eq "test@example.com" + expect(user).to be_a User + end + end +end \ No newline at end of file diff --git a/views/index.erb b/views/index.erb index 319d5331..f938cfca 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,4 +1,6 @@ -

Welcome to Chitter:

+<% if @email%> +

Welcome to Chitter <%= @email%>

+<% end %>
diff --git a/views/users/new.erb b/views/users/new.erb new file mode 100644 index 00000000..6dd6768b --- /dev/null +++ b/views/users/new.erb @@ -0,0 +1,5 @@ +
+ + + +
\ No newline at end of file