diff --git a/.gitignore b/.gitignore index 5399405e..022fa62f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ coverage # Local cache of Rubocop remote config .rubocop-* +capybara-*.html diff --git a/Gemfile b/Gemfile index 99d8e519..2fb95daa 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,7 @@ group :test do gem 'rspec' gem 'simplecov', require: false gem 'simplecov-console', require: false + gem 'launchy' end group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index 7d4eb449..5762ffdf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -15,9 +15,13 @@ GEM xpath (~> 3.2) diff-lcs (1.4.4) docile (1.4.0) + launchy (2.5.0) + addressable (~> 2.7) 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,10 +87,12 @@ GEM nokogiri (~> 1.8) PLATFORMS + arm64-darwin-21 x86_64-darwin-20 DEPENDENCIES capybara + launchy pg rspec rubocop (= 1.20) diff --git a/README.md b/README.md index f9638b66..5fab7844 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ command `CREATE DATABASE chitter_test;`; You should see 1 passing test. +## Testing +To test this code run +* rspec + ## User stories ``` diff --git a/app.rb b/app.rb index 2450fb92..207867b4 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,24 @@ require 'sinatra/base' +require './lib/post' class Chitter < Sinatra::Base - get '/test' do - 'Test page' + get '/' do + 'Chitter App' + erb :index + end + + get '/posts' do + @posts = Post.all + erb :see_posts + end + + get '/posts/new' do + erb :new_post + end + + post '/posts' do + Post.create(date: params[:date], author: params[:author], message: params[:message]) + redirect '/posts' end run! if app_file == $0 diff --git a/db/migrations/02_add_author_to_the_post.sql b/db/migrations/02_add_author_to_the_post.sql new file mode 100644 index 00000000..631e251f --- /dev/null +++ b/db/migrations/02_add_author_to_the_post.sql @@ -0,0 +1 @@ +ALTER TABLE peeps ADD COLUMN author VARCHAR(60); \ No newline at end of file diff --git a/db/migrations/03_add_date_to_the_post.sql b/db/migrations/03_add_date_to_the_post.sql new file mode 100644 index 00000000..5a9ca02b --- /dev/null +++ b/db/migrations/03_add_date_to_the_post.sql @@ -0,0 +1 @@ +ALTER TABLE peeps ADD COLUMN date VARCHAR(60); \ No newline at end of file diff --git a/lib/post.rb b/lib/post.rb new file mode 100644 index 00000000..4f90328d --- /dev/null +++ b/lib/post.rb @@ -0,0 +1,35 @@ +require 'pg' + +class Post + attr_reader :id, :date, :author, :message + + def initialize(id:, date:, author:, message:) + @id = id + @date = date + @author = author + @message = message + end + + 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 ORDER BY date DESC") + result.map do |peep| + Post.new(id: peep['id'], date: peep['date'], author: peep['author'], message: peep['message']) + end + end + + def self.create(date:, author:, message:) + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + + result = connection.exec_params("INSERT INTO peeps (date, author, message) VALUES('#{date}', $1, $2) RETURNING id, date, author, message;", [author, message]) + Post.new(id: result[0]['id'], date: result[0]['date'], author: result[0]['author'], message: result[0]['message']) + end +end diff --git a/spec/database_helpers.rb b/spec/database_helpers.rb new file mode 100644 index 00000000..ce8e636c --- /dev/null +++ b/spec/database_helpers.rb @@ -0,0 +1,6 @@ +require 'pg' + +def persisted_data(id:) + connection = PG.connect(dbname: 'chitter_test') + result = connection.query("SELECT * FROM peeps WHERE id = #{id};") +end diff --git a/spec/features/creating_post_spec.rb b/spec/features/creating_post_spec.rb new file mode 100644 index 00000000..b6c20905 --- /dev/null +++ b/spec/features/creating_post_spec.rb @@ -0,0 +1,13 @@ +feature 'Adding a new post' do + scenario 'a user can add a new post' do + visit('/posts/new') + fill_in("date", with: "2022-04-01") + fill_in("author", with: "Rose") + fill_in("message", with: "How are you?") + click_button('Submit') + + expect(page).to have_content("2022-04-01") + expect(page).to have_content("Rose") + expect(page).to have_content("How are you?") + 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/features/viewing_posts_spec.rb b/spec/features/viewing_posts_spec.rb new file mode 100644 index 00000000..70cd7c26 --- /dev/null +++ b/spec/features/viewing_posts_spec.rb @@ -0,0 +1,37 @@ +require 'pg' + +feature 'Welcome page' do + scenario 'user visits homepage ' do + visit('/') + expect(page).to have_content "Chitter App" + expect(page).to have_link(href: "/posts") + expect(page).to have_link(href: "/posts/new") + end +end + +feature 'Viewing posts' do + scenario 'visiting /posts shows message' do + + Post.create(date: "2022-04-01", author: "Kate", message: "How are you?") + + visit('/posts') + expect(page).to have_content("2022-04-01") + expect(page).to have_content("Kate") + expect(page).to have_content("How are you?") + + end +end + +feature 'Viewing posts' do + scenario 'visiting /posts shows new messages first' do + + Post.create(date: "2022-04-01", author: "Kate", message: "How are you?") + Post.create(date: "2022-06-01", author: "Kate", message: "How are you?") + Post.create(date: "2022-08-01", author: "Kate", message: "How are you?") + + visit('/posts') + expect(page).to have_content("2022-08-01") + expect(page).to have_content("2022-06-01") + expect(page).to have_content("2022-04-01") + end +end diff --git a/spec/post_spec.rb b/spec/post_spec.rb new file mode 100644 index 00000000..6dbcb93e --- /dev/null +++ b/spec/post_spec.rb @@ -0,0 +1,35 @@ +require 'post' +require 'pg' +require 'database_helpers' + +RSpec.describe Post do + describe '.all' do + it 'returns a list of posts in a reverse chronological order' do + connection = PG.connect(dbname: 'chitter_test') + + post = Post.create(date: "2022-04-01", author: 'Kate', message: 'This is my first post!') + Post.create(date: "2022-04-02", author: 'Rose', message: 'How are you?') + Post.create(date: "2022-04-03", author: 'Emma', message: 'Working from home') + + messages = Post.all + expect(messages.length).to eq 3 + expect(messages.first).to be_a Post + expect(messages.first.date).to eq "2022-04-03" + expect(messages.first.author).to eq 'Emma' + expect(messages.first.message).to eq 'Working from home' + end + end + + describe '.create' do + it 'creates a new post' do + post = Post.create(date: "2022-04-01", author: 'Kate', message: 'This is my first post!') + persisted_data = persisted_data(id: post.id) + + expect(post).to be_a Post + expect(post.id).to eq persisted_data.first['id'] + expect(post.date).to eq '2022-04-01' + expect(post.author).to eq 'Kate' + expect(post.message).to eq 'This is my first post!' + end + end +end diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..15755a20 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,40 @@ +
+ + + +