Skip to content

Commit

Permalink
More tests changed to constraint model
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeybusygin committed Aug 29, 2024
1 parent 0321058 commit 5ab3298
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 65 deletions.
26 changes: 14 additions & 12 deletions ShippingRates.Tests/Helpers/DHLServicesValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using NUnit.Framework;
using ShippingRates.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShippingRates.Helpers.Tests
{
Expand All @@ -13,9 +9,12 @@ public class DHLServicesValidatorTests
[Test()]
public void IsServiceValidTest()
{
Assert.IsTrue(DHLServicesValidator.IsServiceValid('M'));
Assert.IsTrue(DHLServicesValidator.IsServiceValid('n'));
Assert.IsFalse(DHLServicesValidator.IsServiceValid('9'));
Assert.Multiple(() =>
{
Assert.That(DHLServicesValidator.IsServiceValid('M'), Is.True);
Assert.That(DHLServicesValidator.IsServiceValid('n'), Is.True);
Assert.That(DHLServicesValidator.IsServiceValid('9'), Is.False);
});
}

[Test()]
Expand All @@ -24,11 +23,14 @@ public void GetValidServicesTest()
var services = new char[] { 'M', 'n', '9' };
var validServices = DHLServicesValidator.GetValidServices(services);

Assert.IsTrue(validServices.Count() == 2);
Assert.IsTrue(validServices.Count(s => s == 'M') == 1);
Assert.IsTrue(validServices.Count(s => s == 'n') == 0);
Assert.IsTrue(validServices.Count(s => s == 'N') == 1);
Assert.IsTrue(validServices.Count(s => s == '9') == 0);
Assert.Multiple(() =>
{
Assert.That(validServices, Has.Length.EqualTo(2));
Assert.That(validServices.Count(s => s == 'M'), Is.EqualTo(1));
Assert.That(validServices.Where(s => s == 'n'), Is.Empty);
Assert.That(validServices.Count(s => s == 'N'), Is.EqualTo(1));
Assert.That(validServices.Where(s => s == '9'), Is.Empty);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void CanGetFedExServiceCodes()
{
var serviceCodes = _provider.GetServiceCodes();

Assert.NotNull(serviceCodes);
Assert.IsNotEmpty(serviceCodes);
Assert.That(serviceCodes, Is.Not.Null);
Assert.That(serviceCodes, Is.Not.Empty);
}
}
}
52 changes: 32 additions & 20 deletions ShippingRates.Tests/Units/PackageDimensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,48 @@ public class PackageWeightTests
public void LbsToKgs()
{
var weight1 = new PackageWeight(UnitsSystem.USCustomary, 5);
Assert.AreEqual(5, weight1.Get());
Assert.AreEqual(5, weight1.Get(UnitsSystem.USCustomary));
Assert.AreEqual(2.26796185m, weight1.Get(UnitsSystem.Metric));
Assert.AreEqual(5, weight1.GetRounded(UnitsSystem.USCustomary));
Assert.AreEqual(3, weight1.GetRounded(UnitsSystem.Metric));
Assert.Multiple(() =>
{
Assert.That(weight1.Get(), Is.EqualTo(5));
Assert.That(weight1.Get(UnitsSystem.USCustomary), Is.EqualTo(5));
Assert.That(weight1.Get(UnitsSystem.Metric), Is.EqualTo(2.26796185m));
Assert.That(weight1.GetRounded(UnitsSystem.USCustomary), Is.EqualTo(5));
Assert.That(weight1.GetRounded(UnitsSystem.Metric), Is.EqualTo(3));
});

var weight2 = new PackageWeight(UnitsSystem.USCustomary, 0.3m);
Assert.AreEqual(0.3m, weight2.Get());
Assert.AreEqual(0.3m, weight2.Get(UnitsSystem.USCustomary));
Assert.AreEqual(0.136077711m, weight2.Get(UnitsSystem.Metric));
Assert.AreEqual(1, weight2.GetRounded(UnitsSystem.USCustomary));
Assert.AreEqual(1, weight2.GetRounded(UnitsSystem.Metric));
Assert.Multiple(() =>
{
Assert.That(weight2.Get(), Is.EqualTo(0.3m));
Assert.That(weight2.Get(UnitsSystem.USCustomary), Is.EqualTo(0.3m));
Assert.That(weight2.Get(UnitsSystem.Metric), Is.EqualTo(0.136077711m));
Assert.That(weight2.GetRounded(UnitsSystem.USCustomary), Is.EqualTo(1));
Assert.That(weight2.GetRounded(UnitsSystem.Metric), Is.EqualTo(1));
});
}

[Test()]
public void KgsToLbs()
{
var weight1 = new PackageWeight(UnitsSystem.Metric, 5);
Assert.AreEqual(5, weight1.Get());
Assert.AreEqual(11.0231m, weight1.Get(UnitsSystem.USCustomary));
Assert.AreEqual(5, weight1.Get(UnitsSystem.Metric));
Assert.AreEqual(12, weight1.GetRounded(UnitsSystem.USCustomary));
Assert.AreEqual(5, weight1.GetRounded(UnitsSystem.Metric));
Assert.Multiple(() =>
{
Assert.That(weight1.Get(), Is.EqualTo(5));
Assert.That(weight1.Get(UnitsSystem.USCustomary), Is.EqualTo(11.0231m));
Assert.That(weight1.Get(UnitsSystem.Metric), Is.EqualTo(5));
Assert.That(weight1.GetRounded(UnitsSystem.USCustomary), Is.EqualTo(12));
Assert.That(weight1.GetRounded(UnitsSystem.Metric), Is.EqualTo(5));
});

var weight2 = new PackageWeight(UnitsSystem.Metric, 0.5m);
Assert.AreEqual(0.5m, weight2.Get());
Assert.AreEqual(1.10231m, weight2.Get(UnitsSystem.USCustomary));
Assert.AreEqual(0.5m, weight2.Get(UnitsSystem.Metric));
Assert.AreEqual(2, weight2.GetRounded(UnitsSystem.USCustomary));
Assert.AreEqual(1, weight2.GetRounded(UnitsSystem.Metric));
Assert.Multiple(() =>
{
Assert.That(weight2.Get(), Is.EqualTo(0.5m));
Assert.That(weight2.Get(UnitsSystem.USCustomary), Is.EqualTo(1.10231m));
Assert.That(weight2.Get(UnitsSystem.Metric), Is.EqualTo(0.5m));
Assert.That(weight2.GetRounded(UnitsSystem.USCustomary), Is.EqualTo(2));
Assert.That(weight2.GetRounded(UnitsSystem.Metric), Is.EqualTo(1));
});
}
}
}
20 changes: 11 additions & 9 deletions ShippingRates.Tests/Units/PackageKgCmTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NUnit.Framework;
using ShippingRates.Models;

