Refactor and enhance tests for Domain::Fa and Domain::E621
- Updated factories for Domain::Fa::User and Domain::E621::Post to improve test data creation. - Refactored job specs for Domain::E621 to streamline HTTP client mocking and improve clarity. - Enhanced tests for Domain::Fa::HomePageJob to ensure proper job enqueuing and user interactions. - Removed obsolete test files related to Domain::Fa::PostEnqueuer and Domain::Fa::UserEnqueuer to clean up the test suite. - Improved validation tests for Domain::Fa::Post and Domain::Fa::User models to ensure robustness.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
FactoryBot.define do
|
||||
factory :domain_fa_user, class: "Domain::Fa::User" do
|
||||
sequence(:url_name) { |n| "user#{n}" }
|
||||
sequence(:name) { |n| "User #{n}" }
|
||||
url_name { Domain::Fa::User.name_to_url_name(name) }
|
||||
state { :ok }
|
||||
state_detail { {} }
|
||||
log_entry_detail { {} }
|
||||
|
||||
@@ -2,105 +2,38 @@ require "rails_helper"
|
||||
|
||||
describe Domain::E621::Job::PostsIndexJob do
|
||||
let(:http_client_mock) { instance_double("::Scraper::HttpClient") }
|
||||
before do
|
||||
Scraper::ClientFactory.http_client_mock = http_client_mock
|
||||
SpecUtil.init_http_client_mock(
|
||||
http_client_mock,
|
||||
[
|
||||
{
|
||||
uri: "https://e621.net/posts.json",
|
||||
status_code: 200,
|
||||
content_type: "text/html",
|
||||
contents:
|
||||
SpecUtil.read_fixture_file("domain/e621/job/posts_index_1.json"),
|
||||
caused_by_entry_idx: nil,
|
||||
},
|
||||
],
|
||||
before { Scraper::ClientFactory.http_client_mock = http_client_mock }
|
||||
|
||||
it "works" do
|
||||
file = create(:http_log_entry)
|
||||
mock_log_entries =
|
||||
SpecUtil.init_http_client_mock(
|
||||
http_client_mock,
|
||||
[
|
||||
{
|
||||
uri: "https://e621.net/posts.json",
|
||||
status_code: 200,
|
||||
content_type: "application/json; charset=utf-8",
|
||||
contents:
|
||||
SpecUtil.read_fixture_file("domain/e621/job/posts_index_1.json"),
|
||||
caused_by_entry: file,
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
described_class.perform_now({ caused_by_entry: file })
|
||||
|
||||
expect(Domain::E621::Post.count).to eq(5)
|
||||
post = Domain::E621::Post.find_by(e621_id: 4_247_443)
|
||||
expect(post.file_url_str).to eq(
|
||||
"https://static1.e621.net/data/1c/61/1c6169aa51668681e9697a48144d7c78.jpg",
|
||||
)
|
||||
expect(post.md5).to eq("1c6169aa51668681e9697a48144d7c78")
|
||||
expect(post.tags_array).to match(
|
||||
hash_including(
|
||||
"general" => array_including("alcohol", "beach_ball", "wide_hips"),
|
||||
"species" => array_including("mammal", "procyonid", "raccoon"),
|
||||
),
|
||||
)
|
||||
end
|
||||
|
||||
describe "#perform" do
|
||||
it "creates new posts" do
|
||||
expect { perform_now({}) }.to change(Domain::E621::Post, :count).by(5)
|
||||
end
|
||||
|
||||
it "updates existing posts" do
|
||||
post =
|
||||
Domain::E621::Post.create!(
|
||||
{
|
||||
e621_id: 4_247_443,
|
||||
md5: "1c6169aa51668681e9697a48144d7c78",
|
||||
tags_array: ["some_tag"],
|
||||
},
|
||||
)
|
||||
expect(post.tags_array).to eq({ "general" => ["some_tag"] })
|
||||
expect { perform_now({}) }.to change(Domain::E621::Post, :count).by(4)
|
||||
post.reload
|
||||
expect(post.file_url_str).to eq(
|
||||
"https://static1.e621.net/data/1c/61/1c6169aa51668681e9697a48144d7c78.jpg",
|
||||
)
|
||||
expect(post.tags_array).to match(
|
||||
hash_including(
|
||||
"general" => array_including("alcohol", "beach_ball", "wide_hips"),
|
||||
"species" => array_including("mammal", "procyonid", "raccoon"),
|
||||
),
|
||||
)
|
||||
expect(post.tags_array.values.flatten).not_to include("some_tag")
|
||||
end
|
||||
|
||||
# it "fixes tags to reflect reality" do
|
||||
# post =
|
||||
# Domain::E621::Post.create!(
|
||||
# { e621_id: 4_247_443, md5: "1c6169aa51668681e9697a48144d7c78" },
|
||||
# )
|
||||
# tag1 = Domain::E621::Tag.create!(name: "tag1")
|
||||
# tag2 = Domain::E621::Tag.create!(name: "mammal")
|
||||
# post.taggings.create!(tag: tag1, category: "general")
|
||||
# post.taggings.create!(tag: tag2, category: "general")
|
||||
# post.save!
|
||||
|
||||
# perform_now({})
|
||||
# expect(
|
||||
# SpecUtil.enqueued_jobs(Domain::E621::Job::StaticFileJob).length,
|
||||
# ).to eq(4)
|
||||
|
||||
# post.reload
|
||||
# # removes the tag1 tag
|
||||
# expect(post.tags.map(&:name)).not_to include("tag1")
|
||||
|
||||
# # keeps tags that are in the json
|
||||
# expect(post.tags.map(&:name)).to include("absurd_res")
|
||||
# expect(post.tags.map(&:name)).to include("mammal")
|
||||
|
||||
# # changes existing mammal/general tag to mammal/species
|
||||
# expect(
|
||||
# post.taggings.find { |tagging| tagging.tag.name == "mammal" }.category,
|
||||
# ).to eq("species")
|
||||
# end
|
||||
|
||||
it "updates a post where the md5 has changed" do
|
||||
file = SpecUtil.create_http_log_entry
|
||||
post =
|
||||
Domain::E621::Post.create!(
|
||||
{
|
||||
e621_id: 4_247_443,
|
||||
md5: "0000000051668681e9697a48144d7c78",
|
||||
file: file,
|
||||
},
|
||||
)
|
||||
perform_now({})
|
||||
post.reload
|
||||
expect(post.md5).to eq("1c6169aa51668681e9697a48144d7c78")
|
||||
expect(post.file).to be_nil
|
||||
expect(post.state_detail["prev_md5s"]).to eq(
|
||||
["md5" => "0000000051668681e9697a48144d7c78", "file_id" => file.id],
|
||||
)
|
||||
static_file_jobs =
|
||||
SpecUtil.enqueued_jobs(Domain::E621::Job::StaticFileJob)
|
||||
expect(static_file_jobs.length).to eq(5)
|
||||
expect(static_file_jobs).to match(
|
||||
including(including(args: [including(post: post)])),
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,8 +5,8 @@ describe Domain::E621::Job::ScanPostJob do
|
||||
before { Scraper::ClientFactory.http_client_mock = http_client_mock }
|
||||
|
||||
it "scans the post" do
|
||||
post = SpecUtil.create_e621_post(e621_id: 2_227_914)
|
||||
caused_by_entry = SpecUtil.create_http_log_entry
|
||||
post = create(:domain_e621_post, e621_id: 2_227_914)
|
||||
caused_by_entry = create(:http_log_entry)
|
||||
mock_log_entries =
|
||||
SpecUtil.init_http_client_mock(
|
||||
http_client_mock,
|
||||
@@ -46,7 +46,7 @@ describe Domain::E621::Job::ScanPostJob do
|
||||
end
|
||||
|
||||
it "handles a post with no file url" do
|
||||
post = SpecUtil.create_e621_post(e621_id: 5_270_136)
|
||||
post = create(:domain_e621_post, e621_id: 5_270_136)
|
||||
mock_log_entries =
|
||||
SpecUtil.init_http_client_mock(
|
||||
http_client_mock,
|
||||
|
||||
@@ -1,169 +1,69 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Domain::E621::Job::StaticFileJob do
|
||||
AN_IMAGE_SHA256 =
|
||||
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
||||
AN_IMAGE_MD5 = "d41d8cd98f00b204e9800998ecf8427e"
|
||||
AN_IMAGE_PATH = "domain/e621/job/an-image.png"
|
||||
|
||||
let(:http_client_mock) { instance_double("::Scraper::HttpClient") }
|
||||
before { Scraper::ClientFactory.http_client_mock = http_client_mock }
|
||||
|
||||
describe "#perform" do
|
||||
it "enqueues a post scan job if it doesn't have a file url" do
|
||||
hle = SpecUtil.create_http_log_entry
|
||||
post = Domain::E621::Post.create!({ e621_id: 12_345, md5: AN_IMAGE_MD5 })
|
||||
perform_now({ post: post, caused_by_entry: hle })
|
||||
expect(SpecUtil.enqueued_jobs(Domain::E621::Job::ScanPostJob)).to match(
|
||||
[including(args: [{ post: post, caused_by_entry: hle }])]
|
||||
it "downloads the file" do
|
||||
post =
|
||||
create(
|
||||
:domain_e621_post,
|
||||
file_url_str:
|
||||
"https://static1.e621.net/data/c0/fa/c0fa5293f1d1440c2d3f2c3e027d3c36.jpg",
|
||||
)
|
||||
end
|
||||
|
||||
it "downloads the file if file_url_str is present" do
|
||||
hle = SpecUtil.create_http_log_entry
|
||||
hle = create(:http_log_entry)
|
||||
mock_log_entries =
|
||||
SpecUtil.init_http_client_mock(
|
||||
http_client_mock,
|
||||
[
|
||||
{
|
||||
uri: "https://static1.e621.net/file/foo.png",
|
||||
uri:
|
||||
"https://static1.e621.net/data/c0/fa/c0fa5293f1d1440c2d3f2c3e027d3c36.jpg",
|
||||
status_code: 200,
|
||||
content_type: "image/png",
|
||||
contents: SpecUtil.read_fixture_file(AN_IMAGE_PATH),
|
||||
caused_by_entry: hle
|
||||
}
|
||||
]
|
||||
content_type: "image/jpeg",
|
||||
contents: "test",
|
||||
caused_by_entry: hle,
|
||||
},
|
||||
],
|
||||
)
|
||||
post =
|
||||
Domain::E621::Post.create!(
|
||||
{
|
||||
e621_id: 12_345,
|
||||
md5: AN_IMAGE_MD5,
|
||||
file_url_str: "https://static1.e621.net/file/foo.png"
|
||||
}
|
||||
)
|
||||
perform_now({ post: post, caused_by_entry: hle })
|
||||
|
||||
post.reload
|
||||
expect(post.file).not_to be_nil
|
||||
expect(post.file.response.sha256_hex).to eq(AN_IMAGE_SHA256)
|
||||
end
|
||||
described_class.perform_now({ post: post, caused_by_entry: hle })
|
||||
post.reload
|
||||
expect(post.file).to eq(mock_log_entries[0])
|
||||
end
|
||||
|
||||
it "marks the post as errored if the download fails" do
|
||||
hles =
|
||||
SpecUtil.init_http_client_mock(
|
||||
http_client_mock,
|
||||
[
|
||||
{
|
||||
uri: "https://static1.e621.net/file/foo.png",
|
||||
status_code: 404,
|
||||
content_type: "text/html",
|
||||
contents: "not found"
|
||||
}
|
||||
]
|
||||
)
|
||||
post =
|
||||
Domain::E621::Post.create!(
|
||||
{
|
||||
e621_id: 12_345,
|
||||
md5: AN_IMAGE_MD5,
|
||||
file_url_str: "https://static1.e621.net/file/foo.png"
|
||||
}
|
||||
)
|
||||
perform_now({ post: post })
|
||||
|
||||
post.reload
|
||||
expect(post.state).to eq("file_error")
|
||||
expect(post.file).to be_nil
|
||||
expect(post.state_detail["file_error"]).to eq(
|
||||
{
|
||||
"status_code" => 404,
|
||||
"log_entry_id" => hles[0].id,
|
||||
"retry_count" => 1
|
||||
}
|
||||
it "handles a 404" do
|
||||
post =
|
||||
create(
|
||||
:domain_e621_post,
|
||||
file_url_str:
|
||||
"https://static1.e621.net/data/c0/fa/c0fa5293f1d1440c2d3f2c3e027d3c36.jpg",
|
||||
)
|
||||
end
|
||||
|
||||
it "recovers from a failed download" do
|
||||
hles =
|
||||
SpecUtil.init_http_client_mock(
|
||||
http_client_mock,
|
||||
[
|
||||
{
|
||||
uri: "https://static1.e621.net/file/foo.png",
|
||||
status_code: 500,
|
||||
content_type: "text/html",
|
||||
contents: "not found"
|
||||
},
|
||||
{
|
||||
uri: "https://static1.e621.net/file/foo.png",
|
||||
status_code: 200,
|
||||
content_type: "image/png",
|
||||
contents: SpecUtil.read_fixture_file(AN_IMAGE_PATH)
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
post =
|
||||
Domain::E621::Post.create!(
|
||||
hle = create(:http_log_entry)
|
||||
mock_log_entries =
|
||||
SpecUtil.init_http_client_mock(
|
||||
http_client_mock,
|
||||
[
|
||||
{
|
||||
e621_id: 12_345,
|
||||
md5: AN_IMAGE_MD5,
|
||||
file_url_str: "https://static1.e621.net/file/foo.png"
|
||||
}
|
||||
)
|
||||
uri:
|
||||
"https://static1.e621.net/data/c0/fa/c0fa5293f1d1440c2d3f2c3e027d3c36.jpg",
|
||||
status_code: 404,
|
||||
content_type: "text/html",
|
||||
contents: "test",
|
||||
caused_by_entry: hle,
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
perform_now({ post: post }, should_raise: true)
|
||||
post.reload
|
||||
expect(post.file).to be_nil
|
||||
|
||||
perform_now({ post: post })
|
||||
post.reload
|
||||
expect(post.file).not_to be_nil
|
||||
expect(post.file.response.sha256_hex).to eq(AN_IMAGE_SHA256)
|
||||
end
|
||||
|
||||
it "throws on a non-404 error in order to retry later" do
|
||||
num_retries = 3
|
||||
hles =
|
||||
SpecUtil.init_http_client_mock(
|
||||
http_client_mock,
|
||||
[
|
||||
{
|
||||
uri: "https://static1.e621.net/file/foo.png",
|
||||
status_code: 500,
|
||||
content_type: "text/html",
|
||||
contents: "not found"
|
||||
}
|
||||
] * num_retries
|
||||
)
|
||||
post =
|
||||
Domain::E621::Post.create!(
|
||||
{
|
||||
e621_id: 12_345,
|
||||
md5: AN_IMAGE_MD5,
|
||||
file_url_str: "https://static1.e621.net/file/foo.png"
|
||||
}
|
||||
)
|
||||
|
||||
num_retries.times.map do |retry_num|
|
||||
perform_now({ post: post }, should_raise: true)
|
||||
post.reload
|
||||
expect(post.state).to eq("file_error")
|
||||
expect(post.file).to be_nil
|
||||
expect(post.state_detail["file_error"]).to eq(
|
||||
{
|
||||
"status_code" => 500,
|
||||
"log_entry_id" => hles[retry_num].id,
|
||||
"retry_count" => retry_num + 1
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
# the last retry should not throw, but simply bail out early
|
||||
perform_now({ post: post })
|
||||
post.reload
|
||||
expect(post.state).to eq("file_error")
|
||||
expect(post.file).to be_nil
|
||||
end
|
||||
described_class.perform_now({ post: post, caused_by_entry: hle })
|
||||
post.reload
|
||||
expect(post.state).to eq("file_error")
|
||||
expect(post.state_detail["file_error"]).to eq(
|
||||
{
|
||||
"status_code" => 404,
|
||||
"log_entry_id" => mock_log_entries[0].id,
|
||||
"retry_count" => 1,
|
||||
},
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,7 +20,7 @@ describe Domain::Fa::Job::HomePageJob do
|
||||
before do
|
||||
creator =
|
||||
Domain::Fa::User.create!(
|
||||
{ name: "LemontasticTobster", url_name: "lemontastictobster" }
|
||||
{ name: "LemontasticTobster", url_name: "lemontastictobster" },
|
||||
)
|
||||
Domain::Fa::Post.create!({ fa_id: 52_807_274, creator: creator })
|
||||
end
|
||||
@@ -32,9 +32,9 @@ describe Domain::Fa::Job::HomePageJob do
|
||||
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::ScanPostJob)).to match(
|
||||
[
|
||||
including(
|
||||
args: [{ post: post.call, caused_by_entry: log_entries[0] }]
|
||||
)
|
||||
]
|
||||
args: [{ post: post.call, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
],
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -52,9 +52,9 @@ describe Domain::Fa::Job::HomePageJob do
|
||||
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::ScanFileJob)).to match(
|
||||
[
|
||||
including(
|
||||
args: [{ post: post.call, caused_by_entry: log_entries[0] }]
|
||||
)
|
||||
]
|
||||
args: [{ post: post.call, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
],
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -72,9 +72,9 @@ describe Domain::Fa::Job::HomePageJob do
|
||||
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::UserPageJob)).to match(
|
||||
[
|
||||
including(
|
||||
args: [{ user: user.call, caused_by_entry: log_entries[0] }]
|
||||
)
|
||||
]
|
||||
args: [{ user: user.call, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
],
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -90,13 +90,13 @@ describe Domain::Fa::Job::HomePageJob do
|
||||
if expect_to_enqueue
|
||||
it "enqueues user gallery job" do
|
||||
expect(
|
||||
SpecUtil.enqueued_jobs(Domain::Fa::Job::UserGalleryJob)
|
||||
SpecUtil.enqueued_jobs(Domain::Fa::Job::UserGalleryJob),
|
||||
).to match(
|
||||
[
|
||||
including(
|
||||
args: [{ user: user.call, caused_by_entry: log_entries[0] }]
|
||||
)
|
||||
]
|
||||
args: [{ user: user.call, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
],
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -104,7 +104,7 @@ describe Domain::Fa::Job::HomePageJob do
|
||||
unless expect_to_enqueue
|
||||
it "does not enqueue user gallery job" do
|
||||
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::UserGalleryJob)).to eq(
|
||||
[]
|
||||
[],
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -145,23 +145,23 @@ describe Domain::Fa::Job::HomePageJob do
|
||||
content_type: "text/html",
|
||||
contents:
|
||||
SpecUtil.read_fixture_file("domain/fa/job/home_page.html"),
|
||||
caused_by_entry_idx: nil
|
||||
}
|
||||
]
|
||||
caused_by_entry_idx: nil,
|
||||
},
|
||||
],
|
||||
)
|
||||
end
|
||||
|
||||
it "creates a new post" do
|
||||
expect do perform_now(default_args) end.to change(
|
||||
Domain::Fa::Post,
|
||||
:count
|
||||
:count,
|
||||
).by(1)
|
||||
end
|
||||
|
||||
it "creates a new user" do
|
||||
expect do perform_now(default_args) end.to change(
|
||||
Domain::Fa::User,
|
||||
:count
|
||||
:count,
|
||||
).by(1)
|
||||
end
|
||||
|
||||
@@ -180,49 +180,49 @@ describe Domain::Fa::Job::HomePageJob do
|
||||
it "enqueues post scan job" do
|
||||
perform_now(default_args)
|
||||
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::ScanPostJob).length).to eq(
|
||||
5
|
||||
5,
|
||||
)
|
||||
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::ScanPostJob)).to match(
|
||||
[
|
||||
including(args: [{ post: post, caused_by_entry: log_entries[0] }]),
|
||||
including(
|
||||
args: [{ fa_id: 52_807_273, caused_by_entry: log_entries[0] }]
|
||||
args: [{ fa_id: 52_807_273, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
including(
|
||||
args: [{ fa_id: 52_807_272, caused_by_entry: log_entries[0] }]
|
||||
args: [{ fa_id: 52_807_272, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
including(
|
||||
args: [{ fa_id: 52_807_271, caused_by_entry: log_entries[0] }]
|
||||
args: [{ fa_id: 52_807_271, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
including(
|
||||
args: [{ fa_id: 52_807_270, caused_by_entry: log_entries[0] }]
|
||||
)
|
||||
]
|
||||
args: [{ fa_id: 52_807_270, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
],
|
||||
)
|
||||
end
|
||||
|
||||
it "does not enqueue if a post already exists" do
|
||||
Domain::Fa::Post.create!(
|
||||
{ fa_id: 52_807_272, creator: SpecUtil.create_domain_fa_user }
|
||||
{ fa_id: 52_807_272, creator: create(:domain_fa_user) },
|
||||
)
|
||||
perform_now(default_args)
|
||||
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::ScanPostJob).length).to eq(
|
||||
4
|
||||
4,
|
||||
)
|
||||
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::ScanPostJob)).to match(
|
||||
[
|
||||
including(args: [{ post: post, caused_by_entry: log_entries[0] }]),
|
||||
including(
|
||||
args: [{ fa_id: 52_807_273, caused_by_entry: log_entries[0] }]
|
||||
args: [{ fa_id: 52_807_273, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
# note that 52807272 not enqueued
|
||||
including(
|
||||
args: [{ fa_id: 52_807_271, caused_by_entry: log_entries[0] }]
|
||||
args: [{ fa_id: 52_807_271, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
including(
|
||||
args: [{ fa_id: 52_807_270, caused_by_entry: log_entries[0] }]
|
||||
)
|
||||
]
|
||||
args: [{ fa_id: 52_807_270, caused_by_entry: log_entries[0] }],
|
||||
),
|
||||
],
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Domain::Fa::PostEnqueuer do
|
||||
let(:creator) { SpecUtil.create_domain_fa_user }
|
||||
let(:job_klasses) do
|
||||
[Domain::Fa::Job::ScanPostJob, Domain::Fa::Job::ScanFileJob]
|
||||
end
|
||||
let(:enqueued_fa_ids) do
|
||||
proc do
|
||||
SpecUtil.enqueued_jobs(job_klasses).map { |job| job[:args][0][:fa_id] }
|
||||
end
|
||||
end
|
||||
let(:enqueued_fa_jobs) do
|
||||
proc { SpecUtil.enqueued_jobs(job_klasses).map { |job| job[:job] } }
|
||||
end
|
||||
context "forward scanning posts missing file" do
|
||||
let!(:posts) do
|
||||
no_file_url =
|
||||
4.times.map { SpecUtil.create_domain_fa_post(creator: creator) }
|
||||
with_file_url =
|
||||
3.times.map do
|
||||
SpecUtil
|
||||
.build_domain_fa_post(creator: creator)
|
||||
.tap do |p|
|
||||
p.file_url_str = "https://www.example.com/img.jpg"
|
||||
p.save!
|
||||
end
|
||||
end
|
||||
no_file_url + with_file_url
|
||||
end
|
||||
|
||||
let(:enqueuer) do
|
||||
Domain::Fa::PostEnqueuer.new(
|
||||
reverse_scan_holes: false,
|
||||
start_at: 0,
|
||||
high_water_mark: 5,
|
||||
low_water_mark: 3
|
||||
)
|
||||
end
|
||||
|
||||
it "enqueues posts" do
|
||||
post_fa_ids = posts.map(&:fa_id)
|
||||
|
||||
enqueuer.run_once
|
||||
expect(enqueued_fa_ids.call).to contain_exactly(*post_fa_ids[0...5])
|
||||
expect(enqueued_fa_jobs.call).to contain_exactly(
|
||||
*(
|
||||
[Domain::Fa::Job::ScanPostJob] * 4 +
|
||||
[Domain::Fa::Job::ScanFileJob] * 1
|
||||
)
|
||||
)
|
||||
SpecUtil.shift_jobs
|
||||
|
||||
# jobs should have concurrency keys
|
||||
expect(
|
||||
SpecUtil.enqueued_jobs(job_klasses).first[:good_job].concurrency_key
|
||||
).to_not be_nil
|
||||
|
||||
enqueuer.run_once
|
||||
expect(enqueued_fa_ids.call).to contain_exactly(*post_fa_ids[1...5])
|
||||
expect(enqueued_fa_jobs.call).to contain_exactly(
|
||||
*(
|
||||
[Domain::Fa::Job::ScanPostJob] * 3 +
|
||||
[Domain::Fa::Job::ScanFileJob] * 1
|
||||
)
|
||||
)
|
||||
SpecUtil.shift_jobs
|
||||
|
||||
enqueuer.run_once
|
||||
expect(enqueued_fa_ids.call).to contain_exactly(*post_fa_ids[2...7])
|
||||
expect(enqueued_fa_jobs.call).to contain_exactly(
|
||||
*(
|
||||
[Domain::Fa::Job::ScanPostJob] * 2 +
|
||||
[Domain::Fa::Job::ScanFileJob] * 3
|
||||
)
|
||||
)
|
||||
|
||||
SpecUtil.shift_jobs(job_klasses, 3)
|
||||
|
||||
expect { enqueuer.run_once }.to raise_exception(StopIteration)
|
||||
expect(enqueued_fa_ids.call).to contain_exactly(*post_fa_ids[5...7])
|
||||
end
|
||||
|
||||
it "does not enqueue posts which are already in the queue" do
|
||||
post_fa_ids = posts.map(&:fa_id)
|
||||
Domain::Fa::Job::ScanPostJob.perform_later({ fa_id: post_fa_ids[1] })
|
||||
expect(enqueued_fa_ids.call).to contain_exactly(post_fa_ids[1])
|
||||
|
||||
# post [1] should be filtered out
|
||||
enqueuer.run_once
|
||||
expect(enqueued_fa_ids.call[0]).to eq(post_fa_ids[1])
|
||||
expect(enqueued_fa_ids.call[1..].shuffle).to contain_exactly(
|
||||
post_fa_ids[0],
|
||||
post_fa_ids[2],
|
||||
post_fa_ids[3]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "reverse enqueues missing models" do
|
||||
let!(:post1) { SpecUtil.create_domain_fa_post(creator: creator, fa_id: 3) }
|
||||
|
||||
let(:enqueuer) do
|
||||
Domain::Fa::PostEnqueuer.new(
|
||||
reverse_scan_holes: true,
|
||||
start_at: 7,
|
||||
high_water_mark: 5,
|
||||
low_water_mark: 2
|
||||
)
|
||||
end
|
||||
|
||||
it "works" do
|
||||
enqueuer.run_once
|
||||
expect(enqueued_fa_ids.call).to eq([7, 6, 5, 4, 2])
|
||||
SpecUtil.shift_jobs
|
||||
|
||||
enqueuer.run_once
|
||||
expect(enqueued_fa_ids.call).to eq([6, 5, 4, 2])
|
||||
SpecUtil.shift_jobs
|
||||
|
||||
enqueuer.run_once
|
||||
expect(enqueued_fa_ids.call).to eq([5, 4, 2])
|
||||
SpecUtil.shift_jobs
|
||||
|
||||
enqueuer.run_once
|
||||
expect(enqueued_fa_ids.call).to eq([4, 2, 1])
|
||||
SpecUtil.shift_jobs
|
||||
|
||||
expect { enqueuer.run_once }.to raise_exception(StopIteration)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,40 +0,0 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Domain::Fa::UserEnqueuer do
|
||||
it "works" do
|
||||
users = 7.times.map { SpecUtil.create_domain_fa_user }
|
||||
|
||||
enqueuer =
|
||||
Domain::Fa::UserEnqueuer.new(
|
||||
start_at: 0,
|
||||
high_water_mark: 5,
|
||||
low_water_mark: 3
|
||||
)
|
||||
|
||||
get_enqueued_users =
|
||||
proc do
|
||||
SpecUtil
|
||||
.enqueued_jobs(Domain::Fa::Job::UserIncrementalJob)
|
||||
.map { |job| job[:args][0][:user] }
|
||||
end
|
||||
|
||||
enqueuer.run_once
|
||||
# expect(get_enqueued_users.call.length).to eq(5)
|
||||
# expect(get_enqueued_users.call).to eq(users[0...5])
|
||||
# expect(SpecUtil.enqueued_jobs.length).to eq(15)
|
||||
# SpecUtil.shift_jobs(Domain::Fa::Job::UserIncrementalJob)
|
||||
|
||||
# enqueuer.run_once
|
||||
# expect(get_enqueued_users.call).to eq(users[1...5])
|
||||
# SpecUtil.shift_jobs(Domain::Fa::Job::UserIncrementalJob)
|
||||
|
||||
# enqueuer.run_once
|
||||
# expect(get_enqueued_users.call).to eq(users[2...7])
|
||||
# SpecUtil.shift_jobs(Domain::Fa::Job::UserIncrementalJob, 3)
|
||||
|
||||
# expect do
|
||||
# enqueuer.run_once
|
||||
# end.to raise_exception(StopIteration)
|
||||
# expect(get_enqueued_users.call).to eq(users[5...7])
|
||||
end
|
||||
end
|
||||
@@ -1,43 +0,0 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Domain::Fa::UserFactorCalculator do
|
||||
before do
|
||||
create_follows =
|
||||
proc do |follower, users|
|
||||
users.each do |user|
|
||||
Domain::Fa::Follow.create!(follower: follower, followed: user)
|
||||
end
|
||||
end
|
||||
|
||||
@cluster1 =
|
||||
10.times.map do |i|
|
||||
SpecUtil.create_domain_fa_user(name: "cluster-1-#{i}")
|
||||
end
|
||||
@cluster2 =
|
||||
10.times.map do |i|
|
||||
SpecUtil.create_domain_fa_user(name: "cluster-2-#{i}")
|
||||
end
|
||||
@follower1, @follower2, @follower3 =
|
||||
3.times.map do |i|
|
||||
SpecUtil.create_domain_fa_user(name: "follower-#{i + 1}")
|
||||
end
|
||||
|
||||
create_follows.call(@follower1, @cluster1)
|
||||
create_follows.call(@follower2, @cluster2)
|
||||
create_follows.call(@follower3, @cluster1[0...5] + @cluster2[0...5])
|
||||
end
|
||||
|
||||
it "works" do
|
||||
worker = Domain::Fa::UserFactorCalculator.new
|
||||
worker.fit
|
||||
worker.write_factors
|
||||
|
||||
c1user = @cluster1.first
|
||||
|
||||
# should be able to query similar users
|
||||
nearest = c1user.similar_users_by_followed.limit(5)
|
||||
expect(@cluster1).to include(nearest[0])
|
||||
expect(nearest).to_not include(c1user)
|
||||
nearest.each { |user| expect(user.neighbor_distance).to be >= 0.0 }
|
||||
end
|
||||
end
|
||||
@@ -26,6 +26,11 @@ RSpec.describe Domain::Fa::Post do
|
||||
expect { post.state = state }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
it "can be built with created_at" do
|
||||
post = build(:domain_fa_post, created_at: 1.day.ago)
|
||||
expect(post).to be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe "associations" do
|
||||
@@ -153,7 +158,7 @@ RSpec.describe Domain::Fa::Post do
|
||||
end
|
||||
|
||||
it "ensures indexed_post is created" do
|
||||
post = SpecUtil.build_domain_fa_post(created_at: 1.day.ago)
|
||||
post = build(:domain_fa_post, created_at: 1.day.ago)
|
||||
post.save!
|
||||
expect(post.indexed_post).to be_present
|
||||
expect(post.indexed_post.created_at).to eq(post.created_at)
|
||||
|
||||
@@ -7,7 +7,7 @@ describe Domain::Fa::User do
|
||||
end
|
||||
|
||||
it "user can be destroyed" do
|
||||
user = SpecUtil.create_domain_fa_user(name: "Foo", url_name: "foo")
|
||||
user = create(:domain_fa_user, name: "Foo", url_name: "foo")
|
||||
user.ensure_avatar!
|
||||
user.destroy
|
||||
end
|
||||
@@ -72,9 +72,9 @@ describe Domain::Fa::User do
|
||||
end
|
||||
|
||||
it "posts can be moved from one user to another" do
|
||||
user1 = SpecUtil.create_domain_fa_user(name: "Foo", url_name: "foo")
|
||||
user2 = SpecUtil.create_domain_fa_user(name: "Bar", url_name: "bar")
|
||||
post1 = SpecUtil.create_domain_fa_post(creator: user1)
|
||||
user1 = create(:domain_fa_user, name: "Foo", url_name: "foo")
|
||||
user2 = create(:domain_fa_user, name: "Bar", url_name: "bar")
|
||||
post1 = create(:domain_fa_post, creator: user1)
|
||||
|
||||
# should not be able to destroy when there are posts associated with the user
|
||||
expect(user1.destroy).to be_falsey
|
||||
@@ -94,7 +94,7 @@ describe Domain::Fa::User do
|
||||
end
|
||||
|
||||
it "can deal with scanned_at values in state_detail" do
|
||||
user = SpecUtil.create_domain_fa_user(name: "Foo", url_name: "foo")
|
||||
user = create(:domain_fa_user, name: "Foo", url_name: "foo")
|
||||
|
||||
expect(user.scanned_incremental_at).to be_nil
|
||||
expect(user.due_for_incremental_scan?).to be_truthy
|
||||
|
||||
Reference in New Issue
Block a user