# frozen_string_literal: true require 'test_helper' require 'upgrow/active_record_adapter' require 'upgrow/basic_model' require 'upgrow/basic_repository' require 'upgrow/input' module Upgrow class ActiveRecordAdapterTest < ActiveSupport::TestCase class User < BasicModel attribute :name end class UserInput < Input attribute :name end class UserRepository < BasicRepository include ActiveRecordAdapter end class UserRecord; end setup do @base = Minitest::Mock.new UserRepository.base = @base @repository = UserRepository.new @record = Minitest::Mock.new @record_attributes = { name: 'volmer', id: 1 } @record.expect(:attributes, @record_attributes) end test '.base is the Active Record Base class according to the Repository name by default' do UserRepository.base = nil assert_equal UserRecord, UserRepository.base end test '#all returns all Records as Models' do @base.expect(:all, [@record]) all = @repository.all assert all.one? assert_equal 1, all.first.id assert_equal 'volmer', all.first.name end test '#find retrieves the Model for the given ID' do @base.expect(:find, @record, [1]) model = @repository.find(1) assert_equal 1, model.id assert_equal 'volmer', model.name end test '#create creates a new Record with the given attributes' do input = UserInput.new(name: 'volmer') @base.expect(:create!, @record, [{ name: 'volmer' }]) model = @repository.create(input) assert_equal 1, model.id assert_equal 'volmer', model.name end test '#update changes the existing Record attributes' do input = UserInput.new(name: 'rafael') @base.expect(:update, @record, [1, { name: 'rafael' }]) @record_attributes[:name] = 'rafael' model = @repository.update(1, input) assert_equal 1, model.id assert_equal 'rafael', model.name end test '#delete deletes the Record with the given ID' do @base.expect(:destroy, true, [1]) @repository.delete(1) end end end