File: null_relation_test.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (117 lines) | stat: -rw-r--r-- 3,532 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

require "cases/helper"
require "models/developer"
require "models/comment"
require "models/post"
require "models/topic"
require "support/async_helper"

class NullRelationTest < ActiveRecord::TestCase
  include AsyncHelper

  fixtures :posts, :comments

  def test_none
    assert_no_queries do
      assert_equal [], Developer.none
      assert_equal [], Developer.all.none
    end
  end

  def test_none_chainable
    assert_queries_count(0) do
      assert_equal [], Developer.none.where(name: "David")
    end
  end

  def test_none_chainable_to_existing_scope_extension_method
    assert_no_queries do
      assert_equal 1, Topic.anonymous_extension.none.one
    end
  end

  def test_async_query_on_null_relation
    assert_no_queries do
      assert_equal [], Developer.none.load_async.load
    end
  end

  def test_none_chained_to_methods_firing_queries_straight_to_db
    assert_no_queries do
      assert_equal [],    Developer.none.pluck(:id, :name)
      assert_equal 0,     Developer.none.delete_all
      assert_equal 0,     Developer.none.update_all(name: "David")
      assert_equal 0,     Developer.none.delete(1)
      assert_equal false, Developer.none.exists?(1)
    end
  end

  def test_null_relation_content_size_methods
    assert_no_queries do
      assert_equal 0,     Developer.none.size
      assert_equal 0,     Developer.none.count
      assert_equal true,  Developer.none.empty?
      assert_equal true,  Developer.none.none?
      assert_equal false, Developer.none.any?
      assert_equal false, Developer.none.one?
      assert_equal false, Developer.none.many?
    end
  end

  def test_null_relation_used_with_constraints
    post = Post.first
    assert_no_queries do
      scope = post.comments
      none = Post.none
      scope = scope.merge(none)
      assert_equal 0, scope.size
    end
  end

  def test_null_relation_metadata_methods
    assert_includes Developer.none.to_sql, " WHERE (1=0)"
    assert_equal({}, Developer.none.where_values_hash)
  end

  def test_null_relation_where_values_hash
    assert_equal({ "salary" => 100_000 }, Developer.none.where(salary: 100_000).where_values_hash)
  end

  [:count, :sum].each do |method|
    define_method "test_null_relation_#{method}" do
      assert_no_queries do
        assert_equal 0, Comment.none.public_send(method, :id)
        assert_equal Hash.new, Comment.none.group(:post_id).public_send(method, :id)
      end
    end

    define_method "test_null_relation_#{method}_async" do
      assert_no_queries do
        assert_async_equal 0, Comment.none.public_send("async_#{method}", :id)
        assert_async_equal Hash.new, Comment.none.group(:post_id).public_send("async_#{method}", :id)
      end
    end
  end

  [:average, :minimum, :maximum].each do |method|
    define_method "test_null_relation_#{method}" do
      assert_no_queries do
        assert_nil Comment.none.public_send(method, :id)
        assert_equal Hash.new, Comment.none.group(:post_id).public_send(method, :id)
      end
    end

    define_method "test_null_relation_#{method}_async" do
      assert_no_queries do
        assert_async_equal nil, Comment.none.public_send("async_#{method}", :id)
        assert_async_equal Hash.new, Comment.none.group(:post_id).public_send("async_#{method}", :id)
      end
    end
  end

  def test_null_relation_in_where_condition
    assert_operator Comment.count, :>, 0 # precondition, make sure there are comments.
    assert_equal 0, Comment.where(post_id: Post.none).count
  end
end