Skip to content
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

Increase test coverage of python tests #1018

Merged
merged 5 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions python/resdata/grid/faults/fault.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def get_neighbor_cells(self):

def get_polyline(self, k):
layer = self[k]
return layer.getPolyline(name="Polyline[%s]" % self.getName())
return layer.getPolyline(name="Polyline[%s]" % self.get_name())

def get_ij_polyline(self, k):
layer = self[k]
Expand Down Expand Up @@ -390,7 +390,7 @@ def extend_to_edge(self, edge, k):
return self.extendToPolyline(edge, k)

def extend_to_b_box(self, bbox, k, start=True):
fault_polyline = self.getPolyline(k)
fault_polyline = self.get_polyline(k)
if start:
p0 = fault_polyline[1]
p1 = fault_polyline[0]
Expand All @@ -402,8 +402,8 @@ def extend_to_b_box(self, bbox, k, start=True):
intersections = GeometryTools.rayPolygonIntersections(p1, ray_dir, bbox)
if intersections:
p2 = intersections[0][1]
if self.getName():
name = "Extend:%s" % self.getName()
if self.get_name():
name = "Extend:%s" % self.get_name()
else:
name = None

Expand Down
159 changes: 109 additions & 50 deletions python/tests/rd_tests/test_faults.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
from unittest import skipIf
import time
import pytest

from resdata import util

from resdata import ResDataType
Expand Down Expand Up @@ -33,11 +33,11 @@ def test_PolylineIJ(self):
nz = 10
grid = GridGenerator.create_rectangular((nx, ny, nz), (0.1, 0.1, 0.1))
f = Fault(grid, "F")
f.addRecord(0, 1, 0, 0, 0, 0, "Y-")
f.addRecord(2, 2, 0, 1, 0, 0, "X-")
f.addRecord(2, 2, 1, 1, 0, 0, "Y")
f.add_record(0, 1, 0, 0, 0, 0, "Y-")
f.add_record(2, 2, 0, 1, 0, 0, "X-")
f.add_record(2, 2, 1, 1, 0, 0, "Y")

pl = f.getIJPolyline(0)
pl = f.get_ij_polyline(0)
self.assertEqual(pl, [(0, 0), (2, 0), (2, 2), (3, 2)])

def test_empty_collection(self):
Expand All @@ -47,13 +47,13 @@ def test_empty_collection(self):
self.assertFalse(faults.hasFault("FX"))

with self.assertRaises(TypeError):
f = faults[[]]
_ = faults[[]]

with self.assertRaises(KeyError):
f = faults["FX"]
_ = faults["FX"]

with self.assertRaises(IndexError):
f = faults[0]
_ = faults[0]

self.assertFalse("NAME" in faults)

Expand Down Expand Up @@ -209,10 +209,10 @@ def test_connect_faults(self):
fault3 = Fault(grid, "Fault3")
fault4 = Fault(grid, "Fault4")

fault1.addRecord(1, 1, 10, grid.getNY() - 1, 0, 0, "X")
fault2.addRecord(5, 10, 15, 15, 0, 0, "Y")
fault3.addRecord(5, 10, 5, 5, 0, 0, "Y")
fault4.addRecord(20, 20, 10, grid.getNY() - 1, 0, 0, "X")
fault1.add_record(1, 1, 10, grid.get_ny() - 1, 0, 0, "X")
fault2.add_record(5, 10, 15, 15, 0, 0, "Y")
fault3.add_record(5, 10, 5, 5, 0, 0, "Y")
fault4.add_record(20, 20, 10, grid.get_ny() - 1, 0, 0, "X")

for other_fault in [fault2, fault3, fault4]:
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -360,10 +360,10 @@ def test_join_faults(self):
fault3 = Fault(grid, "Fault3")
fault4 = Fault(grid, "Fault4")

fault1.addRecord(1, 1, 10, grid.getNY() - 1, 0, 0, "X")
fault2.addRecord(5, 10, 15, 15, 0, 0, "Y")
fault3.addRecord(5, 10, 5, 5, 0, 0, "Y")
fault4.addRecord(20, 20, 10, grid.getNY() - 1, 0, 0, "X")
fault1.add_record(1, 1, 10, grid.get_ny() - 1, 0, 0, "X")
fault2.add_record(5, 10, 15, 15, 0, 0, "Y")
fault3.add_record(5, 10, 5, 5, 0, 0, "Y")
fault4.add_record(20, 20, 10, grid.get_ny() - 1, 0, 0, "X")

rays = fault1.getEndRays(0)
self.assertEqual(rays[0], [(2, 10), (0, -1)])
Expand Down Expand Up @@ -391,10 +391,10 @@ def test_contact(self):
fault3 = Fault(grid, "Fault3")
fault4 = Fault(grid, "Fault4")

fault1.addRecord(1, 1, 10, grid.getNY() - 1, 0, 0, "X")
fault2.addRecord(5, 30, 15, 15, 0, 0, "Y")
fault3.addRecord(2, 10, 9, 9, 0, 0, "Y")
fault4.addRecord(20, 20, 10, grid.getNY() - 1, 0, 0, "X")
fault1.add_record(1, 1, 10, grid.get_ny() - 1, 0, 0, "X")
fault2.add_record(5, 30, 15, 15, 0, 0, "Y")
fault3.add_record(2, 10, 9, 9, 0, 0, "Y")
fault4.add_record(20, 20, 10, grid.get_ny() - 1, 0, 0, "X")

# self.assertFalse( fault1.intersectsFault(fault2 , 0) )
# self.assertFalse( fault2.intersectsFault(fault1 , 0) )
Expand All @@ -421,35 +421,63 @@ def test_iter(self):
def test_fault(self):
f = Fault(self.grid, "NAME")

with self.assertRaises(ValueError):
with pytest.raises(ValueError, match="Invalid face:F"):
# Invalid face
f.addRecord(10, 10, 11, 11, 1, 43, "F")
f.add_record(10, 10, 11, 11, 1, 43, "F")

with self.assertRaises(ValueError):
with pytest.raises(ValueError, match="Invalid I1:-1"):
# Invalid coordinates
f.addRecord(-1, 10, 11, 11, 1, 43, "X")
f.add_record(-1, 10, 11, 11, 1, 43, "X")

with self.assertRaises(ValueError):
with pytest.raises(ValueError, match="Invalid I1:10000"):
# Invalid coordinates
f.addRecord(10000, 10, 11, 11, 1, 43, "X")
f.add_record(10000, 100000, 11, 11, 1, 43, "X")

with self.assertRaises(ValueError):
with pytest.raises(ValueError, match="Invalid I2:10002"):
# Invalid coordinates
f.addRecord(10, 9, 11, 11, 1, 43, "X")
f.add_record(10, 10002, 11, 11, 1, 43, "X")

with self.assertRaises(ValueError):
with pytest.raises(ValueError, match="Invalid J1:-11"):
# Invalid coordinates
f.addRecord(10, 9, 11, 11, 1, 43, "X")
f.add_record(10, 10, -11, 11, 1, 43, "X")

with self.assertRaises(ValueError):
with pytest.raises(ValueError, match="Invalid J2:11110"):
# Invalid coordinates
f.add_record(10, 10, 11, 11110, 1, 43, "X")

with pytest.raises(ValueError, match="Invalid K1:-11"):
# Invalid coordinates
f.add_record(10, 10, 11, 11, -11, 43, "X")

with pytest.raises(ValueError, match="Invalid K2:43000"):
# Invalid coordinates
f.add_record(10, 10, 11, 11, 1, 43000, "X")

with pytest.raises(ValueError, match="Invalid I1 I2 indices"):
# Invalid coordinates
f.add_record(10, 9, 11, 11, 1, 43, "X")

with pytest.raises(ValueError, match="Invalid J1 J2 indices"):
# Invalid coordinates
f.add_record(8, 9, 12, 11, 1, 43, "X")

with pytest.raises(ValueError, match="Invalid K1 K2 indices"):
# Invalid coordinates
f.add_record(8, 9, 11, 11, 100, 43, "X")

with pytest.raises(ValueError, match="For face:X we must have I1 == I2"):
# Invalid coordinates/face combination
f.addRecord(10, 11, 11, 11, 1, 43, "X")
f.add_record(10, 11, 11, 11, 1, 43, "X")

with self.assertRaises(ValueError):
with pytest.raises(ValueError, match="For face:Y we must have J1 == J2"):
# Invalid coordinates/face combination
f.addRecord(10, 11, 11, 12, 1, 43, "Y")
f.add_record(10, 11, 11, 12, 1, 43, "Y")

f.addRecord(10, 10, 0, 10, 1, 10, "X")
with pytest.raises(ValueError, match="For face:K we must have K1 == K2"):
# Invalid coordinates/face combination
f.add_record(10, 11, 11, 12, 1, 43, "K")

f.add_record(10, 10, 0, 10, 1, 10, "X")

def test_segment(self):
s0 = FaultSegment(0, 10)
Expand Down Expand Up @@ -588,10 +616,10 @@ def test_polyline_intersection(self):
fault3 = Fault(grid, "Fault3")
fault4 = Fault(grid, "Fault4")

fault1.addRecord(1, 1, 10, grid.getNY() - 1, 0, 0, "X")
fault2.addRecord(5, 10, 15, 15, 0, 0, "Y")
fault3.addRecord(5, 10, 5, 5, 0, 0, "Y")
fault4.addRecord(20, 20, 10, grid.getNY() - 1, 0, 0, "X")
fault1.add_record(1, 1, 10, grid.get_ny() - 1, 0, 0, "X")
fault2.add_record(5, 10, 15, 15, 0, 0, "Y")
fault3.add_record(5, 10, 5, 5, 0, 0, "Y")
fault4.add_record(20, 20, 10, grid.get_ny() - 1, 0, 0, "X")

