Sha256: c8bb9901d51b7e5170418d99ff69f6b3b024dfbe2323044ac844cc13fcaa849f

Contents?: true

Size: 1.97 KB

Versions: 21

Compression:

Stored size: 1.97 KB

Contents

# Copyright 2020 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

# frozen_string_literal: true

require "test_helper"
require "models/firm"
require "models/account"
require "models/department"


module ActiveRecord
  module Associations
    class HasOneTest < SpannerAdapter::TestCase
      include SpannerAdapter::Associations::TestHelper

      attr_accessor :firm, :account

      def setup
        super

        @account = Account.create name: "Account - #{rand 1000}", credit_limit: 100
        @firm = Firm.create name: "Firm-#{rand 1000}", account: account

        @account.reload
        @firm.reload
      end

      def teardown
        Firm.destroy_all
        Account.destroy_all
        Department.destroy_all
      end

      def test_has_one
        assert_equal account, firm.account
        assert_equal account.credit_limit, firm.account.credit_limit
      end

      def test_has_one_does_not_use_order_by
        sql_log = capture_sql { firm.account }
        assert sql_log.all? { |sql| !/order by/i.match?(sql) }, "ORDER BY was used in the query: #{sql_log}"
      end

      def test_finding_using_primary_key
        assert_equal Account.find_by(firm_id: firm.id), firm.account
      end

      def test_successful_build_association
        account = firm.build_account(credit_limit: 1000)
        assert account.save

        firm.reload
        assert_equal account, firm.account
      end

      def test_delete_associated_records
        assert_equal account, firm.account

        firm.account.destroy
        firm.reload
        assert_nil firm.account
      end

      def test_polymorphic_association
        assert_equal 0, firm.departments.count

        firm.departments.create(name: "Department - 1")
        firm.reload

        assert_equal 1, firm.departments.count
        assert_equal "Department - 1", firm.departments.first.name
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
activerecord-spanner-adapter-1.6.3 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.6.2 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.6.1 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.6.0 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.5.1 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.5.0 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.4.4 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.4.3 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.4.2 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.4.1 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.4.0 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.3.1 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.2.2 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.2.1 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.2.0 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.1.0 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.0.1 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-1.0.0 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-0.7.0 acceptance/cases/associations/has_one_associations_test.rb
activerecord-spanner-adapter-0.6.0 acceptance/cases/associations/has_one_associations_test.rb