# frozen_string_literal: true require 'test_helper' module Upgrow class BasicRepositoryTest < ActiveSupport::TestCase class User < Model; end class UserRepository < BasicRepository; end class NoModelRepository < BasicRepository; end setup do freeze_time @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) assert_equal :my_base, repository.base assert_equal User, repository.model_class end test '.new sets the base to nil by default' do assert_nil @repository.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 end test '.new raises a Name Error if the Model class is undefined' do error = assert_raises(NameError) do NoModelRepository.new(base: :my_base) 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 ) 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 } 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 } 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