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

Miranda Frudd - chitter challenge (unfinished) #235

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -83,6 +85,7 @@ GEM
nokogiri (~> 1.8)

PLATFORMS
arm64-darwin-21
x86_64-darwin-20

DEPENDENCIES
Expand Down
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ To setup the database:
* 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!');`
* Create the user table using the query saved in 02_create_user_table.sql

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.

Expand All @@ -26,11 +27,17 @@ To setup the test database:
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
* Populate your table with a row by running `INSERT INTO peeps (message) values ('This is a peep!');`
* Create the user table using the query saved in 02_create_user_table.sql

* `bundle install`
* `rspec`

You should see 1 passing test.
## Run

run use ruby app.rb

rackup is not working - currently unsure why

## User stories

Expand Down Expand Up @@ -64,3 +71,51 @@ As a Maker
So that I can find relevant peeps
I want to filter on a specific keyword
```


# FIRST USER STORY

```
As a Maker
So that I can see what people are doing
I want to see all the messages (peeps)
in a browser
```

class Message
def initialize # name is a string
@messages = []
end

def add(message)
# adds the message to @messages
end

def self.all
# Returns @messages
end
end

# TESTS

# 1
RSpec.describe Message do
context "so that I can see what people are doing" do
it "#all displays all messages do
message1 = Message.new
message2 = Message.new
message1.add("First message")
message2.add("Second message")
expect(Message.all).to eq ["First message", "Second message"]
end
end
end

# 2 -> feature test

feature 'messages' do
scenario 'to have a messages on it' do
visit '/messages'
expect(page).to have_content "First message", "Second message"
end
end
18 changes: 16 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
require 'sinatra/base'
require './lib/message'

class Chitter < Sinatra::Base
get '/test' do
'Test page'

enable :sessions

get '/' do
erb :index
end

post '/message' do
Message.create(params[:message])
end

get '/messages' do
@messages = Message.all
p @messages
erb :messages
end

run! if app_file == $0
Expand Down
1 change: 1 addition & 0 deletions db/migrations/02_create_user_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE users(id SERIAL PRIMARY KEY, name VARCHAR(150), email VARCHAR(60), password VARCHAR(60));
22 changes: 22 additions & 0 deletions lib/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'pg'

class Message
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 { |peep| peep['message'] }
end

def self.create(message)
if ENV['ENVIRONMENT'] == 'test'
connection = PG.connect(dbname: 'chitter_test')
else
connection = PG.connect(dbname: 'chitter')
end
Comment on lines +15 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

You're using this same piece of code in more than one place in this file. How might you get rid of this duplication?

connection.exec("INSERT INTO peeps (message) VALUES ('#{message}');")
end
end
22 changes: 22 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'pg'

class User
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 users;')
result.map { |peep| peep['name'] }
end

def self.create(name, email, password)
if ENV['ENVIRONMENT'] == 'test'
connection = PG.connect(dbname: 'chitter_test')
else
connection = PG.connect(dbname: 'chitter')
end
connection.exec("INSERT INTO users (name, email, password) VALUES ('#{name}', '#{email}', '#{password}');")
end
end
7 changes: 7 additions & 0 deletions spec/features/homepage_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
feature '/' do
scenario 'to have a form' do
visit '/'
fill_in 'Message', with: 'First Message'
expect(page).to have_button('Submit')
end
end
9 changes: 9 additions & 0 deletions spec/features/messages_page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
feature '/' do
scenario 'to submit a form' do
visit '/'
fill_in 'Message', with: 'First Message'
click_on 'Submit'
visit '/messages'
expect(page).to have_content "First Message"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good example of a feature test, well done!

end
end
6 changes: 0 additions & 6 deletions spec/features/test_page_spec.rb

This file was deleted.

27 changes: 27 additions & 0 deletions spec/message_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'message'

RSpec.describe '.all' do
context "so that I can see what people are doing" do
it "#all displays all messages" do
connection = PG.connect(dbname: 'chitter_test')

connection.exec("INSERT INTO peeps (message) VALUES ('First message')")
connection.exec("INSERT INTO peeps (message) VALUES ('Second message')")

messages = Message.all

expect(messages).to include "First message"
expect(messages).to include "Second message"

end
end
end

RSpec.describe '.create' do
context "so that I can create a new message" do
it "#create adds a new message" do
message = Message.create('Another message')
expect(Message.all).to include "Another message"
end
end
end
17 changes: 17 additions & 0 deletions spec/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'user'

describe User do
context "#all" do
it "returns all users" do
user1 = User.create("example name1", "name1@example.com", "password1")
expect(User.all[-1]).to eq "example name1"
end
end

context "#create" do
it "adds a user" do
User.create("example name2", "name2@example.com", "password2")
expect(User.all[-1]).to eq "example name2"
end
end
end
10 changes: 10 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<form action="/message" method="post" >
<label for="message">Message</label>
<input id="message" name="message" type="text" />
<input name="submit" type="submit" value="Submit" />
<label for="start">Post date</label>
<input type="date" id="start" name="start"
required pattern="\d{4}-\d{2}-\d{2}"
value="2022-07-22"
min="2022-01-01" max="2023-12-31">
</form>
3 changes: 3 additions & 0 deletions views/messages.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% @messages.each do | message | %>
<li> <%= message %> </li>
<% end %>