Skip to content

unittest.Test: remove('*.osc'), md5.hexdigest, osc_modif_options.source #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions osc_modif/osc_modif.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
###########################################################################
## Modified: WK-GiHu osc_modif_101.py

import sys, re, urllib, time
from modules import OsmSax
Expand Down Expand Up @@ -102,7 +103,10 @@ def init(self):
import unittest

class Test(unittest.TestCase):

import os
rootPath = "tests"
outPath = os.path.join(rootPath, "out")

def setUp(self):
import os
import shutil
Expand All @@ -111,8 +115,14 @@ def setUp(self):
OsmBin.InitFolder("tmp-osmbin/")
self.osmbin = OsmBin.OsmBin("tmp-osmbin/", "w")
self.osmbin.Import("tests/000.osm")
if not os.path.exists("tests/out"):
os.makedirs("tests/out")

if not os.path.exists( self.outPath ):
os.makedirs( self.outPath )
else:
from modules.helperLib import remove
# remove old <outPath>*.osc files
remove( os.path.join(self.outPath, "*.osc") )
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Class variable to reduce data redundancy.
Outsourced remove("tests/out/*.osc"):


del self.osmbin

def tearDown(self):
Expand All @@ -122,9 +132,9 @@ def tearDown(self):
del self.osmbin
shutil.rmtree("tmp-osmbin/")

def compare_files(self, a, b):
import filecmp
return filecmp.cmp(a, b)
def compare_files(self, aDigest, b):
from modules.helperLib import hash_file
return hash_file(b).cmp(aDigest)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed compare_files to use md5 hexdigest:


def test(self):
class osc_modif_options:
Expand All @@ -136,14 +146,14 @@ class osc_modif_options:
osmbin_path = "tmp-osmbin/"
osc_modif(None, osc_modif_options)

assert self.compare_files("tests/results/001.bbox.osc", "tests/out/001.bbox.osc")
assert self.compare_files('03a720a5b8c79f6c1bc486be5eb1e879', "tests/out/001.bbox.osc")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adapted to new self.compare_files
Parameter 1: A md5 hexdigest, which you can get with command line tool md5sum
Parameter 2: File whose data md5 hexdigest to be compared with give md5 hexdigest

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering: won't it be easier to keep the original file to compare, instead of putting a md5sum ? It would make it very easy to compare the two files, and check where they differ in case of failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is, because you don't answer my question regarding tests/results/*.
For me, the Name tests/results/* is misleading, expected unittest would write to this directory.
How can we be sure that the data in tests/results/* are error free?
Until now these files are only a copy from tests/out/*. Unittest therefore says nothing about valid datas. But regardless of this, I change compare_files(...) to compare_files('001.bbox.osc').

Copy link
Owner

@jocelynj jocelynj Oct 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry about test/results/*. You are right that the name is misleading. This directory is intended to store the expected results of the various tests - and it is expected that files are a copy of tests/out/*, as tests are verifying that the python code is not broken.
Maybe we could rename this directory to tests/expected/* ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about this:
DE:Referenzdaten EN: reference data FR: Données de référence
Directory Name: 'tests/ref'
Summary:
tests - Input data
tests/out - Unittest output
tests/tmp-osmbin - osmbin unittest dbase
tests/tmp-osmosis - osmosis unittest dbase, if any
tests/ref - reference data to diff tests/out if needed

Thinking about your initial question: "...check where they differ in case of failure", using a hash in unitest does not prevent to make a diff.


class osc_modif_options:
source = "tests/results/001.bbox.osc"
source = "tests/out/001.bbox.osc"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error Correction:
The output of the previous bbox run goes to 'tests/out', therefore 'poly' run have to use the new generated file instead of a old in 'tests/results'.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. This explains why the tests were working on my repository, but not on yours.

dest = "tests/out/001.poly.osc"
poly = "tests/polygon.poly"
position_only = False
osmbin_path = "tmp-osmbin/"
osc_modif(None, osc_modif_options)

assert self.compare_files("tests/results/001.poly.osc", "tests/out/001.poly.osc")
assert self.compare_files('e83aa99d6f72111d2a885b4dbd9e607a', "tests/out/001.poly.osc")