Making WordPress.org

Changeset 6076


Ignore:
Timestamp:
11/05/2017 03:06:14 AM (7 years ago)
Author:
iandunn
Message:

Events: Add tests for get_events() and build_response().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r6075 r6076  
    1919    $tests_failed = 0;
    2020    $tests_failed += test_get_location();
     21    $tests_failed += test_get_events();
     22    $tests_failed += test_build_response();
    2123    $query_count  = count( $wpdb->queries );
    2224    $query_time   = array_sum( array_column( $wpdb->queries, 1 ) );
     
    4042 */
    4143function output_results( $case_id, $passed, $expected_result, $actual_result ) {
     44    if ( $passed ) {
     45        return;
     46    }
     47
    4248    printf(
    4349        "\n* %s: %s",
     
    844850
    845851/**
     852 * Test `get_events()`
     853 *
     854 * @return bool The number of failures
     855 */
     856function test_get_events() {
     857    $failed = 0;
     858    $cases  = get_events_test_cases();
     859
     860    printf( "\n\nRunning %d events tests\n", count( $cases ) );
     861
     862    foreach ( $cases as $case_id => $case ) {
     863        $actual_result = get_events( $case['input'] );
     864
     865        $passed = $case['expected']['count'] === count( $actual_result ) &&
     866            ! empty( $actual_result[0]['url'] ) &&
     867            strtotime( $actual_result[0]['date'] ) > time() - ( 2 * 24 * 60 * 60 ) &&
     868            $case['expected']['country'] === strtoupper( $actual_result[0]['location']['country'] );
     869
     870        output_results( $case_id, $passed, $case['expected'], $actual_result );
     871
     872        if ( ! $passed ) {
     873            $failed++;
     874        }
     875    }
     876
     877    return $failed;
     878}
     879
     880/**
     881 * Get the cases for testing `get_location()`
     882 *
     883 * @return array
     884 */
     885function get_events_test_cases() {
     886    $cases = array(
     887        '2-near-mumbai' => array(
     888            'input' => array(
     889                'number' => '2',
     890                'nearby' => array(
     891                    'latitude'  => '19.024704',
     892                    'longitude' => '72.853966',
     893                ),
     894            ),
     895            'expected' => array(
     896                'count'   => 2,
     897                'country' => 'IN',
     898            ),
     899        ),
     900
     901        '1-in-australia' => array(
     902            'input' => array(
     903                'number' => '1',
     904                'country' => 'AU',
     905            ),
     906            'expected' => array(
     907                'count'   => 1,
     908                'country' => 'AU',
     909            ),
     910        ),
     911    );
     912
     913    return $cases;
     914}
     915
     916/**
     917 * Test `build_response()`
     918 *
     919 * @todo It might be better to do more abstracted tests of `main()`, rather than coupling to the
     920 *       internals of `build_request()`.
     921 *
     922 * @return bool The number of failures
     923 */
     924function test_build_response() {
     925    $failed = 0;
     926    $cases  = build_response_test_cases();
     927
     928    printf( "\n\nRunning %d build_response() tests\n", count( $cases ) );
     929
     930    foreach ( $cases as $case_id => $case ) {
     931        $actual_result = build_response( $case['input']['location'], $case['input']['location_args'] );
     932
     933        $passed = $case['expected']['location'] === $actual_result['location'] &&
     934            isset( $case['expected']['error'] ) === isset( $actual_result['error'] );
     935
     936        if ( $passed && $case['expected']['events'] ) {
     937            $passed = ! empty( $actual_result['events'] ) &&
     938                ! empty( $actual_result['events'][0]['url'] ) &&
     939                strtotime( $actual_result['events'][0]['date'] ) > time() - ( 2 * 24 * 60 * 60 );
     940        }
     941
     942        if ( $passed && isset( $case['expected']['error'] ) ) {
     943            $passed = $case['expected']['error'] === $actual_result['error'];
     944        }
     945
     946        output_results( $case_id, $passed, $case['expected'], $actual_result );
     947
     948        if ( ! $passed ) {
     949            $failed++;
     950        }
     951    }
     952
     953    return $failed;
     954}
     955
     956/**
     957 * Get the cases for testing `build_response()`
     958 *
     959 * @return array
     960 */
     961function build_response_test_cases() {
     962    $cases = array(
     963        'utrecht-ip' => array(
     964            'input' => array(
     965                'location' => array(
     966                    'latitude'  => '52.090284',
     967                    'longitude' => '5.124719',
     968                    'internal'  => true,
     969                ),
     970                'location_args' => array( 'ip' => '84.31.177.21' ),
     971            ),
     972            'expected' => array(
     973                'location' => array(
     974                    'ip' => '84.31.177.21',
     975                ),
     976                'events' => true,
     977            ),
     978        ),
     979
     980        'canada-country' => array(
     981            'input' => array(
     982                'location' => array(
     983                    'country' => 'CA',
     984                ),
     985            ),
     986            'expected' => array(
     987                'location' => array(
     988                    'country' => 'CA',
     989                ),
     990                'events' => true,
     991            ),
     992        ),
     993
     994        'throttled' => array(
     995            'input' => array(
     996                'location' => 'temp-request-throttled',
     997            ),
     998            'expected' => array(
     999                'location' => array(),
     1000                'error'    => 'temp-request-throttled',
     1001                'events'   => false,
     1002            ),
     1003        ),
     1004
     1005        'no-location' => array(
     1006            'input' => array(
     1007                'location' => array(),
     1008            ),
     1009            'expected' => array(
     1010                'location' => array(),
     1011                'error'    => 'no_location_available',
     1012                'events'   => false,
     1013            ),
     1014        ),
     1015    );
     1016
     1017    return $cases;
     1018}
     1019
     1020/**
    8461021 * Stub to simulate cache misses, so that the tests always get fresh results
    8471022 *
Note: See TracChangeset for help on using the changeset viewer.