namespace ShippingRates.Tests.Units
{
Expand All @@ -12,15 +11,18 @@ public void TestDimensions()
var packageLbsInches = new Package(10, 20, 30, 40, 50);
var packageKgCm = new PackageKgCm(10, 20, 30, 40, 50);

Assert.AreNotEqual(packageKgCm.GetHeight(UnitsSystem.Metric), packageLbsInches.GetHeight(UnitsSystem.Metric));
Assert.AreNotEqual(packageKgCm.GetLength(UnitsSystem.Metric), packageLbsInches.GetLength(UnitsSystem.Metric));
Assert.AreNotEqual(packageKgCm.GetWidth(UnitsSystem.USCustomary), packageLbsInches.GetWidth(UnitsSystem.USCustomary));
Assert.AreNotEqual(packageKgCm.GetHeight(UnitsSystem.USCustomary), packageLbsInches.GetHeight(UnitsSystem.USCustomary));
Assert.Multiple(() =>
{
Assert.That(packageLbsInches.GetHeight(UnitsSystem.Metric), Is.Not.EqualTo(packageKgCm.GetHeight(UnitsSystem.Metric)));
Assert.That(packageLbsInches.GetLength(UnitsSystem.Metric), Is.Not.EqualTo(packageKgCm.GetLength(UnitsSystem.Metric)));
Assert.That(packageLbsInches.GetWidth(UnitsSystem.USCustomary), Is.Not.EqualTo(packageKgCm.GetWidth(UnitsSystem.USCustomary)));
Assert.That(packageLbsInches.GetHeight(UnitsSystem.USCustomary), Is.Not.EqualTo(packageKgCm.GetHeight(UnitsSystem.USCustomary)));

Assert.AreEqual(40, packageLbsInches.GetWeight(UnitsSystem.USCustomary));
Assert.AreEqual(18.1436948m, packageLbsInches.GetWeight(UnitsSystem.Metric));
Assert.AreEqual(88.1848m, packageKgCm.GetWeight(UnitsSystem.USCustomary));
Assert.AreEqual(40, packageKgCm.GetWeight(UnitsSystem.Metric));
Assert.That(packageLbsInches.GetWeight(UnitsSystem.USCustomary), Is.EqualTo(40));
Assert.That(packageLbsInches.GetWeight(UnitsSystem.Metric), Is.EqualTo(18.1436948m));
Assert.That(packageKgCm.GetWeight(UnitsSystem.USCustomary), Is.EqualTo(88.1848m));
Assert.That(packageKgCm.GetWeight(UnitsSystem.Metric), Is.EqualTo(40));
});
}
}
}
7 changes: 5 additions & 2 deletions ShippingRates.Tests/Units/PackageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ public class PackageTests
public void PoundsAndOuncesCalculatedCorrectly(decimal weight, int pounds, decimal ounces)
{
var package = new Package(1, 2, 3, weight, 100);
Assert.That(package.PoundsAndOunces.Pounds, Is.EqualTo(pounds));
Assert.That(package.PoundsAndOunces.Ounces, Is.EqualTo(ounces));
Assert.Multiple(() =>
{
Assert.That(package.PoundsAndOunces.Pounds, Is.EqualTo(pounds));
Assert.That(package.PoundsAndOunces.Ounces, Is.EqualTo(ounces));
});
}
}
}
52 changes: 32 additions & 20 deletions ShippingRates.Tests/Units/PackageWeightTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,48 @@ public class PackageDimensionTests
public void InchesToCm()
{
var dimension1 = new PackageDimension(UnitsSystem.USCustomary, 5);
Assert.AreEqual(5, dimension1.Get());
Assert.AreEqual(5, dimension1.Get(UnitsSystem.USCustomary));
Assert.AreEqual(12.7m, dimension1.Get(UnitsSystem.Metric));
Assert.AreEqual(5, dimension1.GetRounded(UnitsSystem.USCustomary));
Assert.AreEqual(13, dimension1.GetRounded(UnitsSystem.Metric));
Assert.Multiple(() =>
{
Assert.That(dimension1.Get(), Is.EqualTo(5));
Assert.That(dimension1.Get(UnitsSystem.USCustomary), Is.EqualTo(5));
Assert.That(dimension1.Get(UnitsSystem.Metric), Is.EqualTo(12.7m));
Assert.That(dimension1.GetRounded(UnitsSystem.USCustomary), Is.EqualTo(5));
Assert.That(dimension1.GetRounded(UnitsSystem.Metric), Is.EqualTo(13));
});

var dimension2 = new PackageDimension(UnitsSystem.USCustomary, 0.4m);
Assert.AreEqual(0.4m, dimension2.Get());
Assert.AreEqual(0.4m, dimension2.Get(UnitsSystem.USCustomary));
Assert.AreEqual(1.016m, dimension2.Get(UnitsSystem.Metric));
Assert.AreEqual(1, dimension2.GetRounded(UnitsSystem.USCustomary));
Assert.AreEqual(2, dimension2.GetRounded(UnitsSystem.Metric));
Assert.Multiple(() =>
{
Assert.That(dimension2.Get(), Is.EqualTo(0.4m));
Assert.That(dimension2.Get(UnitsSystem.USCustomary), Is.EqualTo(0.4m));
Assert.That(dimension2.Get(UnitsSystem.Metric), Is.EqualTo(1.016m));
Assert.That(dimension2.GetRounded(UnitsSystem.USCustomary), Is.EqualTo(1));
Assert.That(dimension2.GetRounded(UnitsSystem.Metric), Is.EqualTo(2));
});
}

