DEV: Add spec for summary links

DEV: Add spec for summary links

diff --git a/spec/serializers/user_summary_serializer_spec.rb b/spec/serializers/user_summary_serializer_spec.rb
index 3f82102..825bfd0 100644
--- a/spec/serializers/user_summary_serializer_spec.rb
+++ b/spec/serializers/user_summary_serializer_spec.rb
@@ -29,4 +29,27 @@ describe UserSummarySerializer do
     SiteSetting.enable_names = false
     expect(serializer.as_json[:most_liked_users][0][:name]).to eq(nil)
   end
+
+  it "returns correct links data ranking" do
+    topic = Fabricate(:topic)
+    post = Fabricate(:post_with_external_links, user: topic.user, topic: topic)
+    TopicLink.extract_from(post)
+    topic.topic_links.each_with_index do |link, index|
+      index.times do |i|
+        TopicLinkClick.create(topic_link: link, ip_address: "192.168.1.#{i + 1}")
+      end
+    end
+
+    guardian = Guardian.new
+    summary = UserSummary.new(topic.user, guardian)
+    serializer = UserSummarySerializer.new(summary, scope: guardian, root: false)
+    json = serializer.as_json
+
+    expect(json[:links][0][:url]).to eq("http://www.codinghorror.com/blog")
+    expect(json[:links][0][:clicks]).to eq(6)
+    expect(json[:links][1][:url]).to eq("http://twitter.com")
+    expect(json[:links][1][:clicks]).to eq(5)
+    expect(json[:links][2][:url]).to eq("https://google.com")
+    expect(json[:links][2][:clicks]).to eq(4)
+  end
 end

GitHub sha: 73a3e2cc

This commit appears in #10175 which was approved by eviltrout. It was merged by Falco.

@falco this spec fails consistently on macOS:

Failures:

  1) UserSummarySerializer returns correct links data ranking
     Failure/Error: expect(json[:links][0][:url]).to eq("http://www.codinghorror.com/blog")

       expected: "http://www.codinghorror.com/blog"
            got: "https://google.com"

       (compared using ==)
     # ./spec/serializers/user_summary_serializer_spec.rb:49:in `block (2 levels) in <top (required)>'

Do you mind having a look? I couldn’t quickly figure out where the order of the links is set (maybe it’s implied?).

Can you try DEV: Use explicit ordering so spec is realiable · discourse/discourse@32f6c5c · GitHub ?

2 Likes

Same result, unfortunately.

wat

How can this be possible?

Can you try to add

diff --git a/spec/serializers/user_summary_serializer_spec.rb b/spec/serializers/user_summary_serializer_spec.rb
index 08270082f4..1f635e46be 100644
--- a/spec/serializers/user_summary_serializer_spec.rb
+++ b/spec/serializers/user_summary_serializer_spec.rb
@@ -34,6 +34,9 @@ describe UserSummarySerializer do
     topic = Fabricate(:topic)
     post = Fabricate(:post_with_external_links, user: topic.user, topic: topic)
     TopicLink.extract_from(post)
+
+    pp TopicLink.where(topic_id: topic.id).order(url: :asc).pluck(:url)
+
     TopicLink.where(topic_id: topic.id).order(url: :asc).each_with_index do |link, index|
       index.times do |i|
         TopicLinkClick.create(topic_link: link, ip_address: "192.168.1.#{i + 1}")

and run it bundle exec rspec spec/serializers/user_summary_serializer_spec.rb:33 ?

I get

["http://discourse.org#a#b",
 "http://discourse.org#faq",
 "http://forumwarz.com",
 "http://google.com",
 "https://google.com",
 "http://twitter.com",
 "http://www.codinghorror.com/blog"]

Also my PG is

psql -c 'select version();'
                                                          version
----------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.3 (Ubuntu 12.3-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0, 64-bit
1 Like

Will have a look in a little while.

Here’s the output of the pp command (topic links ordered by url ascending):

["http://discourse.org#a#b",
 "http://discourse.org#faq",
 "http://forumwarz.com",
 "http://google.com",
 "http://twitter.com",
 "http://www.codinghorror.com/blog",
 "https://google.com"]

My PG version is 11.3:

 PostgreSQL 11.3 on x86_64-apple-darwin18.5.0, compiled by Apple LLVM version 10.0.1 (clang-1001.0.46.4), 64-bit

However, ordering by domain, gives me the same result as you, and the tests pass, so I tried that in DEV: Order links by domain in spec · discourse/discourse@b44748e · GitHub

1 Like