Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PackageFeedSortingTest functional test #6185

Merged
merged 3 commits into from
Jul 23, 2018
Merged

Conversation

chenriksson
Copy link
Member

Fixes #6168 (functional test failure when <10 distinct packages in feed)

@@ -196,47 +197,67 @@ private static string GetPackagesAppearInFeedInOrderUrl(DateTime time, string ti
var request = WebRequest.Create(UrlHelper.V2FeedRootUrl + @"stats/downloads/last6weeks/");
var response = await request.GetResponseAsync();

string responseText;
using (var sr = new StreamReader(response.GetResponseStream()))
string responseText = await new StreamReader(response.GetResponseStream())
Copy link
Contributor

@loic-sharma loic-sharma Jul 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were the usings block for the StreamReaders removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oversight in refactoring - fixed!

}

// skip over `"PackageId": `
feedResponseText = feedResponseText.Substring(start + 14);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of using @"""PackageId"": ".Length instead of 14?

@chenriksson chenriksson force-pushed the chenriks-testfix-feedsort branch from bf94171 to 936f829 Compare July 19, 2018 16:32
@chenriksson chenriksson merged commit c16ba4f into dev Jul 23, 2018
@chenriksson chenriksson deleted the chenriks-testfix-feedsort branch July 23, 2018 16:18
request = WebRequest.Create(UrlHelper.BaseUrl + @"stats/packageversions");
private string[] GetPackageNamesFromFeedResponse(string feedResponseText)
{
const string PackageIdStartKey = @"""PackageId"": """;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the \" syntax is a little easier to read, you've got like a billion "s in here

break;
}

feedResponseText = feedResponseText.Substring(start + PackageIdStartKey.Length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response is simple JSON, I think we could easily parse it with JSON.NET and this code would be much simpler.

var packages = JArray.Parse(feedResponseText);
foreach (var package in packages)
{
    yield return ((JObject)package)["PackageId"].Value<string>();
}

Could also model it and use JsonConvert.DeserializeObject, e.g.

private class Last6WeeksStatistics
{
    public IEnumerable<PackageStatistic> PackageStatistics { get; set; }
}

private class PackageStatistic
{
    public string PackageId { get; set; }
}

responseText = await sr.ReadToEndAsync();
// Check to make sure the top 10 packages are in the same order as the feed.
// We add angle brackets to prevent false failures due to duplicate package names in the page.
var condition = statsResponse.IndexOf(">" + last + "<", StringComparison.Ordinal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty hacky--we're asserting the order of an HTML table for looking for the index of >{package ID}< and so on.

The real structure is:

<table>
    <tbody>
        <tr>
            <td>{row number}<td>
            <td><a href="/packages/{package ID}">{package ID}</a><td>
            ...
        </tr>
    </tbody>
</table>

I guess this is sufficient for now, but this code would be much more readable if you could find some sort of actual HTML parser for this purpose.

@chenriksson
Copy link
Member Author

@scottbommarito My goal was just the bug fix, but did some light refactoring while there. Not sure it's worth revisiting the test for heavier refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants