Repository navigation
Expand file tree
/
Copy pathtest_utils.py
More file actions
112 lines (96 loc) · 3.7 KB
/
Copy pathtest_utils.py
File metadata and controls
112 lines (96 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2015 Planet Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mock import Mock
import pytest
from datetime import datetime
from planet.api import utils
from planet.api import exceptions
from _common import read_fixture
def test_strp_lenient():
for spec in [
'2017-02-02T16:45:43.887484+00:00',
'2017-02-02T16:45:43.887484+00',
'2017-02-02T16:45:43.887484',
'2017-02-02T16:45:43',
'2017-02-02T16:45',
'2017-02-02T16',
'2017-02-02',
]:
p = utils.strp_lenient(spec)
assert p is not None, spec + " failed"
assert datetime.strftime(p, utils._ISO_FMT).startswith(spec)
withz = '2017-02-02T16:45:43Z'
assert utils.strp_lenient(withz) == utils.strp_lenient(withz[:-1])
def test_geometry_from_json():
# base case, no geometry
assert None is utils.geometry_from_json({})
# from an empty feature collection
collection = {'type': 'FeatureCollection', 'features': []}
assert None is utils.geometry_from_json(collection)
# simple geometry, we're guessing by the type property and presence of
# the coordinates property
geom = {'type': 'Polygon', 'coordinates': [1, 2]}
assert geom == utils.geometry_from_json(geom)
# from a feature
feature = {'type': 'Feature', 'geometry': geom}
assert geom == utils.geometry_from_json(feature)
# from a feature collection
collection = {'type': 'FeatureCollection', 'features': [feature]}
assert geom == utils.geometry_from_json(collection)
def test_probably_wkt():
# not wkt
assert not utils.probably_wkt('')
assert not utils.probably_wkt('{ geojson }')
# it is wkt but we don't support it
assert not utils.probably_wkt('POLYHEDRALSURFACE ()')
assert not utils.probably_wkt('TRIANGLE((0 0 0,0 1 0,1 1 0,0 0 0))')
# all valid wkt
wkt = read_fixture('valid-wkt.txt').split('\n')
assert len(wkt) > 0
for valid in wkt:
assert utils.probably_wkt(valid)
def test_probably_geojson():
# nope
assert utils.probably_geojson('') is None
assert utils.probably_geojson('{}') is None
assert utils.probably_geojson({}) is None
assert utils.probably_geojson({'type': 'random'}) is None
# yep
assert utils.probably_geojson({'type': 'Point'}) == {'type': 'Point'}
assert utils.probably_geojson('{"type": "Point"}') == {'type': 'Point'}
def test_check_status():
r = Mock()
r.status_code = 429
r.text = ''
with pytest.raises(exceptions.TooManyRequests):
utils.check_status(r)
r.text = 'exceeded QUOTA dude'
with pytest.raises(exceptions.OverQuota):
utils.check_status(r)
def test_write_to_file(tmpdir):
body = Mock()
body.name = 'foobar'
body.write.return_value = None
utils.write_to_file(str(tmpdir))(body)
path, callback = body.write.call_args[0]
expected = tmpdir.join(body.name)
assert str(expected) == path
assert callback is None
expected.write('')
callback = Mock()
callback.return_value = None
utils.write_to_file(str(tmpdir), overwrite=False)(body)
utils.write_to_file(str(tmpdir), callback=callback, overwrite=False)(body)
assert body.write.call_count == 1
assert callback.call_args[1]['skip'] == body