about summary refs log tree commit diff
path: root/spec
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2018-05-30 02:50:23 +0200
committerGitHub <noreply@github.com>2018-05-30 02:50:23 +0200
commita7d726c3836a87006cedcdc4bd186f8aff89d093 (patch)
treed1a2e321c043d61ad66f0da3f0e732b0ae75938d /spec
parent461542784b555237316f3dd5e32ea224cd3ab8ef (diff)
Improve counter caches on Status and Account (#7644)
Do not touch statuses_count on accounts table when mass-destroying
statuses to reduce load when removing accounts, same for
reblogs_count and favourites_count

Do not count statuses with direct visibility in statuses_count

Fix #828
Diffstat (limited to 'spec')
-rw-r--r--spec/models/account_spec.rb31
-rw-r--r--spec/models/status_spec.rb14
2 files changed, 45 insertions, 0 deletions
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index 3aaaa55eb..cce659a8a 100644
--- a/spec/models/account_spec.rb
+++ b/spec/models/account_spec.rb
@@ -525,6 +525,37 @@ RSpec.describe Account, type: :model do
     end
   end
 
+  describe '#statuses_count' do
+    subject { Fabricate(:account) }
+
+    it 'counts statuses' do
+      Fabricate(:status, account: subject)
+      Fabricate(:status, account: subject)
+      expect(subject.statuses_count).to eq 2
+    end
+
+    it 'does not count direct statuses' do
+      Fabricate(:status, account: subject, visibility: :direct)
+      expect(subject.statuses_count).to eq 0
+    end
+
+    it 'is decremented when status is removed' do
+      status = Fabricate(:status, account: subject)
+      expect(subject.statuses_count).to eq 1
+      status.destroy
+      expect(subject.statuses_count).to eq 0
+    end
+
+    it 'is decremented when status is removed when account is not preloaded' do
+      status = Fabricate(:status, account: subject)
+      expect(subject.reload.statuses_count).to eq 1
+      clean_status = Status.find(status.id)
+      expect(clean_status.association(:account).loaded?).to be false
+      clean_status.destroy
+      expect(subject.reload.statuses_count).to eq 0
+    end
+  end
+
   describe '.following_map' do
     it 'returns an hash' do
       expect(Account.following_map([], 1)).to be_a Hash
diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb
index aee4f49b4..5113b652f 100644
--- a/spec/models/status_spec.rb
+++ b/spec/models/status_spec.rb
@@ -175,6 +175,13 @@ RSpec.describe Status, type: :model do
 
       expect(subject.reblogs_count).to eq 2
     end
+
+    it 'is decremented when reblog is removed' do
+      reblog = Fabricate(:status, account: bob, reblog: subject)
+      expect(subject.reblogs_count).to eq 1
+      reblog.destroy
+      expect(subject.reblogs_count).to eq 0
+    end
   end
 
   describe '#favourites_count' do
@@ -184,6 +191,13 @@ RSpec.describe Status, type: :model do
 
       expect(subject.favourites_count).to eq 2
     end
+
+    it 'is decremented when favourite is removed' do
+      favourite = Fabricate(:favourite, account: bob, status: subject)
+      expect(subject.favourites_count).to eq 1
+      favourite.destroy
+      expect(subject.favourites_count).to eq 0
+    end
   end
 
   describe '#proper' do