Sha256: 46b9a3124a1b82f1d2cf54adf115a6da6e21e9ff6345422dd074c9b77f6560de

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require 'test_helper'

require 'upgrow/basic_model'
require 'upgrow/repository'
require 'upgrow/input'

module Upgrow
  class ActiveRecordQueriesTest < ActiveSupport::TestCase
    class User < BasicModel
      attribute :name
    end

    class UserRecord < TestRecord
    end

    class UserInput < Input
      attribute :name
    end

    class UserRepository < Repository
    end

    setup do
      @base = Minitest::Mock.new
      UserRepository.base = @base
      @repository = UserRepository.new

      @record = UserRecord.new(name: 'volmer', id: 1)
    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')
      new_record = UserRecord.new(id: 1, name: 'rafael')

      @base.expect(:update, new_record, [1, { 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
upgrow-0.0.5 test/upgrow/active_record_queries_test.rb
upgrow-0.0.4 test/upgrow/active_record_queries_test.rb