Ejegg has submitted this change and it was merged.
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(+), 25 deletions(-)
Approvals:
Ejegg: Looks good to me, approved
diff --git a/CentralNotice.hooks.php b/CentralNotice.hooks.php
index eefac7c..41e1e56 100644
--- a/CentralNotice.hooks.php
+++ b/CentralNotice.hooks.php
@@ -365,6 +365,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 89716b2..dcbf0c0 100644
--- a/tests/CentralNoticeTestFixtures.php
+++ b/tests/CentralNoticeTestFixtures.php
@@ -8,7 +8,7 @@
// Use exactly the api defaults where available
static public $defaultCampaign;
static public $defaultBanner;
-
+
function __construct() {
$this->user = User::newFromName( 'UTSysop' );
@@ -16,11 +16,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(),
);
@@ -34,6 +34,22 @@
'campaign_z_index' => 0,
'weight' => 25,
);
+ }
+
+ 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 ) {
@@ -145,10 +161,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(),
@@ -174,6 +190,7 @@
'geo_countries' => array(
'FR',
'GR',
+ 'XX',
),
'banners' => array(
array(
@@ -196,6 +213,7 @@
'countries' => array(
'FR',
'GR',
+ 'XX',
),
'banners' => array(
array(
@@ -209,6 +227,10 @@
),
),
),
+ ),
+ //alloc
+ array(
+ 'b1' => '0.500',
),
);
}
@@ -273,6 +295,13 @@
),
),
),
+ //alloc
+ array(
+ 'c1b1' => '0.300',
+ 'c1b2' => '0.300',
+ 'c2b1' => '0.200',
+ 'c2b2' => '0.200',
+ ),
);
}
@@ -319,10 +348,15 @@
),
),
),
+ //alloc
+ array(
+ 'b1' => '0.024',
+ 'b2' => '0.488',
+ 'b3' => '0.488',
+ ),
);
}
- // FIXME: unused
static function blanksScenario() {
return array(
// input
@@ -331,7 +365,9 @@
array(
'throttle' => 10,
'banners' => array(
- array(),
+ array(
+ 'name' => 'b1',
+ ),
),
),
),
@@ -339,13 +375,20 @@
// expected
array(
array(
- 'slots' => 3,
+ 'banners' => array(
+ array(
+ 'name' => 'b1',
+ ),
+ ),
),
- )
+ ),
+ //alloc
+ array(
+ 'b1' => 0.100,
+ ),
);
}
- // FIXME: unused
static function priorityScenario() {
return array(
// input
@@ -355,14 +398,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',
+ ),
),
),
),
@@ -370,18 +417,21 @@
// expected
array(
array(
- 'campaign' => 'c1',
- 'slots' => 0,
+ 'name' => 'c1',
),
array(
- 'campaign' => 'c2',
- 'slots' => 30,
+ 'name' => 'c2',
),
+ ),
+ //alloc
+ array(
+ 'c1b1' => 0.000,
+ 'c2b1' => 1.000,
),
);
}
- static function geoInUsaScenario() {
+ static function geoInCountryScenario() {
return array(
// input
array(
@@ -390,7 +440,7 @@
'geotargetted' => true,
'geo_countries' => array(
'FR',
- 'US',
+ 'XX',
),
'banners' => array(
array(
@@ -407,7 +457,7 @@
'geotargetted' => true,
'countries' => array(
'FR',
- 'US',
+ 'XX',
),
'banners' => array(
array(
@@ -417,10 +467,14 @@
),
),
),
+ //alloc
+ array(
+ 'b1' => 1.000,
+ ),
);
}
- static function geoNotInUsaScenario() {
+ static function geoNotInCountryScenario() {
return array(
// input
array(
@@ -456,6 +510,9 @@
),
),
),
+ //alloc
+ array(
+ ),
);
}
@@ -476,6 +533,9 @@
),
),
// expected
+ array(
+ ),
+ //alloc
array(
),
);
@@ -500,6 +560,9 @@
// expected
array(
),
+ //alloc
+ array(
+ ),
);
}
@@ -520,6 +583,9 @@
),
),
// expected
+ array(
+ ),
+ //alloc
array(
),
);
@@ -545,6 +611,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: merged
Gerrit-Change-Id: Ifcc1ae015ec7f0d5789f7cd3a0918d4907a56b1e
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: Mwalker <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits