# frozen_string_literal: true require 'test_helper' module Upgrow class ActiveRecordAdapterTest < ActiveSupport::TestCase class User < Model attribute :name end class UserInput < Input attribute :name end class UserRepository < BasicRepository include ActiveRecordAdapter end class UserRecord; end setup do freeze_time @base = Minitest::Mock.new @repository = UserRepository.new(base: @base) @record = Minitest::Mock.new @record_attributes = { name: 'volmer', id: 1, created_at: Time.now, updated_at: Time.now } @record.expect(:attributes, @record_attributes) end test '.new sets base to the Active Record Base class according to the Repository name by default' do repository = UserRepository.new assert_equal UserRecord, repository.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 assert_equal Time.now, all.first.created_at assert_equal Time.now, all.first.updated_at 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 assert_equal Time.now, model.created_at assert_equal Time.now, model.updated_at 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 assert_equal Time.now, model.created_at assert_equal Time.now, model.updated_at 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 assert_equal Time.now, model.created_at assert_equal Time.now, model.updated_at end test '#delete deletes the Record with the given ID' do @base.expect(:destroy, true, [1]) @repository.delete(1) end end end