Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit ac66dec

Browse files
committed
Fix IsSameWeek and IsSameMonth. Add tests for that.
1 parent a5a1c50 commit ac66dec

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

src/GitHub.VisualStudio/Services/UsageService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public bool IsSameDay(DateTimeOffset lastUpdated)
3232

3333
public bool IsSameWeek(DateTimeOffset lastUpdated)
3434
{
35-
return GetIso8601WeekOfYear(lastUpdated) == GetIso8601WeekOfYear(DateTimeOffset.Now) && lastUpdated.Year != DateTimeOffset.Now.Year;
35+
return GetIso8601WeekOfYear(lastUpdated) == GetIso8601WeekOfYear(DateTimeOffset.Now) && lastUpdated.Year == DateTimeOffset.Now.Year;
3636
}
3737

3838
public bool IsSameMonth(DateTimeOffset lastUpdated)
3939
{
40-
return lastUpdated.Month == DateTimeOffset.Now.Month;
40+
return lastUpdated.Month == DateTimeOffset.Now.Month && lastUpdated.Year == DateTimeOffset.Now.Year;
4141
}
4242

4343
public IDisposable StartTimer(Func<Task> callback, TimeSpan dueTime, TimeSpan period)

test/UnitTests/GitHub.VisualStudio/Services/UsageTrackerTests.cs renamed to test/UnitTests/GitHub.VisualStudio/Services/MetricsTests.cs

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
using GitHub.Settings;
88
using NSubstitute;
99
using NUnit.Framework;
10-
using System.Reflection;
1110
using System.Linq;
1211
using System.Globalization;
1312
using GitHub.Reflection;
14-
using GitHub;
1513

1614
namespace MetricsTests
1715
{
@@ -418,4 +416,90 @@ static IUsageService CreateUsageService(
418416
return result;
419417
}
420418
}
419+
420+
public class UsageServiceTests : TestBaseClass
421+
{
422+
[Test]
423+
public void IsSameDayWorks()
424+
{
425+
var usageService = new UsageService(Substitute.For<IGitHubServiceProvider>());
426+
var now = DateTimeOffset.Now;
427+
Assert.True(usageService.IsSameDay(now));
428+
Assert.True(usageService.IsSameDay(new DateTimeOffset(now.Year, now.Month, now.Day, 0, 0, 0, TimeSpan.Zero)));
429+
Assert.False(usageService.IsSameDay(new DateTimeOffset(now.Year, now.Month, now.Day+1, 0, 0, 0, TimeSpan.Zero)));
430+
Assert.False(usageService.IsSameDay(new DateTimeOffset(now.Year, now.Month, now.Day-1, 0, 0, 0, TimeSpan.Zero)));
431+
Assert.True(usageService.IsSameDay(new DateTimeOffset(now.Year, now.Month, now.Day, 10, 3, 1, TimeSpan.Zero)));
432+
Assert.False(usageService.IsSameDay(new DateTimeOffset(now.Year, now.Month, now.Day+1, 10, 3, 1, TimeSpan.Zero)));
433+
Assert.False(usageService.IsSameDay(new DateTimeOffset(now.Year, now.Month, now.Day-1, 10, 3, 1, TimeSpan.Zero)));
434+
}
435+
436+
[Test]
437+
public void IsSameWeekWorks()
438+
{
439+
var usageService = new UsageService(Substitute.For<IGitHubServiceProvider>());
440+
var now = DateTimeOffset.Now;
441+
442+
Assert.True(usageService.IsSameWeek(now));
443+
var nowWeek = GetIso8601WeekOfYear(now);
444+
445+
DateTimeOffset nextWeek = now;
446+
for (int i = 1; i < 8; i++)
447+
{
448+
nextWeek = nextWeek.AddDays(1);
449+
var week = GetIso8601WeekOfYear(nextWeek);
450+
Assert.AreEqual(week == nowWeek, usageService.IsSameWeek(nextWeek));
451+
}
452+
453+
DateTimeOffset prevWeek = now;
454+
for (int i = 1; i < 8; i++)
455+
{
456+
prevWeek = prevWeek.AddDays(-1);
457+
var week = GetIso8601WeekOfYear(prevWeek);
458+
Assert.AreEqual(week == nowWeek, usageService.IsSameWeek(prevWeek));
459+
}
460+
461+
Assert.False(usageService.IsSameWeek(now.AddYears(1)));
462+
}
463+
464+
[Test]
465+
public void IsSameMonthWorks()
466+
{
467+
var usageService = new UsageService(Substitute.For<IGitHubServiceProvider>());
468+
var now = DateTimeOffset.Now;
469+
470+
Assert.True(usageService.IsSameMonth(now));
471+
472+
DateTimeOffset nextMonth = now;
473+
for (int i = 1; i < 40; i++)
474+
{
475+
nextMonth = nextMonth.AddDays(1);
476+
Assert.AreEqual(nextMonth.Month == now.Month, usageService.IsSameMonth(nextMonth));
477+
}
478+
479+
DateTimeOffset prevMonth = now;
480+
for (int i = 1; i < 40; i++)
481+
{
482+
prevMonth = prevMonth.AddDays(-1);
483+
Assert.AreEqual(prevMonth.Month == now.Month, usageService.IsSameMonth(prevMonth));
484+
}
485+
486+
Assert.False(usageService.IsSameMonth(now.AddYears(1)));
487+
}
488+
489+
// http://blogs.msdn.com/b/shawnste/archive/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net.aspx
490+
static int GetIso8601WeekOfYear(DateTimeOffset time)
491+
{
492+
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
493+
// be the same week# as whatever Thursday, Friday or Saturday are,
494+
// and we always get those right
495+
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time.UtcDateTime);
496+
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
497+
{
498+
time = time.AddDays(3);
499+
}
500+
501+
// Return the week of our adjusted day
502+
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time.UtcDateTime, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
503+
}
504+
}
421505
}

test/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@
268268
<Compile Include="GitHub.VisualStudio\Services\JsonConnectionCacheTests.cs" />
269269
<Compile Include="GitHub.VisualStudio\Services\ConnectionManagerTests.cs" />
270270
<Compile Include="GitHub.VisualStudio\Services\RepositoryPublishServiceTests.cs" />
271-
<Compile Include="GitHub.VisualStudio\Services\UsageTrackerTests.cs" />
271+
<Compile Include="GitHub.VisualStudio\Services\MetricsTests.cs" />
272272
<Compile Include="GitHub.VisualStudio\TeamExplorer\Home\GraphsNavigationItemTests.cs" />
273273
<Compile Include="Helpers\CommandTestHelpers.cs" />
274274
<Compile Include="Helpers\LazySubstitute.cs" />

0 commit comments

Comments
 (0)