Skip to content

Commit

Permalink
chore: revert test version to match CI
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmeadi committed Nov 29, 2024
1 parent f151e2a commit 21e83e9
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/IStreamFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface IStreamFeed
/// <remarks>https://getstream.io/activity-feeds/docs/dotnet-csharp/feeds_101/?language=csharp</remarks>
Task<AddActivitiesResponse> AddActivitiesAsync(IEnumerable<Activity> activities);

Task<UpdateToTargetsResponse> BatchUpdateActivityToTargetsAsync(List<UpdateToTargetsRequest> reqs);

/// <summary>Add a new activity to the feed.</summary>
/// <remarks>https://getstream.io/activity-feeds/docs/dotnet-csharp/feeds_101/?language=csharp</remarks>
Task<Activity> AddActivityAsync(Activity activity);
Expand Down
27 changes: 27 additions & 0 deletions src/Models/UpdateToTargets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;

Check warning on line 2 in src/Models/UpdateToTargets.cs

View workflow job for this annotation

GitHub Actions / Run tests

Using directives should be ordered alphabetically by the namespaces

Check warning on line 2 in src/Models/UpdateToTargets.cs

View workflow job for this annotation

GitHub Actions / Run tests

Using directives should be ordered alphabetically by the namespaces

Check warning on line 2 in src/Models/UpdateToTargets.cs

View workflow job for this annotation

GitHub Actions / Run tests

Using directives should be ordered alphabetically by the namespaces

Check warning on line 2 in src/Models/UpdateToTargets.cs

View workflow job for this annotation

GitHub Actions / Run tests

Using directives should be ordered alphabetically by the namespaces
using Newtonsoft.Json;

namespace Stream.Models
{
public class UpdateToTargetsRequest
{
[JsonProperty("foreign_id")]
public string ForeignID { get; set; }

[JsonProperty("time")]
public string Time { get; set; }

[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("new_targets")]
public List<string> NewTargets { get; set; }

[JsonProperty("added_targets")]
public List<string> Adds { get; set; }

[JsonProperty("removed_targets")]
public List<string> RemovedTargets { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/StreamFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ public async Task<GenericGetResponse<Activity>> GetActivitiesAsync(int offset =
throw StreamException.FromResponse(response);
}

public async Task<UpdateToTargetsResponse> BatchUpdateActivityToTargetsAsync(List<UpdateToTargetsRequest> reqs)
{
var endpoint = $"feed_targets/{_feedSlug}/{_userId}/activity_to_targets/";

var request = _client.BuildAppRequest(endpoint, HttpMethod.Post);
request.SetJsonBody(StreamJsonConverter.SerializeObject(reqs));
var response = await _client.MakeRequestAsync(request);
if (response.StatusCode != HttpStatusCode.Created)
{
throw new HttpRequestException($"Request failed with status code {response.StatusCode}");
}

if (response.StatusCode == HttpStatusCode.Created)
return StreamJsonConverter.DeserializeObject<UpdateToTargetsResponse>(response.Content);

throw StreamException.FromResponse(response);
}

public async Task<UpdateToTargetsResponse> UpdateActivityToTargetsAsync(string id,
IEnumerable<string> adds = null,
IEnumerable<string> newTargets = null,
Expand All @@ -136,6 +154,8 @@ public async Task<UpdateToTargetsResponse> UpdateActivityToTargetsAsync(string i
newTargets?.ForEach(FeedIdValidator.ThrowIfFeedIdIsInvalid);
removed?.ForEach(FeedIdValidator.ThrowIfFeedIdIsInvalid);

Check warning on line 156 in src/StreamFeed.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain multiple blank lines in a row

Check warning on line 156 in src/StreamFeed.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain multiple blank lines in a row

Check warning on line 156 in src/StreamFeed.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain multiple blank lines in a row

Check warning on line 156 in src/StreamFeed.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain multiple blank lines in a row


var payload = new
{
id = id,
Expand Down
90 changes: 90 additions & 0 deletions tests/ActivityTests/UpdateActivityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,96 @@ public async Task TestUpdateToTargets()
Assert.AreEqual(2, updatedAct.To.ToList().FindAll(t => newOnes.Contains(t)).Count);
}

[Test]
public async Task TestBatchUpdateToTargets()
{
var fidTime = new ForeignIdTime(Guid.NewGuid().ToString(), DateTime.UtcNow);

var targets = new List<string>()
{
"flat:" + Guid.NewGuid().ToString(),
"user:" + Guid.NewGuid().ToString(),
};

var act = new Activity("upd", "test", "1")
{
ForeignId = fidTime.ForeignId,
Time = fidTime.Time,
To = targets,
};

var insertedAct = await this.UserFeed.AddActivityAsync(act);
Assert.AreEqual(2, insertedAct.To.Count);

// add 1
var add = "user:" + Guid.NewGuid().ToString();
var updateReqs = new List<UpdateToTargetsRequest>
{
new UpdateToTargetsRequest

Check warning on line 192 in tests/ActivityTests/UpdateActivityTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

Use trailing comma in multi-line initializers
{
Id = insertedAct.Id,
Adds = new List<string> { add }

Check warning on line 195 in tests/ActivityTests/UpdateActivityTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

Use trailing comma in multi-line initializers
}
};
var updateResp = await this.UserFeed.BatchUpdateActivityToTargetsAsync(updateReqs);
Assert.AreEqual(insertedAct.Id, updateResp.Activity.Id);
Assert.AreEqual(1, updateResp.Added.Count);
Assert.AreEqual(add, updateResp.Added[0]);
Assert.AreEqual(3, updateResp.Activity.To.Count);
Assert.IsNotNull(updateResp.Activity.To.ToList().Find(t => t == add));

var updatedAct = (await this.UserFeed.GetActivitiesAsync(0, 1, FeedFilter.Where().IdLessThanEqual(insertedAct.Id))).Results.FirstOrDefault();
Assert.NotNull(updatedAct);
Assert.AreEqual(3, updatedAct.To.Count);
Assert.IsNotNull(updatedAct.To.ToList().Find(t => t == add));

// remove 1
var remove = targets[0];
updateReqs = new List<UpdateToTargetsRequest>
{
new UpdateToTargetsRequest
{
Id = insertedAct.Id,
RemovedTargets = new List<string> { remove }
}
};
updateResp = await this.UserFeed.BatchUpdateActivityToTargetsAsync(updateReqs);
Assert.AreEqual(insertedAct.Id, updateResp.Activity.Id);
Assert.AreEqual(1, updateResp.Removed.Count);
Assert.AreEqual(remove, updateResp.Removed[0]);
Assert.AreEqual(2, updateResp.Activity.To.Count);
Assert.IsNull(updateResp.Activity.To.ToList().Find(t => t == remove));

updatedAct = (await this.UserFeed.GetActivitiesAsync(0, 1, FeedFilter.Where().IdLessThanEqual(insertedAct.Id))).Results.FirstOrDefault();
Assert.NotNull(updatedAct);
Assert.AreEqual(2, updatedAct.To.Count);
Assert.IsNull(updatedAct.To.ToList().Find(t => t == remove));

// new ones
var newOnes = new List<string>()
{
"flat:" + Guid.NewGuid().ToString(),
"user:" + Guid.NewGuid().ToString(),
};
updateReqs = new List<UpdateToTargetsRequest>
{
new UpdateToTargetsRequest
{
Id = insertedAct.Id,
NewTargets = newOnes
}
};
updateResp = await this.UserFeed.BatchUpdateActivityToTargetsAsync(updateReqs);
Assert.AreEqual(insertedAct.Id, updateResp.Activity.Id);
Assert.AreEqual(2, updateResp.Activity.To.Count);
Assert.AreEqual(2, updateResp.Added.Count);
Assert.AreEqual(2, updateResp.Added.ToList().FindAll(t => newOnes.Contains(t)).Count);
updatedAct = (await this.UserFeed.GetActivitiesAsync(0, 1, FeedFilter.Where().IdLessThanEqual(insertedAct.Id))).Results.FirstOrDefault();
Assert.NotNull(updatedAct);
Assert.AreEqual(2, updatedAct.To.Count);
Assert.AreEqual(2, updatedAct.To.ToList().FindAll(t => newOnes.Contains(t)).Count);
}

[Test]
public async Task TestActivityPartialUpdateByForeignIDTime()
{
Expand Down

0 comments on commit 21e83e9

Please sign in to comment.