Awight has uploaded a new change for review.
https://gerrit.wikimedia.org/r/172958
Change subject: Tests for BannerAllocationCalculator
......................................................................
Tests for BannerAllocationCalculator
* Introduces expected allocations for test fixtures.
* Lenient float comparison.
Change-Id: Ifcc1ae015ec7f0d5789f7cd3a0918d4907a56b1e
---
M CentralNotice.hooks.php
A tests/BannerAllocationCalculatorTest.php
M tests/CentralNoticeTestFixtures.php
M tests/ComparisonUtil.php
4 files changed, 167 insertions(+), 24 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralNotice
refs/changes/58/172958/1
diff --git a/CentralNotice.hooks.php b/CentralNotice.hooks.php
index 919dc13..d5c5ecc 100644
--- a/CentralNotice.hooks.php
+++ b/CentralNotice.hooks.php
@@ -362,6 +362,7 @@
$files[ ] = __DIR__ . '/tests/AllocationsTest.php';
$files[ ] = __DIR__ . '/tests/ApiAllocationsTest.php';
$files[ ] = __DIR__ . '/tests/CentralNoticeTest.php';
+ $files[ ] = __DIR__ . '/tests/BannerAllocationCalculatorTest.php';
$files[ ] = __DIR__ . '/tests/BannerChoiceDataProviderTest.php';
$files[ ] = __DIR__ . '/tests/BannerTest.php';
return true;
diff --git a/tests/BannerAllocationCalculatorTest.php
b/tests/BannerAllocationCalculatorTest.php
new file mode 100644
index 0000000..2e3b298
--- /dev/null
+++ b/tests/BannerAllocationCalculatorTest.php
@@ -0,0 +1,48 @@
+<?php
+
+/**
+ * @group CentralNotice
+ * @group medium
+ * @group Database
+ */
+class BannerAllocationCalculatorTest extends MediaWikiTestCase {
+ /** @var CentralNoticeTestFixtures */
+ protected $cnFixtures;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->cnFixtures = new CentralNoticeTestFixtures();
+ }
+
+ protected function tearDown() {
+ $this->cnFixtures->removeFixtures();
+ parent::tearDown();
+ }
+
+ /**
+ * @dataProvider CentralNoticeTestFixtures::allocationsData
+ */
+ public function testAllocations( $fixtures, $expectedChoices,
$expectedAllocations ) {
+ $this->cnFixtures->addFixtures( $fixtures );
+
+ $allocationsProvider = new BannerChoiceDataProvider(
+ CentralNoticeTestFixtures::getDefaultProject(),
+ CentralNoticeTestFixtures::getDefaultLanguage(),
+ BannerChoiceDataProvider::ANONYMOUS
+ );
+ $choices = $allocationsProvider->getChoicesForCountry(
+ CentralNoticeTestFixtures::getDefaultCountry()
+ );
+ $banners =
BannerAllocationCalculator::filterAndTransformBanners(
+ $choices,
+ CentralNoticeTestFixtures::getDefaultDevice(),
+ 0
+ );
+ $allocations =
BannerAllocationCalculator::calculateAllocations( $banners );
+
+ $this->assertTrue(
+ ComparisonUtil::assertEqualAllocations( $allocations,
$expectedAllocations )
+ );
+ }
+}
diff --git a/tests/CentralNoticeTestFixtures.php
b/tests/CentralNoticeTestFixtures.php
index da43cba..6e64eaf 100644
--- a/tests/CentralNoticeTestFixtures.php
+++ b/tests/CentralNoticeTestFixtures.php
@@ -7,7 +7,7 @@
// Use exactly the api defaults where available
static public $defaultCampaign;
static public $defaultBanner;
-
+
function __construct() {
$this->user = User::newFromName( 'UTSysop' );
@@ -15,11 +15,11 @@
'enabled' => 1,
// inclusive comparison is used, so this does not cause
a race condition.
'startTs' => wfTimestamp( TS_MW ),
- 'projects' => array(
ApiCentralNoticeAllocations::DEFAULT_PROJECT ),
- 'project_languages' => array(
ApiCentralNoticeAllocations::DEFAULT_LANGUAGE ),
+ 'projects' => array(
CentralNoticeTestFixtures::getDefaultProject() ),
+ 'project_languages' => array(
CentralNoticeTestFixtures::getDefaultLanguage() ),
'preferred' => CentralNotice::NORMAL_PRIORITY,
'geotargetted' => 0,
- 'geo_countries' => array(
ApiCentralNoticeAllocations::DEFAULT_COUNTRY ),
+ 'geo_countries' => array(
CentralNoticeTestFixtures::getDefaultCountry() ),
'throttle' => 100,
'banners' => array(),
);
@@ -35,8 +35,27 @@
);
}
+ static function getDefaultLanguage() {
+ return ApiCentralNoticeAllocations::DEFAULT_LANGUAGE;
+ }
+
+ static function getDefaultProject() {
+ return ApiCentralNoticeAllocations::DEFAULT_PROJECT;
+ }
+
+ static function getDefaultCountry() {
+ return ApiCentralNoticeAllocations::DEFAULT_COUNTRY;
+ }
+
+ static function getDefaultDevice() {
+ return 'desktop';
+ }
+
function addFixtures( $spec ) {
- CNDeviceTarget::addDeviceTarget( 'desktop',
'{{int:centralnotice-devicetype-desktop}}' );
+ CNDeviceTarget::addDeviceTarget(
+ CentralNoticeTestFixtures::getDefaultDevice(),
+ '{{int:centralnotice-devicetype-desktop}}'
+ );
foreach ( $spec['campaigns'] as $campaignSpec ) {
$campaign = $campaignSpec + static::$defaultCampaign +
array(
@@ -104,10 +123,10 @@
CentralNoticeTestFixtures::completenessScenario(),
CentralNoticeTestFixtures::throttlingScenario(),
CentralNoticeTestFixtures::overallocationScenario(),
- //CentralNoticeTestFixtures::blanksScenario(),
- //CentralNoticeTestFixtures::priorityScenario(),
- CentralNoticeTestFixtures::geoInUsaScenario(),
- CentralNoticeTestFixtures::geoNotInUsaScenario(),
+ CentralNoticeTestFixtures::blanksScenario(),
+ CentralNoticeTestFixtures::priorityScenario(),
+ CentralNoticeTestFixtures::geoInCountryScenario(),
+ CentralNoticeTestFixtures::geoNotInCountryScenario(),
CentralNoticeTestFixtures::notInProjectScenario(),
CentralNoticeTestFixtures::notInLanguageScenario(),
CentralNoticeTestFixtures::notInTimeWindowScenario(),
@@ -131,6 +150,7 @@
'startTs' => $startTs,
'geotargetted' => true,
'geo_countries' => array(
+ 'XX',
'FR',
'GR',
),
@@ -153,6 +173,7 @@
'start' => $startTs,
'end' => $endTs,
'countries' => array(
+ 'XX',
'FR',
'GR',
),
@@ -168,6 +189,10 @@
),
),
),
+ ),
+ //alloc
+ array(
+ 'b1' => '0.500',
),
);
}
@@ -232,6 +257,13 @@
),
),
),
+ //alloc
+ array(
+ 'c1b1' => '0.300',
+ 'c1b2' => '0.300',
+ 'c2b1' => '0.200',
+ 'c2b2' => '0.200',
+ ),
);
}
@@ -278,10 +310,15 @@
),
),
),
+ //alloc
+ array(
+ 'b1' => '0.024',
+ 'b2' => '0.488',
+ 'b3' => '0.488',
+ ),
);
}
- // FIXME: unused
static function blanksScenario() {
return array(
// input
@@ -290,7 +327,9 @@
array(
'throttle' => 10,
'banners' => array(
- array(),
+ array(
+ 'name' => 'b1',
+ ),
),
),
),
@@ -298,13 +337,18 @@
// expected
array(
array(
- 'slots' => 3,
+ 'banners' => array(
+ 'b1',
+ ),
),
- )
+ ),
+ //alloc
+ array(
+ 'b1' => 0.100,
+ ),
);
}
- // FIXME: unused
static function priorityScenario() {
return array(
// input
@@ -314,14 +358,18 @@
'name' => 'c1',
'preferred' =>
CentralNotice::LOW_PRIORITY,
'banners' => array(
- array(),
+ array(
+ 'name' =>
'c1b1',
+ ),
),
),
array(
'name' => 'c2',
'preferred' =>
CentralNotice::NORMAL_PRIORITY,
'banners' => array(
- array(),
+ array(
+ 'name' =>
'c2b1',
+ ),
),
),
),
@@ -330,17 +378,20 @@
array(
array(
'campaign' => 'c1',
- 'slots' => 0,
),
array(
'campaign' => 'c2',
- 'slots' => 30,
),
+ ),
+ //alloc
+ array(
+ 'c1b1' => 0.000,
+ 'c2b1' => 1.000,
),
);
}
- static function geoInUsaScenario() {
+ static function geoInCountryScenario() {
return array(
// input
array(
@@ -349,7 +400,7 @@
'geotargetted' => true,
'geo_countries' => array(
'FR',
- 'US',
+ 'XX',
),
'banners' => array(
array(
@@ -366,7 +417,7 @@
'geotargetted' => true,
'countries' => array(
'FR',
- 'US',
+ 'XX',
),
'banners' => array(
array(
@@ -376,10 +427,14 @@
),
),
),
+ //alloc
+ array(
+ 'b1' => 1.000,
+ ),
);
}
- static function geoNotInUsaScenario() {
+ static function geoNotInCountryScenario() {
return array(
// input
array(
@@ -415,6 +470,9 @@
),
),
),
+ //alloc
+ array(
+ ),
);
}
@@ -435,6 +493,9 @@
),
),
// expected
+ array(
+ ),
+ //alloc
array(
),
);
@@ -459,6 +520,9 @@
// expected
array(
),
+ //alloc
+ array(
+ ),
);
}
@@ -479,6 +543,9 @@
),
),
// expected
+ array(
+ ),
+ //alloc
array(
),
);
@@ -504,6 +571,9 @@
// expected
array(
),
+ //alloc
+ array(
+ ),
);
}
}
diff --git a/tests/ComparisonUtil.php b/tests/ComparisonUtil.php
index 0fde013..c2575a5 100644
--- a/tests/ComparisonUtil.php
+++ b/tests/ComparisonUtil.php
@@ -4,7 +4,7 @@
/**
* Check that all elements of $inner match in $super, recursively.
*/
- static public function assertSuperset( $super, $inner, $path = array()
) {
+ public static function assertSuperset( $super, $inner, $path = array()
) {
$expected_value = static::array_dereference( $inner, $path );
if ( is_array( $expected_value ) ) {
foreach ( array_keys( $expected_value ) as $key ) {
@@ -21,7 +21,7 @@
return true;
}
- static protected function array_dereference( $root, $path ) {
+ protected static function array_dereference( $root, $path ) {
$cur_path = array();
while ( count( $path ) ) {
$key = array_shift( $path );
@@ -33,4 +33,28 @@
}
return $root;
}
+
+ /**
+ * Match banner allocations arrays, using lenient floating-point
comparison
+ */
+ public static function assertEqualAllocations( $allocations, $expected
) {
+ $delta = 0.001;
+
+ if ( count( $allocations ) != count( $expected ) ) {
+ throw new Exception( "Wrong number of banners, expected
" . json_encode( $expected ) . ", got " . json_encode( $allocations ) );
+ }
+ foreach ( $allocations as $banner ) {
+ $banner_name = $banner['name'];
+ if ( !array_key_exists( $banner_name, $expected ) ) {
+ throw new Exception( "Surprise banner
{$banner_name}" );
+ }
+ $actual_allocation = $banner['allocation'];
+ $expected_allocation = $expected[$banner_name];
+ if ( abs( $actual_allocation - $expected_allocation ) >
$delta ) {
+ throw new Exception( "Allocation for
{$banner_name} should have been {$expected_allocation}, but instead was
{$actual_allocation}" );
+ }
+ }
+
+ return true;
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/172958
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifcc1ae015ec7f0d5789f7cd3a0918d4907a56b1e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits