test/upgrow/basic_repository_test.rb in upgrow-0.0.2 vs test/upgrow/basic_repository_test.rb in upgrow-0.0.3

- old
+ new

@@ -2,72 +2,93 @@ require 'test_helper' module Upgrow class BasicRepositoryTest < ActiveSupport::TestCase - class User < Model; end + class User < BasicModel; end + class Car < BasicModel + attribute :wheels + end + class UserRepository < BasicRepository; end class NoModelRepository < BasicRepository; end setup do - freeze_time - + @original_base = UserRepository.base + @original_model_class = UserRepository.model_class @repository = UserRepository.new end - test '.new instantiates a new Repository with a base and a Model class' do - repository = UserRepository.new(base: :my_base, model_class: User) + teardown do + UserRepository.base = @original_base + UserRepository.model_class = @original_model_class + end - assert_equal :my_base, repository.base - assert_equal User, repository.model_class + test '.base is nil by default' do + assert_nil UserRepository.base end - test '.new sets the base to nil by default' do - assert_nil @repository.base + test '.base can be set' do + UserRepository.base = :my_base + assert_equal :my_base, UserRepository.base end - test '.new sets the Model class by default to a constant derived from the Repository name' do - assert_equal User, @repository.model_class + test '.model_class is iferred based on the Repository name' do + assert_equal User, UserRepository.model_class end - test '.new raises a Name Error if the Model class is undefined' do + test '.model_class can be set' do + UserRepository.model_class = :my_model_class + assert_equal :my_model_class, UserRepository.model_class + end + + test '.model_class raises a Name Error if the Model class is undefined' do error = assert_raises(NameError) do - NoModelRepository.new(base: :my_base) + NoModelRepository.model_class end assert_includes( error.message, 'uninitialized constant Upgrow::BasicRepositoryTest::NoModel' ) end - test '.to_model returns a new Model with the given attributes as keyword arguments' do - model = @repository.to_model( - id: 1, created_at: Time.now, updated_at: Time.now - ) + test '#base is inferred from the Repository class base' do + UserRepository.base = :my_base + assert_equal :my_base, UserRepository.new.base + end + test '#model_class is inferred from the Repository class Model class' do + UserRepository.model_class = :my_model_class + assert_equal :my_model_class, UserRepository.new.model_class + end + + test '#to_model returns a new Model with the given attributes as keyword arguments' do + model = @repository.to_model(id: 1) + assert_equal 1, model.id - assert_equal Time.now, model.created_at - assert_equal Time.now, model.updated_at end - test '.to_model accepts a Hash of Symbols' do - args = { id: 1, created_at: Time.now, updated_at: Time.now } + test '#to_model accepts a Model class explicitly' do + model = @repository.to_model(Car, id: 1, wheels: 4) + + assert_equal 1, model.id + assert_equal 4, model.wheels + end + + test '#to_model accepts a Hash of Symbols' do + args = { id: 1 } model = @repository.to_model(args) assert_equal 1, model.id - assert_equal Time.now, model.created_at - assert_equal Time.now, model.updated_at end - test '.to_model accepts a Hash of Strings' do - args = { 'id' => 1, 'created_at' => Time.now, 'updated_at' => Time.now } + test '#to_model accepts a Hash of Strings' do + args = { 'id' => 1 } model = @repository.to_model(args) assert_equal 1, model.id - assert_equal Time.now, model.created_at - assert_equal Time.now, model.updated_at end end end