Skip to content

Commit

Permalink
Add tests and pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abduhbm committed Jun 10, 2020
1 parent d650f30 commit 2162f1f
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 245 deletions.
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
@@ -0,0 +1,25 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/asottile/reorder_python_imports
rev: v2.3.0
hooks:
- id: reorder-python-imports

- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.3
hooks:
- id: flake8

- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
- id: black
12 changes: 0 additions & 12 deletions Pipfile

This file was deleted.

161 changes: 0 additions & 161 deletions Pipfile.lock

This file was deleted.

4 changes: 1 addition & 3 deletions README.rst
Expand Up @@ -229,7 +229,7 @@ no_rows):
z_val = z_obj.z_values
print('Z-values shape: ', z_val.shape)
new_zgrid = ZMAPGrid(z_values=z_val, min_x=-630000.0000, max_x=672000.0000,
new_zgrid = ZMAPGrid(z_values=z_val, min_x=-630000.0000, max_x=672000.0000,
min_y=2000000.0000, max_y=2621000.0000)
Expand Down Expand Up @@ -283,5 +283,3 @@ References
* https://lists.osgeo.org/pipermail/gdal-dev/2011-June/029173.html
* https://gist.github.com/wassname/526d5fde3f3cbeb67da8
* Saltus, R.W. and Bird, K.J., 2003. Digital depth horizon compilations of the Alaskan North Slope and adjacent arctic regions. U.S. Geological Survey data release: https://doi.org/10.3133/ofr03230


6 changes: 6 additions & 0 deletions setup.cfg
@@ -0,0 +1,6 @@
[flake8]
ignore = E203
max-line-length = 119

[pep8]
ignore = E265,E501,W504
Empty file added tests/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions tests/conftest.py
@@ -0,0 +1,28 @@
import pytest

from zmapio import ZMAPGrid

z_text = """! File created by DMBTools2.GridFileFormats.ZmapPlusFile
!
@GRID FILE, GRID, 4
20, -9999.0000000, , 7, 1
6, 4, 0, 200, 0, 300
0.0, 0.0, 0.0
@
-9999.0000000 -9999.0000000 3.0000000 32.0000000
88.0000000 13.0000000
-9999.0000000 20.0000000 8.0000000 42.0000000
75.0000000 5.0000000
5.0000000 100.0000000 35.0000000 50.0000000
27.0000000 1.0000000
2.0000000 36.0000000 10.0000000 6.0000000
9.0000000 -9999.0000000
"""


@pytest.fixture
def zmap_object(tmpdir):
x = tmpdir.join("test.dat")
x.write(z_text)
z_obj = ZMAPGrid(x.strpath)
yield z_obj
35 changes: 35 additions & 0 deletions tests/test_export.py
@@ -0,0 +1,35 @@
import json


def test_export_to_csv(zmap_object, tmpdir):
x = tmpdir.join("output.csv")
zmap_object.to_csv(x.strpath)
lines = x.readlines()
assert len(lines) == 24
assert lines[0] == "0.0,300.0,nan\n"


def test_export_to_geojson(zmap_object, tmpdir):
x = tmpdir.join("output.json")
zmap_object.to_geojson(x.strpath)
d = json.load(x)
assert sorted(list(d.keys())) == ["coordinates", "type"]
assert d.get("type") == "MultiPoint"
assert len(d.get("coordinates")) == 24
assert [0.0, 60.0, 88.0] in d.get("coordinates")


def test_export_to_wkt(zmap_object, tmpdir):
x = tmpdir.join("output.wkt")
zmap_object.to_wkt(x.strpath)
with open(x.strpath) as f:
line = f.readline()
assert line.startswith("MULTIPOINT ((0.0000000 300.0000000 nan),")


def test_export_to_wkt_with_precision(zmap_object, tmpdir):
x = tmpdir.join("output.wkt")
zmap_object.to_wkt(x.strpath, precision=2)
with open(x.strpath) as f:
line = f.readline()
assert line.startswith("MULTIPOINT ((0.00 300.00 nan),")
50 changes: 50 additions & 0 deletions tests/test_read.py
@@ -0,0 +1,50 @@
def test_read_comments_section(zmap_object):
assert len(zmap_object.comments) == 2
assert zmap_object.comments[0].startswith(
" File created by DMBTools2.GridFileFormats"
)


def test_columns_rows_count(zmap_object):
assert zmap_object.no_cols == 4
assert zmap_object.no_rows == 6


def test_max_min_values(zmap_object):
assert zmap_object.max_x == 200.0
assert zmap_object.min_x == 0.0
assert zmap_object.max_y == 300.0
assert zmap_object.min_y == 0.0


def test_decimal_places(zmap_object):
assert zmap_object.decimal_places == 7


def test_nodes_per_line(zmap_object):
assert zmap_object.nodes_per_line == 4


def test_null_values(zmap_object):
assert zmap_object.null_value == -9999
assert zmap_object.null_value_2 == ""


def test_start_column(zmap_object):
assert zmap_object.start_column == 1


def test_x_values(zmap_object):
assert zmap_object.x_values.shape == (6, 4)


def test_y_values(zmap_object):
assert zmap_object.y_values.shape == (6, 4)


def test_z_values(zmap_object):
assert zmap_object.z_values.shape == (4, 6)


def test_z_type(zmap_object):
assert zmap_object.z_type == "GRID"
36 changes: 36 additions & 0 deletions tests/test_write.py
@@ -0,0 +1,36 @@
from zmapio import ZMAPGrid

z_text = """!this is
!a test
@test, GRID, 4
20, -9999.0, , 7, 1
6, 4, 0.0, 200.0, 0.0, 300.0
0.0, 0.0, 0.0
@
-9999.0000000 -9999.0000000 3.0000000 32.0000000
88.0000000 13.0000000
-9999.0000000 20.0000000 8.0000000 42.0000000
75.0000000 5.0000000
5.0000000 100.0000000 35.0000000 50.0000000
27.0000000 1.0000000
2.0000000 36.0000000 10.0000000 6.0000000
9.0000000 -9999.0000000
"""


def test_write_zmap_file(zmap_object, tmpdir):
x = tmpdir.join("output.dat")
z = ZMAPGrid(
z_values=zmap_object.z_values, min_x=0.0, max_x=200.0, min_y=0.0, max_y=300.0
)
z.comments = ["this is", "a test"]
z.nodes_per_line = 4
z.field_width = 20
z.decimal_places = 7
z.name = "test"
z.null_value = -9999.0
z.write(x.strpath)

with open(x.strpath) as f:
data = f.read()
assert data == z_text
4 changes: 2 additions & 2 deletions zmapio/__init__.py
@@ -1,2 +1,2 @@
from .zmap import ZMAPGrid
from .__version__ import __version__
from .__version__ import __version__ # noqa: F401
from .zmap import ZMAPGrid # noqa: F401
2 changes: 1 addition & 1 deletion zmapio/__version__.py
@@ -1 +1 @@
__version__ = '0.4.0'
__version__ = "0.5.0"

0 comments on commit 2162f1f

Please sign in to comment.