[Test()]
public void CmToInches()
{
var dimension1 = new PackageDimension(UnitsSystem.Metric, 6);
Assert.AreEqual(6, dimension1.Get());
Assert.AreEqual(2.362206m, dimension1.Get(UnitsSystem.USCustomary));
Assert.AreEqual(6, dimension1.Get(UnitsSystem.Metric));
Assert.AreEqual(3, dimension1.GetRounded(UnitsSystem.USCustomary));
Assert.AreEqual(6, dimension1.GetRounded(UnitsSystem.Metric));
Assert.Multiple(() =>
{
Assert.That(dimension1.Get(), Is.EqualTo(6));
Assert.That(dimension1.Get(UnitsSystem.USCustomary), Is.EqualTo(2.362206m));
Assert.That(dimension1.Get(UnitsSystem.Metric), Is.EqualTo(6));
Assert.That(dimension1.GetRounded(UnitsSystem.USCustomary), Is.EqualTo(3));
Assert.That(dimension1.GetRounded(UnitsSystem.Metric), Is.EqualTo(6));
});

var dimension2 = new PackageDimension(UnitsSystem.Metric, 2.6m);
Assert.AreEqual(2.6m, dimension2.Get());
Assert.AreEqual(1.0236226m, dimension2.Get(UnitsSystem.USCustomary));
Assert.AreEqual(2.6m, dimension2.Get(UnitsSystem.Metric));
Assert.AreEqual(2, dimension2.GetRounded(UnitsSystem.USCustomary));
Assert.AreEqual(3, dimension2.GetRounded(UnitsSystem.Metric));
Assert.Multiple(() =>
{
Assert.That(dimension2.Get(), Is.EqualTo(2.6m));
Assert.That(dimension2.Get(UnitsSystem.USCustomary), Is.EqualTo(1.0236226m));
Assert.That(dimension2.Get(UnitsSystem.Metric), Is.EqualTo(2.6m));
Assert.That(dimension2.GetRounded(UnitsSystem.USCustomary), Is.EqualTo(2));
Assert.That(dimension2.GetRounded(UnitsSystem.Metric), Is.EqualTo(3));
});
}
}
}

0 comments on commit 5ab3298

Please sign in to comment.