Skip to content

Instantly share code, notes, and snippets.

@clupprich
Last active August 29, 2015 14:15
Show Gist options
  • Save clupprich/9b2ae833d7f9f0a95e2d to your computer and use it in GitHub Desktop.
Save clupprich/9b2ae833d7f9f0a95e2d to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'bundler/setup'
require 'mongo_mapper'
logger = Logger.new(STDOUT)
MongoMapper.connection = Mongo::Connection.new('localhost', logger: logger)
MongoMapper.database = 'customers'
class Customer
include MongoMapper::Document
key :email, String
key :course, String
scope :no_course, where(course: '')
def self.with_email(email)
first(email: email.downcase)
end
end
require 'minitest/autorun'
require './customer'
class TestCustomer < Minitest::Test
def setup
Customer.destroy_all
Customer.create(email: 'customer1@test.com', course: 'algebra')
Customer.create(email: 'customer2@test.com', course: '')
end
def test_course_scope
assert_equal 1, Customer.no_course.count
end
def test_finder
refute_nil Customer.with_email('CUSTOMER1@TEST.COM')
end
def test_course_scope_and_finder
assert_nil Customer.no_course.with_email('CUSTOMER1@TEST.COM')
end
def test_course_scope_and_finder_2
assert_nil Customer.no_course.first(email: 'CUSTOMER1@TEST.COM')
end
end
source 'https://rubygems.org'
gem 'bson_ext'
gem 'mongo_mapper', '0.13.1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment