[5ec3a0b] | 1 | import os |
---|
| 2 | from pyexpat import ExpatError |
---|
| 3 | from xml.dom.minidom import Element |
---|
| 4 | |
---|
| 5 | import pytest |
---|
| 6 | |
---|
| 7 | from ow.utilities import slugify, GPXMinidomParser |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | class TestUtilities(object): |
---|
| 11 | |
---|
| 12 | def test_slugify(self): |
---|
| 13 | res = slugify(u'long story SHORT ') |
---|
| 14 | assert res == u'long-story-short' |
---|
| 15 | res = slugify(u'bla \u03ba\u03b1\u03bb\u03ac \u03c0\u03b1\u03c2') |
---|
| 16 | assert res == u'bla-kala-pas' |
---|
| 17 | |
---|
| 18 | def test_slugify_special_chars(self): |
---|
| 19 | res = slugify(u'(r)-[i]\u00AE') |
---|
| 20 | assert res == u'r-i-r' |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | class TestGPXParseMinidom(object): |
---|
| 24 | |
---|
| 25 | def gpx_file(self, filename): |
---|
| 26 | """ |
---|
| 27 | Return the full path to the given filename from the available fixtures |
---|
| 28 | """ |
---|
| 29 | here = os.path.abspath(os.path.dirname(__file__)) |
---|
| 30 | path = os.path.join(here, 'fixtures', filename) |
---|
| 31 | return path |
---|
| 32 | |
---|
| 33 | def test_load_gpx_invalid(self): |
---|
| 34 | gpx_file = self.gpx_file('invalid.gpx') |
---|
| 35 | parser = GPXMinidomParser(gpx_file) |
---|
| 36 | with pytest.raises(ExpatError): |
---|
| 37 | parser.load_gpx() |
---|
| 38 | assert parser.gpx is None |
---|
| 39 | |
---|
| 40 | gpx_files = [ |
---|
| 41 | ('empty.gpx', Element), |
---|
| 42 | ('20131013.gpx', Element), # GPX 1.0 file |
---|
| 43 | ('20160129-with-extensions.gpx', Element), # GPX 1.1 file with ext. |
---|
| 44 | ] |
---|
| 45 | |
---|
| 46 | @pytest.mark.parametrize(('filename', 'expected'), gpx_files) |
---|
| 47 | def test_load_gpx(self, filename, expected): |
---|
| 48 | """ |
---|
| 49 | Loading valid gpx files ends in the gpx attribute of the parser |
---|
| 50 | being a xml.dom.minidom.Element object, not matter if the gpx file |
---|
| 51 | is empty or a 1.0/1.1 gpx file. |
---|
| 52 | """ |
---|
| 53 | gpx_file = self.gpx_file(filename) |
---|
| 54 | parser = GPXMinidomParser(gpx_file) |
---|
| 55 | parser.load_gpx() |
---|
| 56 | assert isinstance(parser.gpx, expected) |
---|
| 57 | |
---|
| 58 | def test_parse_tracks_empty_gpx(self): |
---|
| 59 | gpx_file = self.gpx_file('empty.gpx') |
---|
| 60 | parser = GPXMinidomParser(gpx_file) |
---|
| 61 | parser.load_gpx() |
---|
| 62 | parser.parse_tracks() |
---|
| 63 | assert parser.tracks == {} |
---|
| 64 | |
---|
| 65 | def test_parse_tracks_1_0_gpx(self): |
---|
| 66 | """ |
---|
| 67 | Parsing a GPX 1.0 file with no extensions. The points in the track |
---|
| 68 | contain keys for the well-known extensions (hr, cad, atemp), but their |
---|
| 69 | values are None |
---|
| 70 | """ |
---|
| 71 | gpx_file = self.gpx_file('20131013.gpx') |
---|
| 72 | parser = GPXMinidomParser(gpx_file) |
---|
| 73 | parser.load_gpx() |
---|
| 74 | parser.parse_tracks() |
---|
| 75 | keys = list(parser.tracks.keys()) |
---|
| 76 | assert keys == [u'A ride I will never forget'] |
---|
| 77 | point = parser.tracks[keys[0]][0] |
---|
| 78 | data = ['lat', 'lon', 'ele', 'time'] |
---|
| 79 | for d in data: |
---|
| 80 | assert point[d] is not None |
---|
| 81 | extensions = ['hr', 'cad', 'atemp'] |
---|
| 82 | for e in extensions: |
---|
| 83 | assert point[e] is None |
---|
| 84 | |
---|
| 85 | def test_parse_tracks_1_1_gpx(self): |
---|
| 86 | """ |
---|
| 87 | Parsing a GPX 1.1 file with extensions. The points in the track contain |
---|
| 88 | keys for the well-known extensions (hr, cad, atemp), with the values |
---|
| 89 | taken from the gpx file (although we test only that they are not None) |
---|
| 90 | """ |
---|
| 91 | gpx_file = self.gpx_file('20160129-with-extensions.gpx') |
---|
| 92 | parser = GPXMinidomParser(gpx_file) |
---|
| 93 | parser.load_gpx() |
---|
| 94 | parser.parse_tracks() |
---|
| 95 | keys = list(parser.tracks.keys()) |
---|
| 96 | assert keys == [ |
---|
| 97 | u'Cota counterclockwise + end bonus'] |
---|
| 98 | point = parser.tracks[keys[0]][0] |
---|
| 99 | data = ['lat', 'lon', 'ele', 'time'] |
---|
| 100 | for d in data: |
---|
| 101 | assert point[d] is not None |
---|
| 102 | extensions = ['hr', 'cad', 'atemp'] |
---|
| 103 | for e in extensions: |
---|
| 104 | assert point[e] is not None |
---|