polyline = Polyline(init_points=[(4, 4), (8, 4)])
self.assertTrue(fault4.intersectsPolyline(polyline, 0))
Expand Down Expand Up @@ -639,20 +667,26 @@ def test_extend_to_polyline(self):
# o o o o

fault1 = Fault(grid, "Fault")
fault2 = Fault(grid, "Fault2")

fault1.add_record(0, 0, 0, 0, 0, 0, "X-")
fault1.add_record(0, 0, 0, 0, 0, 0, "Y")

fault1.addRecord(0, 0, 0, 0, 0, 0, "X-")
fault1.addRecord(0, 0, 0, 0, 0, 0, "Y")
fault2.add_record(0, 2, 1, 1, 0, 0, "Y")

polyline = CPolyline(init_points=[(0, 2), (3, 2)])
points = fault1.extendToPolyline(polyline, 0)
self.assertEqual(points, [(1, 1), (2, 2)])

end_join = fault1.endJoin(polyline, 0)
self.assertEqual(end_join, [(1, 1), (0, 2)])
points = fault1.extend_to_polyline(polyline, 0)
assert points == [(1, 1), (2, 2)]

end_join = fault1.end_join(polyline, 0)
end_join2 = fault1.end_join(fault2, 0)
assert end_join == [(1, 1), (0, 2)]
assert end_join2 == [(1, 1), (0, 2)]

polyline2 = CPolyline(init_points=[(0.8, 2), (0.8, 0.8)])
end_join = fault1.endJoin(polyline2, 0)
self.assertIsNone(end_join)
end_join = fault1.end_join(polyline2, 0)
assert not end_join

def test_extend_polyline_on(self):
grid = GridGenerator.create_rectangular((3, 3, 1), (1, 1, 1))
Expand All @@ -666,7 +700,7 @@ def test_extend_polyline_on(self):
# o o o o

fault1 = Fault(grid, "Fault")
fault1.addRecord(0, 2, 0, 0, 0, 0, "Y")
fault1.add_record(0, 2, 0, 0, 0, 0, "Y")

polyline0 = CPolyline(init_points=[(0, 2)])
polyline1 = CPolyline(init_points=[(0, 2), (3, 2)])
Expand All @@ -692,7 +726,7 @@ def test_stepped(self):
f.addRecord(2, 2, 0, 0, 1, 1, "Z")
f.addRecord(1, 1, 0, 0, 2, 3, "X")

block_kw = ResdataKW("FAULTBLK", grid.getGlobalSize(), ResDataType.RD_INT)
block_kw = ResdataKW("FAULTBLK", grid.get_global_size(), ResDataType.RD_INT)
block_kw.assign(1)
block_kw[5] = 2
block_kw[11] = 2
Expand Down Expand Up @@ -747,11 +781,36 @@ def test_connectWithPolyline(self):
fault3.addRecord(1, 1, 0, 2, 0, 0, "X")

self.assertIsNone(fault3.connect(fault1, 0))
self.assertIsNone(fault3.connect(fault1.getPolyline(0), 0))
self.assertIsNone(fault3.connect(Polyline(init_points=[(4, 4), (1, 2)]), 0))

intersect = fault2.connect(fault1, 0)
intersect2 = fault2.connect(Polyline(init_points=[(0, 0), (1, 2)]), 0)
self.assertEqual(len(intersect), 2)
self.assertEqual(len(intersect2), 2)
p1 = intersect[0]
p2 = intersect[1]

self.assertEqual(p1, (2, 1))
self.assertEqual(p2, (2, 2))

def test_extend_to_bbox(self):
bbox = [(0, 0), (10, 0), (10, 10), (0, 10)]
grid = GridGenerator.create_rectangular((4, 4, 1), (1, 1, 1))
fault1 = Fault(grid, "Fault1")
fault2 = Fault(grid, "Fault2")
fault1.add_record(0, 3, 1, 1, 0, 0, "Y")
fault2.add_record(1, 1, 0, 1, 0, 0, "X")

with pytest.raises(
Exception, match="Logical error - must intersect with bounding box"
):
fault1.extend_to_b_box(bbox, 0, start=True)

line1 = fault1.extend_to_b_box(bbox, 0, start=False)
assert line1.getName() == "Extend:Fault1"
assert line1 == CPolyline(init_points=[(4, 2), (10, 2)])

line2 = fault2.extend_to_b_box(bbox, 0, start=True)
assert line2.getName() == "Extend:Fault2"
assert line2 == CPolyline(init_points=[(2, 0), (2, 0)])
Loading