Example #1
0
    def test_copiesOf_returns_empty_list_for_size_0_file(self):
        from niprov.querying import Query

        target = Mock()
        target.provenance = {"size": 0}
        results = Query(self.dependencies).copiesOf(target)
        self.assertEqual(0, len(results))
Example #2
0
    def setUp(self):
        super(LoggingTests, self).setUp()
        # self.repo.byLocation.return_value = True
        self.opts = Mock()
        self.opts.dryrun = False
        img = Mock()
        img.provenance = {
            "acquired": dt.now(),
            "subject": "JD",
            "protocol": "X",
            "technique": "abc",
            "repetition-time": 1.0,
            "epi-factor": 1.0,
            "magnetization-transfer-contrast": True,
            "diffusion": True,
            "echo-time": 123,
            "flip-angle": 892,
            "inversion-time": 123,
        }
        self.locationFactory.completeString.side_effect = lambda p: p
        self.repo.byLocation.return_value = img
        self.provenancesAdded = []

        def wrapProv(location, transient=False, provenance=False, dependencies=None):
            self.provenancesAdded.append(provenance)
            self.newimg = Mock()
            self.newimg.provenance = {"parentfield": location}
            return self.newimg

        self.dependencies.reconfigureOrGetConfiguration.return_value = self.opts
        patcher = patch("niprov.plogging.add")
        self.add = patcher.start()
        self.add.side_effect = wrapProv
        self.addCleanup(patcher.stop)
Example #3
0
    def test_Keep_accepts_bsonBinary(self):
        from niprov.pictures import PictureCache

        myImg = Mock()
        myImg.provenance = {"id": "007"}
        pictures = PictureCache(sentinel.dependencies)
        pictures.keep(bson.Binary("/x10/x05/x5f"), for_=myImg)
        self.assertEqual("/x10/x05/x5f", pictures.getBytes(for_=myImg))
Example #4
0
 def test_Inherits_from_parent(self):
     parent = Mock()
     parent.provenance = {"x": 123}
     inprov = {"a": 89}
     self.repo.byLocation.return_value = parent
     self.locationFactory.completeString.side_effect = lambda p: "l:" + p
     img = self.log("new", "trans", "parentpath", provenance=inprov)
     self.inheritFrom.assert_called_with(inprov, parent.provenance)
Example #5
0
 def test_If_no_snapshot_doesnt_add_data_field(self):
     self.pictureCache.getBytes.return_value = None
     with patch("niprov.mongo.bson") as bson:
         self.setupRepo()
         img = Mock()
         img.provenance = {"a": 1}
         self.repo.add(img)
         assert not bson.Binary.called
         self.db.provenance.insert_one.assert_called_with({"a": 1})
Example #6
0
    def test_copiesOf(self):
        from niprov.querying import Query

        target = Mock()
        target.provenance = {"hash": "a7b8c9", "size": 1}
        q = Query(self.dependencies).copiesOf(target)
        self.assertEqual(1, len(q.getFields()))
        self.assertEqual("hash", q.getFields()[0].name)
        self.assertEqual("a7b8c9", q.getFields()[0].value)
Example #7
0
 def test_Found_copy_set_as_parent_inherits_and_flags_and_informs_listener(self):
     self.img.provenance = {}
     copy = Mock()
     copy.provenance = {"location": "copy-location"}
     self.query.copiesOf.return_value = [self.img, copy]
     out = self.add("p/afile.f")
     self.inheritFrom.assert_called_with(self.img.provenance, copy.provenance)
     self.listener.usingCopyAsParent.assert_called_with(copy)
     self.assertEqual(copy.location.toString(), out.provenance["parents"][0])
     self.assertEqual(True, out.provenance["copy-as-parent"])
Example #8
0
    def test_Stored_picture_can_be_retrieved_as_filepath(self):
        from niprov.pictures import PictureCache

        myImg = Mock()
        myImg.provenance = {"id": "007"}
        pictures = PictureCache(sentinel.dependencies)
        self.assertIsNone(pictures.getFilepath(for_=myImg))
        pictures.keep(io.BytesIO("/x10/x05/x5f"), for_=myImg)
        picfpath = os.path.expanduser("~/.niprov-snapshots/007.png")
        self.assertEqual(picfpath, pictures.getFilepath(for_=myImg))
Example #9
0
 def test_keepVersionsFromPrevious(self):
     img = self.constructor(self.path, dependencies=self.dependencies)
     prev = Mock()
     prev.provenance = {"y": 1501, "_versions": [{"y": 1499}, {"y": 1500}]}
     img.keepVersionsFromPrevious(prev)
     self.assertEqual("new-version", img.status)
     self.assertEqual(3, len(img.provenance["_versions"]))
     self.assertEqual({"y": 1501}, img.provenance["_versions"][-1])
     self.assertEqual({"y": 1500}, img.provenance["_versions"][-2])
     self.assertEqual({"y": 1499}, img.provenance["_versions"][-3])
Example #10
0
 def test_Obtains_optional_snapshot_data_from_cache_when_serializing(self):
     self.pictureCache.getBytes.return_value = sentinel.snapbytes
     with patch("niprov.mongo.bson") as bson:
         bson.Binary.return_value = sentinel.snapbson
         self.setupRepo()
         img = Mock()
         img.provenance = {"a": 1}
         self.repo.add(img)
         self.pictureCache.getBytes.assert_called_with(for_=img)
         bson.Binary.assert_called_with(sentinel.snapbytes)
         self.db.provenance.insert_one.assert_called_with({"a": 1, "_snapshot-data": sentinel.snapbson})
