Skip to content

Commit

Permalink
Merge pull request #249 from datmo/display-snapshot-diff
Browse files Browse the repository at this point in the history
adding config and stats in snapshot diff along with test
  • Loading branch information
asampat3090 committed Aug 4, 2018
2 parents ba46aad + 58147eb commit 5d67119
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
14 changes: 12 additions & 2 deletions datmo/cli/command/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def diff(self, **kwargs):
snapshot_obj_2 = self.snapshot_controller.get(snapshot_id_2)
comparison_attributes = [
"id", "created_at", "message", "label", "code_id",
"environment_id", "file_collection_id"
"environment_id", "file_collection_id", "config", "stats"
]
table_data = [["Attributes", "Snapshot 1", "", "Snapshot 2"],
["", "", "", ""]]
Expand All @@ -282,7 +282,17 @@ def diff(self, **kwargs):
value_1 = prettify_datetime(value_1)
if isinstance(value_2, datetime):
value_2 = prettify_datetime(value_2)
table_data.append([attribute, value_1, "->", value_2])
if attribute in ["config", "stats"]:
alldict = []
if isinstance(value_1, dict): alldict.append(value_1)
if isinstance(value_2, dict): alldict.append(value_2)
allkey = set().union(*alldict)
for key in allkey:
key_value_1 = "%s: %s" % (key, value_1[key]) if value_1.get(key, None) else "N/A"
key_value_2 = "%s: %s" % (key, value_2[key]) if value_2.get(key, None) else "N/A"
table_data.append([attribute, key_value_1, "->", key_value_2])
else:
table_data.append([attribute, value_1, "->", value_2])
output = format_table(table_data)
self.cli_helper.echo(output)
return output
Expand Down
48 changes: 37 additions & 11 deletions datmo/cli/command/tests/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import glob
import time
import json
import tempfile
import platform
from io import open
Expand Down Expand Up @@ -259,9 +260,8 @@ def test_snapshot_create_from_run(self):
test_command = "sh -c 'echo accuracy:0.45'"
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
self.run = RunCommand(self.cli_helper)
self.run.parse([
"run", "--environment-paths", test_dockerfile, test_command
])
self.run.parse(
["run", "--environment-paths", test_dockerfile, test_command])

# test proper execution of task run command
run_obj = self.run.execute()
Expand All @@ -270,8 +270,7 @@ def test_snapshot_create_from_run(self):

# test run id
self.snapshot_command.parse([
"snapshot", "create", "--message", test_message, "--run-id",
run_id
"snapshot", "create", "--message", test_message, "--run-id", run_id
])

# test for desired side effects
Expand All @@ -289,9 +288,8 @@ def test_snapshot_create_from_run_fail_user_inputs(self):
test_command = "sh -c 'echo accuracy:0.45'"
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
self.run = RunCommand(self.cli_helper)
self.run.parse([
"run", "--environment-paths", test_dockerfile, test_command
])
self.run.parse(
["run", "--environment-paths", test_dockerfile, test_command])

# test proper execution of task run command
run_obj = self.run.execute()
Expand Down Expand Up @@ -648,9 +646,8 @@ def test_snapshot_ls_invisible(self):
test_command = "sh -c 'echo accuracy:0.45'"
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
self.run = RunCommand(self.cli_helper)
self.run.parse([
"run", "--environment-paths", test_dockerfile, test_command
])
self.run.parse(
["run", "--environment-paths", test_dockerfile, test_command])

# test proper execution of task run command
task_obj = self.run.execute()
Expand Down Expand Up @@ -784,6 +781,15 @@ def test_snapshot_checkout(self):

def test_snapshot_diff(self):
self.__set_variables()

# Create config file
with open(self.config_filepath, "wb") as f:
f.write(to_bytes(str('{"depth":6}')))

# Create stats file
with open(self.stats_filepath, "wb") as f:
f.write(to_bytes(str('{"acc":0.97}')))

# Create snapshots to test
self.snapshot_command.parse(
["snapshot", "create", "-m", "my test snapshot"])
Expand All @@ -794,6 +800,14 @@ def test_snapshot_diff(self):
with open(self.filepath_3, "wb") as f:
f.write(to_bytes(str("test")))

# Create config file
with open(self.config_filepath, "wb") as f:
f.write(to_bytes(str('{"depth":5}')))

# Create stats file
with open(self.stats_filepath, "wb") as f:
f.write(to_bytes(str('{"acc":0.91}')))

self.snapshot_command.parse(
["snapshot", "create", "-m", "my second snapshot"])
snapshot_obj_2 = self.snapshot_command.execute()
Expand All @@ -804,6 +818,13 @@ def test_snapshot_diff(self):

result = self.snapshot_command.execute()
assert result
assert "message" in result
assert "label" in result
assert "code_id" in result
assert "environment_id" in result
assert "file_collection_id" in result
assert "config" in result
assert "stats" in result

def test_snapshot_inspect(self):
self.__set_variables()
Expand All @@ -816,3 +837,8 @@ def test_snapshot_inspect(self):

result = self.snapshot_command.execute()
assert result
assert "Code" in result
assert "Environment" in result
assert "Files" in result
assert "Config" in result
assert "Stats" in result

0 comments on commit 5d67119

Please sign in to comment.