From 627af5e128ecf6690b54a973c89c829eec4125ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mei=C3=9Fner?= <daniel.meissner@inf.h-brs.de>
Date: Tue, 22 Dec 2015 14:20:52 +0100
Subject: [PATCH] introduced basic integration tests and fixed first bugs

---
 .coveragerc                |  2 ++
 .gitignore                 |  1 +
 Makefile                   |  6 ++++++
 README.md                  | 16 ++++++++++++++++
 lib/hardware.py            | 10 +++++-----
 pytest.ini                 |  2 ++
 setup.py                   | 17 +++++++++++++++++
 tests/lib/test_disk.py     | 19 +++++++++++++++++++
 tests/lib/test_hardware.py | 29 +++++++++++++++++++++++++++++
 tests/lib/test_su.py       | 10 ++++++++++
 tests/lib/test_version.py  | 22 ++++++++++++++++++++++
 11 files changed, 129 insertions(+), 5 deletions(-)
 create mode 100644 .coveragerc
 create mode 100644 pytest.ini
 create mode 100644 setup.py
 create mode 100644 tests/lib/test_disk.py
 create mode 100644 tests/lib/test_hardware.py
 create mode 100644 tests/lib/test_su.py
 create mode 100644 tests/lib/test_version.py

diff --git a/.coveragerc b/.coveragerc
new file mode 100644
index 0000000..c712d25
--- /dev/null
+++ b/.coveragerc
@@ -0,0 +1,2 @@
+[run]
+omit = tests/*
diff --git a/.gitignore b/.gitignore
index 2ac52a8..c31a993 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ __pycache__
 *autosave
 *egg-info
 .cache
+.coverage
diff --git a/Makefile b/Makefile
index c302044..eafb574 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,12 @@
 
 echo:
 	@echo "clean | remove pyc files"
+	@echo "test  | run tests with pytest"
+
 
 clean:
 	find . -name "*pyc" -delete
+	find . -name "__pycache__" -delete
+
+test:
+	py.test2 --cov
diff --git a/README.md b/README.md
index 77449c2..e772898 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,22 @@
 * 100 GB of disc space
 * HackRF One, rad1o or RTL-SDR DVB-T stick
 
+## Tests
+
+There are several integration tests available.
+
+To run the tests you have to ensure, that the package is installed or
+egg-infos are present in the project directory to have the right paths for
+includes:
+```
+  pip install -e .
+```
+
+After all paths are clear you are able to run "pytest":http//pytest.org
+```
+  py.test
+```
+
 ## Licence
 
 Copyright (c) 2015, Daniel Meißner <daniel.meissner@smail.inf.h-brs.de>
diff --git a/lib/hardware.py b/lib/hardware.py
index feeec62..1f62bb3 100644
--- a/lib/hardware.py
+++ b/lib/hardware.py
@@ -29,7 +29,7 @@ class Hardware(object):
         lsusb_output = subprocess.check_output("lsusb", shell=True)
         for line in lsusb_output.split("\n"):
             if re.match("^Bus", line):
-                device = self._parse_lsusb_line(line)
+                device = self.parse_lsusb_line(line)
                 devices.append(device)
             else:
                 continue
@@ -44,7 +44,7 @@ class Hardware(object):
 
         connected_devices = []
         for device in devices:
-            if self._supported_device(self, device):
+            if self.supported_device(self, device):
                 connected_devices.append(device)
             else:
                 continue
@@ -52,7 +52,7 @@ class Hardware(object):
         return connected_devices
 
     @staticmethod
-    def _parse_lsusb_line(line):
+    def parse_lsusb_line(line):
         """
         Splitt lsusb line in useful key values.
         """
@@ -60,14 +60,14 @@ class Hardware(object):
         device_unsorted = line.split(" ")
 
         device['bus'] = device_unsorted[1]
-        device['device'] = device_unsorted[3]
+        device['device'] = re.match(r"(?P<id>\d+)", device_unsorted[3]).group()
         device['id'] = device_unsorted[5]
         device['name'] = " ".join(device_unsorted[6:])
 
         return device
 
     @staticmethod
-    def _supported_device(self, device):
+    def supported_device(self, device):
         """
         Check if a given device is supported.
         """
diff --git a/pytest.ini b/pytest.ini
new file mode 100644
index 0000000..12bab15
--- /dev/null
+++ b/pytest.ini
@@ -0,0 +1,2 @@
+[pytest]
+addopts = --cov
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..2036d21
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python2.7
+# -*- encoding: utf-8 -*-
+
+from setuptools import setup
+from lib.version import __version__
+
+setup(name="Spectrum Usage",
+      version=__version__,
+      description="RF application to measure spectrum usage in specific frequencies.",
+      author="Daniel Meißner",
+      author_email="daniel.meissner@smail.inf.h-brs.de",
+      url="https://git.fslab.de/dmeiss2s/su",
+      license="GNU General Public License v3 (GPLv3)",
+      platform=["linux2"],
+      test_suite="tests",
+      classifiers=["Operating System :: POSIX",
+                   "Programming Language :: Python :: 2.7"])
diff --git a/tests/lib/test_disk.py b/tests/lib/test_disk.py
new file mode 100644
index 0000000..9dbfa9b
--- /dev/null
+++ b/tests/lib/test_disk.py
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+
+import pytest
+from os.path import expanduser
+
+from lib.disk import Disk
+
+class TestDisk:
+    def test_free_disk_space(self):
+        assert Disk.free_disk_space("/etca") == None
+        assert type(Disk.free_disk_space(expanduser("~"))) == int
+
+    def test_path_writeble(self):
+        assert Disk.path_writeble("/etca") == False
+        assert Disk.path_writeble("/etc/passwd") == False
+        assert Disk.path_writeble(expanduser("~")) == True
+
+    def test_sizeof_fmt(self):
+        assert Disk.sizeof_fmt(2691532) == "2.6 MiB"
diff --git a/tests/lib/test_hardware.py b/tests/lib/test_hardware.py
new file mode 100644
index 0000000..f8fcdb5
--- /dev/null
+++ b/tests/lib/test_hardware.py
@@ -0,0 +1,29 @@
+# -*- encoding: utf-8 -*-
+
+import pytest
+
+from lib.hardware import Hardware
+
+class TestHardware:
+    def test_parse_lsusb_line(self):
+        line = "Bus 006 Device 035: ID 0bda:2838 Realtek Semiconductor Corp. "\
+               "RTL2838 DVB-T"
+
+        dev = Hardware.parse_lsusb_line(line)
+
+        assert dev["id"] == "0bda:2838"
+        assert dev["name"] == "Realtek Semiconductor Corp. RTL2838 DVB-T"
+        assert dev["bus"] == "006"
+        assert dev["device"] == "035"
+
+    def test_supported_device(self):
+        not_supported_device = {"id": "ffff:ffff"}
+        supported_device = {"id": "0bda:2838"}
+        assert Hardware.supported_device(Hardware(), not_supported_device) \
+                == False
+        assert Hardware.supported_device(Hardware(), supported_device) \
+                == True
+
+    def test_supported_hardware(self):
+        assert type(Hardware.SUPPORTED_HARDWARE) == dict
+        assert len(Hardware.SUPPORTED_HARDWARE) != 0
diff --git a/tests/lib/test_su.py b/tests/lib/test_su.py
new file mode 100644
index 0000000..05e2a7b
--- /dev/null
+++ b/tests/lib/test_su.py
@@ -0,0 +1,10 @@
+# -*- encoding: utf-8 -*-
+
+import pytest
+
+from lib.version import __version__
+
+class TestVersion:
+    def test_version(self):
+        assert type(__version__ ) == str
+        assert len(__version__.split(".")) == 3
diff --git a/tests/lib/test_version.py b/tests/lib/test_version.py
new file mode 100644
index 0000000..13602bd
--- /dev/null
+++ b/tests/lib/test_version.py
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+
+import pytest
+import re
+
+from lib.su import Su
+from lib.version import __version__
+
+class TestSu:
+    def test_version(self):
+        assert __version__ == Su.version()
+        assert type(Su.version()) == str
+
+    def test_name(self):
+        assert type(Su.name()) == str
+
+    def test_short_name(self):
+        assert type(Su.short_name()) == str
+
+    def test_name_and_version(self):
+        assert re.search(Su.name(), Su.name_and_version()) != None
+        assert re.search(Su.version(), Su.name_and_version()) != None
-- 
GitLab