Skip to content

Commit

Permalink
CV2-3776: paginate tipline messages (#1677)
Browse files Browse the repository at this point in the history
* CV2-3776: paginate tipline messages

* CV2-3776: add a unit test for pagination using specific message
  • Loading branch information
melsawy authored Oct 4, 2023
1 parent 69641f1 commit 99a6aa9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/graph/types/team_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,6 @@ def shared_teams
end

def tipline_messages(uid:)
object.tipline_messages.where(uid: uid).last(100)
object.tipline_messages.where(uid: uid).order("id DESC")
end
end
76 changes: 76 additions & 0 deletions test/controllers/graphql_controller_9_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,82 @@ def setup
assert_equal "Not Found", JSON.parse(@response.body)['errors'][0]['message']
end

test "should paginate tipline messages" do
t = create_team slug: 'test', private: true
u = create_user
create_team_user user: u, team: t, role: 'admin'
uid = random_string

tp1_uid = create_tipline_message team_id: t.id, uid: uid
tp2_uid = create_tipline_message team_id: t.id, uid: uid
tp3_uid = create_tipline_message team_id: t.id, uid: uid

authenticate_with_user(u)

# Paginating one item per page

# Page 1
query = 'query read { team(slug: "test") { tipline_messages(first: 1, uid:"'+ uid +'") { pageInfo { endCursor, startCursor, hasPreviousPage, hasNextPage } edges { node { dbid } } } } }'
post :create, params: { query: query }
assert_response :success
data = JSON.parse(@response.body)['data']['team']['tipline_messages']
results = data['edges'].to_a.collect{ |e| e['node']['dbid'] }
assert_equal 1, results.size
assert_equal tp3_uid.id, results[0]
page_info = data['pageInfo']
assert page_info['hasNextPage']

# Page 2
query = 'query read { team(slug: "test") { tipline_messages(first: 1, after: "' + page_info['endCursor'] + '", uid:"'+ uid +'") { pageInfo { endCursor, startCursor, hasPreviousPage, hasNextPage } edges { node { dbid } } } } }'
post :create, params: { query: query }
assert_response :success
data = JSON.parse(@response.body)['data']['team']['tipline_messages']
results = data['edges'].to_a.collect{ |e| e['node']['dbid'] }
assert_equal 1, results.size
assert_equal tp2_uid.id, results[0]
page_info = data['pageInfo']
assert page_info['hasNextPage']

# Page 3
query = 'query read { team(slug: "test") { tipline_messages(first: 1, after: "' + page_info['endCursor'] + '", uid:"'+ uid +'") { pageInfo { endCursor, startCursor, hasPreviousPage, hasNextPage } edges { node { dbid } } } } }'
post :create, params: { query: query }
assert_response :success
data = JSON.parse(@response.body)['data']['team']['tipline_messages']
results = data['edges'].to_a.collect{ |e| e['node']['dbid'] }
assert_equal 1, results.size
assert_equal tp1_uid.id, results[0]
page_info = data['pageInfo']
assert !page_info['hasNextPage']
# paginate using specific message id
tp4_uid = create_tipline_message team_id: t.id, uid: uid
query = 'query read { team(slug: "test") { tipline_messages(uid:"'+ uid +'") { pageInfo { endCursor, startCursor, hasPreviousPage, hasNextPage } edges { node { dbid }, cursor } } } }'
post :create, params: { query: query }
assert_response :success
data = JSON.parse(@response.body)['data']['team']['tipline_messages']
id_cursor = {}
data['edges'].to_a.each{ |e| id_cursor[e['node']['dbid']] = e['cursor'] }
# Start with tp4_uid message
query = 'query read { team(slug: "test") { tipline_messages(first: 1, after: "' + id_cursor[tp4_uid.id] + '", uid:"'+ uid +'") { pageInfo { endCursor, startCursor, hasPreviousPage, hasNextPage } edges { node { dbid } } } } }'
post :create, params: { query: query }
assert_response :success
data = JSON.parse(@response.body)['data']['team']['tipline_messages']
results = data['edges'].to_a.collect{ |e| e['node']['dbid'] }
assert_equal 1, results.size
assert_equal tp3_uid.id, results[0]
page_info = data['pageInfo']
assert page_info['hasNextPage']
# Next page
query = 'query read { team(slug: "test") { tipline_messages(first: 1, after: "' + page_info['endCursor'] + '", uid:"'+ uid +'") { pageInfo { endCursor, startCursor, hasPreviousPage, hasNextPage } edges { node { dbid } } } } }'
post :create, params: { query: query }
assert_response :success
data = JSON.parse(@response.body)['data']['team']['tipline_messages']
results = data['edges'].to_a.collect{ |e| e['node']['dbid'] }
assert_equal 1, results.size
assert_equal tp2_uid.id, results[0]
page_info = data['pageInfo']
assert page_info['hasNextPage']
end

protected

def assert_error_message(expected)
Expand Down

0 comments on commit 99a6aa9

Please sign in to comment.