aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2021-06-28 12:18:30 +0300
committerBonfaceKilz2021-06-29 14:50:30 +0300
commitc1d367b89a95277b25c8e3cffc0b82fb4d081fa0 (patch)
tree310e457df7db0b8527c400cb9a2dd38f14d1f378
parent13f4c5e1000996011b64bdcbb077c42fe83b97d6 (diff)
downloadgenenetwork3-c1d367b89a95277b25c8e3cffc0b82fb4d081fa0.tar.gz
Add a diffing function
For now the diff function uses the Linux tool "diff" to generate the diff since it is efficient and straightforward. * gn3/computations/diff.py (generate_diff): New function. * tests/unit/computations/test_diff.py: Test cases for ☝🏾.
-rw-r--r--gn3/computations/diff.py12
-rw-r--r--tests/unit/computations/test_diff.py28
2 files changed, 40 insertions, 0 deletions
diff --git a/gn3/computations/diff.py b/gn3/computations/diff.py
new file mode 100644
index 0000000..b5da68a
--- /dev/null
+++ b/gn3/computations/diff.py
@@ -0,0 +1,12 @@
+"""This module contains code that's used for generating diffs"""
+from typing import Optional
+
+from gn3.commands import run_cmd
+
+
+def generate_diff(data: str, edited_data: str) -> Optional[str]:
+ """Generate the diff between 2 files"""
+ results = run_cmd(f"diff {data} {edited_data}")
+ if results.get("code", -1) > 0:
+ return results.get("output")
+ return None
diff --git a/tests/unit/computations/test_diff.py b/tests/unit/computations/test_diff.py
new file mode 100644
index 0000000..e4f5dde
--- /dev/null
+++ b/tests/unit/computations/test_diff.py
@@ -0,0 +1,28 @@
+"""This contains unit-tests for gn3.computations.diff"""
+import unittest
+import os
+
+from gn3.computations.diff import generate_diff
+
+TESTDIFF = """3,4c3,4
+< C57BL/6J,x,x,x
+< BXD1,18.700,x,x
+---
+> C57BL/6J,19.000,x,x
+> BXD1,15.700,x,x
+6c6
+< BXD11,18.900,x,x
+---
+> BXD11,x,x,x
+"""
+
+
+class TestDiff(unittest.TestCase):
+ """Test cases for computations.diff"""
+ def test_generate_diff(self):
+ """Test that the correct diff is generated"""
+ data = os.path.join(os.path.dirname(__file__).split("unit")[0],
+ "test_data/trait_data_10007.csv")
+ edited_data = os.path.join(os.path.dirname(__file__).split("unit")[0],
+ "test_data/edited_trait_data_10007.csv")
+ self.assertEqual(generate_diff(data, edited_data), TESTDIFF)