Unverified Commit aa8cecd4 authored by jlaura's avatar jlaura Committed by GitHub
Browse files

Adds tests for 498 (#502)

* Adds tests for 498

* Sometimes spelling is hard

* Updates for comments
parent c4bd0e69
Loading
Loading
Loading
Loading
+2 −6
Original line number Diff line number Diff line
@@ -1913,10 +1913,7 @@ class NetworkCandidateGraph(CandidateGraph):
            # Create the nodes in the graph. Really, this is creating the
            # images in the DB
            print('loading {} of {}'.format(cnt+1, total))
            image_name = os.path.basename(f)
            node = NetworkNode(image_path=f, image_name=image_name)
            node.parent = self
            node.populate_db()
            self.add_image(f)

        self.from_database()
        # Execute the computation to compute overlapping geometries
@@ -1932,11 +1929,10 @@ class NetworkCandidateGraph(CandidateGraph):
                  absolute path to image
        """
        image_name = os.path.basename(img_path)
        node = NetworkNode(image_path=f, image_name=image_name)
        node = NetworkNode(image_path=img_path, image_name=image_name)
        node.parent = self
        node.populate_db()


    def copy_images(self, newdir):
        """
        Copy images from a given directory into a new directory and
+9 −4
Original line number Diff line number Diff line
@@ -519,15 +519,20 @@ class NetworkNode(Node):
                # Image already exists
                return

        # If the geodata is not valid, do no create an assocaited keypoints file
        #  One instance when invalid is during testing.
        if hasattr(self.geodata, 'file_name'):
            kpspath = io_keypoints.create_output_path(self.geodata.file_name)
            # Create the keypoints entry
            kps = Keypoints(path=kpspath, nkeypoints=0)
        else:
            kps = None

        try:
            fp, cam_type = self.footprint
        except Exception as e:
            warnings.warn('Unable to generate image footprint.\n{}'.format(e))
            fp = None
            fp = cam_type = None
        # Create the image
        i = Images(name=self['image_name'],
                   path=self['image_path'],
+51 −0
Original line number Diff line number Diff line
@@ -61,3 +61,54 @@ def test_to_isis(db_controlnetwork, ncg, node_a, node_b, tmpdir):
    ncg.to_isis(outpath)

    assert os.path.exists(outpath)


def test_from_filelist(session, default_configuration, tmp_path):
    # Written as a list and not parametrized so that the fixture does not automatically clean
    #  up the DB. Needed to test the functionality of the clear_db kwarg.
    for filelist, clear_db in [(['bar1.cub', 'bar2.cub', 'bar3.cub'], False),
                               ([], True),
                               (['bar1.cub', 'bar2.cub', 'bar3.cub'], True)]:
        filelist = [tmp_path/f for f in filelist]

        # Since we have no overlaps (everything is faked), len(ncg) == 0
        ncg = NetworkCandidateGraph.from_filelist(filelist, default_configuration, clear_db=clear_db)
        
        with ncg.session_scope() as session:
            res = session.query(model.Images).all()
            assert len(res) == len(filelist)

def test_global_clear_db(session, ncg):
    i = model.Images(name='foo', path='/fooland/foo.img')
    with ncg.session_scope() as session:
        session.add(i)

        res = session.query(model.Images).all()
        assert len(res) == 1

    ncg.clear_db()

    with ncg.session_scope() as session:
        res = session.query(model.Images).all()
        assert len(res) == 0

def test_selective_clear_db(session, ncg):
    i = model.Images(name='foo', path='fooland/foo.img')
    p = model.Points(pointtype=2)

    with ncg.session_scope() as session:
        session.add(i)
        session.add(p)

        res = session.query(model.Images).all()
        assert len(res) == 1
        res =  session.query(model.Points).all()
        assert len(res) == 1
    
    ncg.clear_db(tables=['Points'])

    with ncg.session_scope() as session:
        res = session.query(model.Images).all()
        assert len(res) == 1
        res = session.query(model.Points).all()
        assert len(res) == 0
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
from autocnet.io.db import adapters  # imported here to get these registered
+13 −0
Original line number Diff line number Diff line
from pathlib import PosixPath
from psycopg2.extensions import register_adapter, AsIs, adapt

# Adaptors as defined by https://www.psycopg.org/docs/advanced.html#adapting-new-python-types-to-sql-syntax

def adapt_posixpath(posixpath):
    stringified = adapt(str(posixpath))
    return stringified

# TODO: Add adaptors from numpy types. We shouldn't have to typecast 
#  explicitly across the code base.

register_adapter(PosixPath, adapt_posixpath)
 No newline at end of file