Skip to content

Commit

Permalink
MAJOR: return text from output generators
Browse files Browse the repository at this point in the history
This also adds tests for all of the returned text
  • Loading branch information
guyfawcus committed Nov 22, 2017
1 parent 35d2a30 commit 8db2be0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
33 changes: 30 additions & 3 deletions archmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import re
from decimal import Decimal
from io import StringIO
from urllib.error import URLError
from urllib.request import urlopen

Expand Down Expand Up @@ -146,6 +147,9 @@ def make_users(parsed_users, output_file, pretty=False):
parsed_users (list): A list of lists, each sub_list should have 4 elements: ``[latitude, longitude, name, comment]``
output_file (open): Location to save the text output
pretty (bool): If set to True, the output "columns" will be aligned and expanded to match the longest element
Returns:
str: The text written to the output file
"""

users = ''
Expand Down Expand Up @@ -197,6 +201,8 @@ def make_users(parsed_users, output_file, pretty=False):
with open(output_file, 'w') as output:
output.write(users)

return users


def make_geojson(parsed_users, output_file):
"""This function reads the user data supplied by ``parsed_users``, it then generates
Expand All @@ -205,6 +211,9 @@ def make_geojson(parsed_users, output_file):
Args:
parsed_users (list): A list of lists, each sub_list should have 4 elements: ``[latitude, longitude, name, comment]``
output_file (open): Location to save the GeoJSON output
Returns:
str: The text written to the output file
"""
geojson = []

Expand All @@ -221,6 +230,8 @@ def make_geojson(parsed_users, output_file):
with open(output_file, 'w') as output:
output.write(geojson_str)

return geojson_str


def make_kml(parsed_users, output_file):
"""This function reads the user data supplied by ``parsed_users``, it then generates
Expand All @@ -229,6 +240,9 @@ def make_kml(parsed_users, output_file):
Args:
parsed_users (list): A list of lists, each sub_list should have 4 elements: ``[latitude, longitude, name, comment]``
output_file (open): Location to save the KML output
Returns:
str: The text written to the output file
"""
kml = Kml()

Expand All @@ -243,6 +257,8 @@ def make_kml(parsed_users, output_file):
featgeom.Feature._id = 0
featgeom.Geometry._id = 0

return kml.kml()


def make_csv(parsed_users, output_file):
"""This function reads the user data supplied by ``parsed_users``, it then generates
Expand All @@ -251,14 +267,25 @@ def make_csv(parsed_users, output_file):
Args:
parsed_users (list): A list of lists, each sub_list should have 4 elements: ``[latitude, longitude, name, comment]``
output_file (open): Location to save the CSV output
Returns:
str: The text written to the output file
"""
with open(output_file, 'w', newline='') as output:
csvwriter = csv.writer(output, quoting=csv.QUOTE_MINIMAL)
csv_file_writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL)

log.info('Making and writing CSV to ' + output_file)
csvwriter.writerow(['Latitude', 'Longitude', 'Name', 'Comment'])
csv_file_writer.writerow(['Latitude', 'Longitude', 'Name', 'Comment'])
for user in parsed_users:
csvwriter.writerow(user)
csv_file_writer.writerow(user)

csv_string = StringIO()
csv_string_writer = csv.writer(csv_string, quoting=csv.QUOTE_MINIMAL, dialect='unix')
csv_string_writer.writerow(['Latitude', 'Longitude', 'Name', 'Comment'])
for user in parsed_users:
csv_string_writer.writerow(user)

return csv_string.getvalue()


def main():
Expand Down
62 changes: 62 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,68 @@ def test_csv(self):
self.assertEqual(sample_csv, output_csv)


class ReturnedTestCase(unittest.TestCase):
"""These tests compare the return values of ``make_geojson()``, ``make_kml()`` and ``make csv()``
with pre-generated versions that have been checked manually, these *sample* files were
generated by running ``archmap.py`` on the stripped-down/handmade ``ArchMap_List-stripped.html'``.
"""

# 'sample_parsed_users.pickle' is a pickled list that was generated with a known good list
# ('get_users' was run on 'sample_users.txt' and the output was pickled)
with open('tests/sample_parsed_users.pickle', 'rb') as pickled_input:
parsed_users = pickle.load(pickled_input)

def setUp(self):
self.sample_users = 'tests/sample-archmap_users.txt'
self.output_users = 'tests/output-archmap_users.txt'
self.sample_pretty_users = 'tests/sample-archmap_pretty_users.txt'
self.output_pretty_users = 'tests/output-archmap_pretty_users.txt'
self.sample_geojson = 'tests/sample-archmap.geojson'
self.output_geojson = 'tests/output-archmap.geojson'
self.sample_kml = 'tests/sample-archmap.kml'
self.output_kml = 'tests/output-archmap.kml'
self.sample_csv = 'tests/sample-archmap.csv'
self.output_csv = 'tests/output-archmap.csv'

# Set 'maxDiff' to 'None' to be able to see long diffs when something goes wrong.
self.maxDiff = None

def test_users(self):
returned_users = archmap.make_users(self.parsed_users, self.output_users)

with open(self.sample_users, 'r') as file:
sample_users = file.read()
self.assertEqual(sample_users, returned_users)

def test_pretty_users(self):
returned_pretty_users = archmap.make_users(self.parsed_users, self.output_pretty_users, pretty=True)

with open(self.sample_pretty_users, 'r') as file:
sample_pretty_users = file.read()
self.assertEqual(sample_pretty_users, returned_pretty_users)

def test_geojson(self):
returned_geojson = archmap.make_geojson(self.parsed_users, self.output_geojson)

with open(self.sample_geojson, 'r') as file:
sample_geojson = file.read()
self.assertEqual(sample_geojson, returned_geojson)

def test_kml(self):
returned_kml = archmap.make_kml(self.parsed_users, self.output_kml)

with open(self.sample_kml, 'r') as file:
sample_kml = file.read()
self.assertEqual(sample_kml, returned_kml)

def test_csv(self):
returned_csv = archmap.make_csv(self.parsed_users, self.output_csv)

with open(self.sample_csv, 'r') as file:
sample_csv = file.read()
self.assertEqual(sample_csv, returned_csv)


class InteractiveTestCase(unittest.TestCase):
"""These tests test the interactive part of the script - the "main()" function
"""
Expand Down

0 comments on commit 8db2be0

Please sign in to comment.