2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe DraftSequence do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2024-07-10 15:43:11 +08:00
|
|
|
fab!(:upload)
|
2017-09-15 16:44:04 +08:00
|
|
|
|
2020-05-12 13:56:24 +08:00
|
|
|
describe ".next" do
|
|
|
|
it "should produce next sequence for a key" do
|
|
|
|
expect(DraftSequence.next!(user, "test")).to eq 1
|
|
|
|
expect(DraftSequence.next!(user, "test")).to eq 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not produce next sequence for non-human user" do
|
|
|
|
user.id = -99_999
|
|
|
|
2.times { expect(DraftSequence.next!(user, "test")).to eq(0) }
|
|
|
|
end
|
2021-08-04 18:30:37 +08:00
|
|
|
|
2024-07-10 15:43:11 +08:00
|
|
|
it "deletes old drafts and associated upload references" do
|
|
|
|
Draft.set(
|
|
|
|
user,
|
|
|
|
Draft::NEW_TOPIC,
|
|
|
|
0,
|
|
|
|
{
|
|
|
|
reply: "[#{upload.original_filename}|attachment](#{upload.short_url})",
|
|
|
|
action: "createTopic",
|
|
|
|
title: "New topic with an upload",
|
|
|
|
categoryId: 1,
|
|
|
|
tags: [],
|
|
|
|
archetypeId: "regular",
|
|
|
|
metaData: nil,
|
|
|
|
composerTime: 10_000,
|
|
|
|
typingTime: 10_000,
|
|
|
|
}.to_json,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect { DraftSequence.next!(user, Draft::NEW_TOPIC) }.to change { Draft.count }.by(
|
|
|
|
-1,
|
|
|
|
).and change { UploadReference.count }.by(-1).and change {
|
|
|
|
user.reload.user_stat.draft_count
|
|
|
|
}.by(-1)
|
2021-08-04 18:30:37 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-09-15 16:44:04 +08:00
|
|
|
describe ".current" do
|
|
|
|
it "should return 0 by default" do
|
|
|
|
expect(DraftSequence.current(user, "test")).to eq 0
|
|
|
|
end
|
|
|
|
|
2020-05-12 13:56:24 +08:00
|
|
|
it "should return nil for non-human user" do
|
|
|
|
user.id = -99_999
|
2020-05-15 10:39:39 +08:00
|
|
|
expect(DraftSequence.current(user, "test")).to eq(0)
|
2020-05-12 13:56:24 +08:00
|
|
|
end
|
|
|
|
|
2017-09-15 16:44:04 +08:00
|
|
|
it "should return the right sequence" do
|
|
|
|
expect(DraftSequence.next!(user, "test")).to eq(1)
|
|
|
|
expect(DraftSequence.current(user, "test")).to eq(1)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|