class TestCachedMetrics(TestDvc): def _do_write(self, branch): self.dvc.scm.checkout(branch) self.dvc.checkout(force=True) with open('metrics.json', 'w+') as fd: json.dump({'metrics': branch}, fd) stages = self.dvc.add('metrics.json') self.assertEqual(len(stages), 1) stage = stages[0] self.assertNotEqual(stage, None) self.dvc.scm.add(['.gitignore', 'metrics.json.dvc']) self.dvc.scm.commit(branch) def test(self): self.dvc.scm.commit('init') self.dvc.scm.branch('one') self.dvc.scm.branch('two') self._do_write('master') self._do_write('one') self._do_write('two') self.dvc = Project('.') res = self.dvc.metrics_show('metrics.json', all_branches=True, typ='json', xpath='metrics') self.assertEqual( res, { "master": { "metrics.json": ["master"] }, "one": { "metrics.json": ["one"] }, "two": { "metrics.json": ["two"] } })
class TestCachedMetrics(TestDvc): def _do_write(self, branch): self.dvc.scm.checkout(branch) self.dvc.checkout(force=True) with open("metrics.json", "w+") as fd: json.dump({"metrics": branch}, fd) stages = self.dvc.add("metrics.json") self.assertEqual(len(stages), 1) stage = stages[0] self.assertNotEqual(stage, None) self.dvc.scm.add([".gitignore", "metrics.json.dvc"]) self.dvc.scm.commit(branch) def test(self): self.dvc.scm.commit("init") self.dvc.scm.branch("one") self.dvc.scm.branch("two") self._do_write("master") self._do_write("one") self._do_write("two") self.dvc = Project(".") res = self.dvc.metrics_show( "metrics.json", all_branches=True, typ="json", xpath="metrics" ) self.assertEqual( res, { "master": {"metrics.json": ["master"]}, "one": {"metrics.json": ["one"]}, "two": {"metrics.json": ["two"]}, }, )
class TestCachedMetrics(TestDvc): def _do_add(self, branch): self.dvc.scm.checkout(branch) self.dvc.checkout(force=True) with open("metrics.json", "w+") as fd: json.dump({"metrics": branch}, fd) stages = self.dvc.add("metrics.json") self.assertEqual(len(stages), 1) stage = stages[0] self.assertIsNotNone(stage) self.dvc.scm.add([".gitignore", "metrics.json.dvc"]) self.dvc.scm.commit(branch) def _do_run(self, branch): self.dvc.scm.checkout(branch) self.dvc.checkout(force=True) with open("code.py", "w+") as fobj: fobj.write("import sys\n") fobj.write("import os\n") fobj.write("import json\n") fobj.write('print(json.dumps({{"metrics": "{branch}"}}))\n'.format( branch=branch)) stage = self.dvc.run( deps=["code.py"], metrics=["metrics.json"], cmd="python code.py metrics.json > metrics.json", ) self.assertIsNotNone(stage) self.assertEqual(stage.relpath, "metrics.json.dvc") self.dvc.scm.add([".gitignore", "metrics.json.dvc"]) self.dvc.scm.commit(branch) def _test_metrics(self, func): self.dvc.scm.commit("init") self.dvc.scm.branch("one") self.dvc.scm.branch("two") func("master") func("one") func("two") self.dvc = Project(".") res = self.dvc.metrics_show("metrics.json", all_branches=True, typ="json", xpath="metrics") self.assertEqual( res, { "master": { "metrics.json": ["master"] }, "one": { "metrics.json": ["one"] }, "two": { "metrics.json": ["two"] }, }, ) def test_add(self): self._test_metrics(self._do_add) def test_run(self): self._test_metrics(self._do_run)