Skip to content

Commit a48fc87

Browse files
committed
Started writing tests that don't hit the db
1 parent c9f4d55 commit a48fc87

File tree

6 files changed

+94
-10
lines changed

6 files changed

+94
-10
lines changed

Gemfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ gem 'colored'
1212
gem 'pry'
1313
gem 'pry-coolline'
1414
gem 'rake'
15-
gem 'minitest', group: :test
16-
gem 'codeclimate-test-reporter', group: :test, require: nil
15+
16+
group :test do
17+
gem 'activerecord-nulldb-adapter', :git => 'git://github.com/nulldb/nulldb.git'
18+
gem 'minitest'
19+
gem 'codeclimate-test-reporter', require: nil
20+
end

db/schema.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This file is auto-generated from the current state of the database. Instead
2+
# of editing this file, please use the migrations feature of Active Record to
3+
# incrementally modify your database, and then regenerate this schema definition.
4+
#
5+
# Note that this schema.rb definition is the authoritative source for your
6+
# database schema. If you need to create the application database on another
7+
# system, you should be using db:schema:load, not running all the migrations
8+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
9+
# you'll amass, the slower it'll run and the greater likelihood for issues).
10+
#
11+
# It's strongly recommended that you check this file into your version control system.
12+
13+
ActiveRecord::Schema.define(version: 0) do
14+
15+
# These are extensions that must be enabled in order to support this database
16+
enable_extension "plpgsql"
17+
18+
create_table "ingredients", force: true do |t|
19+
t.text "name", null: false
20+
t.integer "recipe_frequency", default: 0, null: false
21+
t.integer "recipe_ids", default: [], null: false, array: true
22+
end
23+
24+
create_table "recipes", force: true do |t|
25+
t.text "source_uri", null: false
26+
t.text "source_ingredients", default: [], null: false, array: true
27+
t.integer "ingredient_ids", default: [], null: false, array: true
28+
t.boolean "has_been_scraped", default: false, null: false
29+
t.text "title"
30+
t.text "description"
31+
end
32+
33+
end

dump_schema.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require './environment.rb'
2+
3+
ActiveRecord::SchemaDumper.dump ActiveRecord::Base.connection, File.open("#{__dir__}/db/schema.rb", 'w')

models/ingredient.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Represents an ingredient used within many recipes
22
class Ingredient < ActiveRecord::Base
3+
validates_presence_of :name
4+
5+
def recipe_linked?(recipe_id)
6+
Ingredient.where(id: id).where('? = ANY (recipe_ids)', recipe_id).any?
7+
end
8+
39
def link_to_recipe!(recipe_id)
410
tap do |i|
5-
if Ingredient.where(id: i.id).where('? = ANY (recipe_ids)', recipe_id)
6-
.any?
7-
fail 'Already Linked'
8-
end
11+
fail 'Already Linked' if recipe_linked?(recipe_id)
912

1013
i.recipe_frequency += 1
1114

test/ingredient_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require_relative './test_helper'
2+
require_relative '../models/ingredient.rb'
3+
4+
describe Ingredient do
5+
it 'requires a name' do
6+
assert_raises(ActiveRecord::RecordInvalid) { Ingredient.new.save! }
7+
end
8+
9+
it 'can be linked to a recipe' do
10+
ingredient = Ingredient.new(name: 'mock_ingredient')
11+
.tap { |i| i.recipe_ids = [] }
12+
13+
# Mocking postgres behaviour for the newly-created instance:
14+
# The "already linked" check
15+
def ingredient.recipe_linked?(id); false end
16+
# The int[] field of recipe ids
17+
ingredient.instance_variable_set :@recipe_ids, []
18+
def ingredient.recipe_ids; @recipe_ids end
19+
20+
assert_equal 0, ingredient.recipe_frequency
21+
22+
RECIPE_ID = 10
23+
ingredient.link_to_recipe!(RECIPE_ID)
24+
25+
assert_equal 1, ingredient.recipe_frequency
26+
assert_equal [RECIPE_ID], ingredient.recipe_ids
27+
end
28+
end

test/test_helper.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
require 'codeclimate-test-reporter'
2-
CodeClimate::TestReporter.start
1+
require 'codeclimate-test-reporter'
2+
CodeClimate::TestReporter.start
33

4-
require 'minitest/spec'
5-
require 'minitest/autorun'
4+
require 'minitest/spec'
5+
require 'minitest/autorun'
6+
7+
require_relative '../environment.rb'
8+
9+
# Mocking out Rails.root so that nulldb/rails plays nicely with MiniTest
10+
module Rails
11+
module_function
12+
def root
13+
__dir__ + '/../'
14+
end
15+
end
16+
require 'nulldb/rails'
17+
18+
ActiveRecord::Base.establish_connection adapter: :nulldb, schema: './db/schema.rb'

0 commit comments

Comments
 (0)