server/src/tests/Controllers/GeoStatsControllerTest.php
author ymh <ymh.work@gmail.com>
Tue, 20 Mar 2018 15:02:40 +0100
changeset 573 25f3d28f51b2
parent 537 d2e6ee099125
permissions -rw-r--r--
Added tag 0.0.25 for changeset 190ae1dee68d

<?php

class GeoStatsControllerTest extends TestCase
{
   public function testGetIndex()
    {
        $query = [
            'index' => env('ELASTICSEARCH_INDEX'),
            'body' => [
                "size" => 0,
                "query" => [
                    'match_all' => (object) null
                ],
                "aggs" => [
                    "geos" => [
                        "terms" => [
                            "size" => 2147483647,
                            "field" => "geonames_hierarchy"
                        ]
                    ]
                ]
            ]
        ];

        Es::shouldReceive('search')
                    ->once()
                    ->with($query)
                    ->andReturn(json_decode("{
  \"took\" : 17,
  \"timed_out\" : false,
  \"_shards\" : {
    \"total\" : 1,
    \"successful\" : 1,
    \"failed\" : 0
  },
  \"hits\" : {
    \"total\" : 3011,
    \"max_score\" : 0.0,
    \"hits\" : [ ]
  },
  \"aggregations\" : {
    \"geos\" : {
      \"doc_count_error_upper_bound\" : 0,
      \"sum_other_doc_count\" : 0,
      \"buckets\" : [ {
        \"key\" : 6255148,
        \"doc_count\" : 2684
      }, {
        \"key\" : 3017382,
        \"doc_count\" : 2674
      }, {
        \"key\" : 3027939,
        \"doc_count\" : 851
      } ]
    }
  }
}", true));

        $response = $this->get('/api/v1/stats/geostats/');
        $response
            ->assertStatus(200)
            ->assertJson(["geostats" => [
            '6255148' => 2684,
            '3017382' => 2674,
            '3027939' => 851
        ]]);
    }


    public function testGetIndexEarth()
    {
        $query = [
            'index' => env('ELASTICSEARCH_INDEX'),
            'body' => [
                "size" => 0,
                "query" => [
                    'match_all' => (object) null
                ],
                "aggs" => [
                    "geos" => [
                        "terms" => [
                            "size" => 2147483647,
                            "field" => "geonames_hierarchy"
                        ]
                    ]
                ]
            ]
        ];

        Es::shouldReceive('search')
                ->once()
                ->with($query)
                ->andReturn(json_decode("{
  \"took\" : 17,
  \"timed_out\" : false,
  \"_shards\" : {
    \"total\" : 1,
    \"successful\" : 1,
    \"failed\" : 0
  },
  \"hits\" : {
    \"total\" : 3011,
    \"max_score\" : 0.0,
    \"hits\" : [ ]
  },
  \"aggregations\" : {
    \"geos\" : {
      \"doc_count_error_upper_bound\" : 0,
      \"sum_other_doc_count\" : 0,
      \"buckets\" : []
    }
  }
}", true));

        $response = $this->get('/api/v1/stats/geostats/?area='.config('corpusparole.geonames_earth_geonamesid'));
        $response->assertStatus(200);
    }


    public function testGetIndexArea()
    {

        $query = [
            "index" => env('ELASTICSEARCH_INDEX'),
            "body" => [
                "size" => 0,
                "query" => [
                    "constant_score" => [
                        "filter" => [
                            "bool" => [
                                "must" => [ [
                                    "term" => [ "geonames_hierarchy" => "code_area" ]
                                ] ]
                            ]
                        ]
                    ]
                ],
                "aggs" => [
                    "geos" => [
                        "terms" => [
                            "size" => 2147483647,
                            "field" => "geonames_hierarchy"
                        ]
                    ]
                ]
            ]
        ];

        Es::shouldReceive('search')
                ->once()
                ->with($query)
                ->andReturn(json_decode("{
  \"took\" : 17,
  \"timed_out\" : false,
  \"_shards\" : {
    \"total\" : 1,
    \"successful\" : 1,
    \"failed\" : 0
  },
  \"hits\" : {
    \"total\" : 3011,
    \"max_score\" : 0.0,
    \"hits\" : [ ]
  },
  \"aggregations\" : {
    \"geos\" : {
      \"doc_count_error_upper_bound\" : 0,
      \"sum_other_doc_count\" : 0,
      \"buckets\" : []
    }
  }
}", true));

        $response = $this->get('/api/v1/stats/geostats/?area=code_area');
        $response->assertStatus(200);
    }

}