Example #11
0
    def test_If_pic_is_not_in_cache_but_on_filesystem_provides_that(self):
        import niprov.pictures

        myImg = Mock()
        myImg.provenance = {"id": "007"}
        pictures = niprov.pictures.PictureCache(sentinel.dependencies)
        pictures.keep(io.BytesIO("/x10/x05/x5f"), for_=myImg)
        pictures.saveToDisk(for_=myImg)
        niprov.pictures._CACHE = {}  # reset cache
        picfpath = os.path.expanduser("~/.niprov-snapshots/007.png")
        self.assertEqual(picfpath, pictures.getFilepath(for_=myImg))
Example #12
0
    def test_Stored_picture_can_be_retrieved_as_bytes(self):
        from niprov.pictures import PictureCache

        myImg = Mock()
        myImg.provenance = {"id": "007"}
        pictures = PictureCache(sentinel.dependencies)
        newPicture = pictures.new()
        newPicture.write("/x10/x05/x5f")
        pictures.keep(newPicture, for_=myImg)
        outBytes = pictures.getBytes(for_=myImg)
        self.assertEqual("/x10/x05/x5f", outBytes)
Example #13
0
    def test_Can_be_told_to_persist_picture_to_disk_now(self):
        from niprov.pictures import PictureCache

        myImg = Mock()
        myImg.provenance = {"id": "007"}
        pictures = PictureCache(sentinel.dependencies)
        pictures.saveToDisk(for_=myImg)  # shouldn't do anything
        pictures.keep(io.BytesIO("/x10/x05/x5f"), for_=myImg)
        pictures.saveToDisk(for_=myImg)
        picfpath = os.path.expanduser("~/.niprov-snapshots/007.png")
        self.assertTrue(os.path.isfile(picfpath))
        with open(picfpath) as picFile:
            self.assertEqual(picFile.read(), "/x10/x05/x5f")
Example #14
0
    def test_updateApproval(self):
        from niprov.jsonfile import JsonFile

        repo = JsonFile(self.dependencies)
        img = Mock()
        img.provenance = {"fiz": "baf", "approval": "notsure"}

        def assertion(img):
            self.assertEqual({"fiz": "baf", "approval": "excellent!"}, img.provenance)

        repo.byLocation = Mock()
        repo.byLocation.return_value = img
        repo.update = Mock()
        repo.update.side_effect = assertion
        repo.updateApproval("/p/f1", "excellent!")
Example #15
0
    def test_fileAdded(self):
        from niprov.commandline import Commandline

        self.dependencies.config.verbosity = "info"
        cmd = Commandline(self.dependencies)
        cmd.log = Mock()
        img = Mock()
        img.path = "xyz"
        img.status = "new"
        cmd.fileAdded(img)
        cmd.log.assert_called_with("info", "New file: xyz")
        img.status = "series-new-file"
        img.provenance = {"filesInSeries": [1, 2, 3]}
        img.getSeriesId.return_value = "series987"
        cmd.fileAdded(img)
        cmd.log.assert_called_with("info", "Added 3rd file to series: series987")
        img.status = "new-version"
        cmd.fileAdded(img)
        cmd.log.assert_called_with("info", "Added new version for: xyz")
Example #16
0
    def test_File(self):
        from niprov.formatnarrated import NarratedFormat

        img = Mock()
        p = {}
        p["protocol"] = "T1"
        p["acquired"] = datetime(2013, 9, 7, 11, 27, 34)
        p["subject"] = "John Doe"
        p["size"] = 345123
        img.provenance = p
        storyteller = NarratedFormat()
        self.assertEqual(
            storyteller.serializeSingle(img),
            (
                "This is a T1 image. "
                "It was recorded September 7, 2013. "
                "The participant's name is John Doe. "
                "It is 345KB in size. "
            ),
        )
Example #17
0
 def fileWithLocationAndParents(self, loc, parents):
     f = Mock()
     f.location.toString.return_value = loc
     f.provenance = {"parents": parents}
     f.parents = parents
     return f
Example #18
0
 def fileWithLocation(self, loc):
     f = Mock()
     f.location.toString.return_value = loc
     f.provenance = {}
     return f
Example #19
0
 def aFile(self):
     somefile = Mock()
     somefile.provenance = {"a": "b"}
     return somefile
Example #20
0
 def aFile(self):
     somefile = Mock()
     somefile.provenance = {}
     somefile.location.toUrl.return_value = 'xkcd://HAL/location.loc'
     return somefile
Example #21
0
 def test_Add(self):
     self.setupRepo()
     img = Mock()
     img.provenance = {"a": 1, "b": 2}
     self.repo.add(img)
     self.db.provenance.insert_one.assert_called_with({"a": 1, "b": 2})
Example #22
0
 def imageWithProvenance(self, prov):
     img = Mock()
     img.provenance = prov
     if "location" in prov:
         img.location.toString.return_value = prov["location"]
     return img
Example #23
0
 def test_update(self):
     self.setupRepo()
     img = Mock()
     img.provenance = {"a": 1, "b": 2}
     self.repo.update(img)
     self.db.provenance.update.assert_called_with({"location": img.location.toString()}, {"a": 1, "b": 2})