# Path: openaddr/sample.py
# def sample_geojson(stream, max_features):
#     ''' Read a stream of input GeoJSON and return a string with a limited feature count.
#     '''
#     features = list()
# 
#     for feature in stream_geojson(stream):
#         if len(features) == max_features:
#             break
# 
#         features.append(feature)
# 
#     geojson = dict(type='FeatureCollection', features=features)
#     return json.dumps(geojson)
# 
# def stream_geojson(stream):
#     '''
#     '''
#     data = ijson.parse(stream)
# 
#     for (prefix1, event1, value1) in data:
#         if event1 != 'start_map':
#             # A root GeoJSON object is a map.
#             raise ValueError((prefix1, event1, value1))
# 
#         for (prefix2, event2, value2) in data:
#             if event2 == 'map_key' and value2 == 'type':
#                 prefix3, event3, value3 = next(data)
# 
#                 if event3 != 'string' and value3 != 'FeatureCollection':
#                     # We only want GeoJSON feature collections
#                     raise ValueError((prefix3, event3, value3))
# 
#             elif event2 == 'map_key' and value2 == 'features':
#                 prefix4, event4, value4 = next(data)
# 
#                 if event4 != 'start_array':
#                     # We only want lists of features here.
#                     raise ValueError((prefix4, event4, value4))
# 
#                 for (prefix5, event5, value5) in data:
#                     if event5 == 'end_array':
#                         break
# 
#                     # let _build_value() handle the feature.
#                     _data = chain([(prefix5, event5, value5)], data)
#                     feature = _build_value(_data)
#                     yield feature


import json
import unittest
from io import BytesIO
from ..sample import sample_geojson, stream_geojson
from __future__ import absolute_import, division, print_function




class TestSample (unittest.TestCase):

    def test_sample(self):
        geojson_input = b'''{ "type": "FeatureCollection", "features": [
                            { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} },
                            { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] ] }, "properties": { "prop0": "value0", "prop1": 0.0 } },
                            { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }, "properties": { "prop0": "value0", "prop1": {"this": "that"}, "prop2": true, "prop3": null } }
                            ] }'''

        geojson0 = json.loads(sample_geojson(BytesIO(geojson_input), max_features=0))
        self.assertEqual(len(geojson0['features']), 0)

        geojson1 = json.loads(sample_geojson(BytesIO(geojson_input), max_features=1))
        self.assertEqual(len(geojson1['features']), 1)


# Path: openaddr/sample.py
# def sample_geojson(stream, max_features):
#     ''' Read a stream of input GeoJSON and return a string with a limited feature count.
#     '''
#     features = list()
# 
#     for feature in stream_geojson(stream):
#         if len(features) == max_features:
#             break
# 
#         features.append(feature)
# 
#     geojson = dict(type='FeatureCollection', features=features)
#     return json.dumps(geojson)
# 
# def stream_geojson(stream):
#     '''
#     '''
#     data = ijson.parse(stream)
# 
#     for (prefix1, event1, value1) in data:
#         if event1 != 'start_map':
#             # A root GeoJSON object is a map.
#             raise ValueError((prefix1, event1, value1))
# 
#         for (prefix2, event2, value2) in data:
#             if event2 == 'map_key' and value2 == 'type':
#                 prefix3, event3, value3 = next(data)
# 
#                 if event3 != 'string' and value3 != 'FeatureCollection':
#                     # We only want GeoJSON feature collections
#                     raise ValueError((prefix3, event3, value3))
# 
#             elif event2 == 'map_key' and value2 == 'features':
#                 prefix4, event4, value4 = next(data)
# 
#                 if event4 != 'start_array':
#                     # We only want lists of features here.
#                     raise ValueError((prefix4, event4, value4))
# 
#                 for (prefix5, event5, value5) in data:
#                     if event5 == 'end_array':
#                         break
# 
#                     # let _build_value() handle the feature.
#                     _data = chain([(prefix5, event5, value5)], data)
#                     feature = _build_value(_data)
#                     yield feature


# Path: openaddr/tests/sample.py
import json
import unittest
from io import BytesIO
from ..sample import sample_geojson, stream_geojson

from __future__ import absolute_import, division, print_function




class TestSample (unittest.TestCase):

    def test_sample(self):
        geojson_input = b'''{ "type": "FeatureCollection", "features": [
                            { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} },
                            { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] ] }, "properties": { "prop0": "value0", "prop1": 0.0 } },
                            { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }, "properties": { "prop0": "value0", "prop1": {"this": "that"}, "prop2": true, "prop3": null } }
                            ] }'''

        geojson0 = json.loads(sample_geojson(BytesIO(geojson_input), max_features=0))
        self.assertEqual(len(geojson0['features']), 0)

        geojson1 = json.loads(sample_geojson(BytesIO(geojson_input), max_features=1))
        self.assertEqual(len(geojson1['features']), 1)


        geojson2 = json.loads(sample_geojson(BytesIO(geojson_input), max_features=2))