From 2255eefba417acfadd1e740e44c44766e1de719d Mon Sep 17 00:00:00 2001 From: Brady James Garvin <bgarvin@cse.unl.edu> Date: Wed, 29 Jul 2020 11:04:17 -0500 Subject: [PATCH] Refactored code for testability and added example test cases. --- example1.kv | 2 +- example1.py | 20 ++++++++++++++------ example2.py | 15 ++++++--------- example3.py | 7 +++++-- test_example1.py | 3 ++- test_example2.py | 4 +--- test_example3.py | 11 ++++++++++- 7 files changed, 39 insertions(+), 23 deletions(-) diff --git a/example1.kv b/example1.kv index 8e850df..2ea9ff2 100644 --- a/example1.kv +++ b/example1.kv @@ -16,7 +16,7 @@ BoxLayout: on_press: app.remove_field() Label: id: result - text: 'Smallest Positive: inf' + text: f'Smallest Positive: {app.smallest_positive}' font_size: sp(24) <Field>: diff --git a/example1.py b/example1.py index f7ee4b1..53d1d42 100644 --- a/example1.py +++ b/example1.py @@ -2,7 +2,7 @@ from math import inf from kivy.app import App from kivy.uix.boxlayout import BoxLayout -from kivy.properties import StringProperty +from kivy.properties import NumericProperty, StringProperty class Field(BoxLayout): @@ -10,6 +10,8 @@ class Field(BoxLayout): class Example1App(App): + smallest_positive = NumericProperty(inf) + def add_field(self): container = self.root.ids.fields container.add_widget(Field(label_text=f'Field #{len(container.children)}: ')) @@ -19,17 +21,23 @@ class Example1App(App): if len(container.children) > 0: container.remove_widget(container.children[0]) + @staticmethod + def _find_smallest_positive(values): + smallest_positive = inf + for value in values: + if 0 < value < smallest_positive: + smallest_positive = value + return smallest_positive + def update(self): container = self.root.ids.fields - smallest_positive = inf + values = [] for field in container.children: try: - value = float(field.ids.input.text) - if 0 < value < smallest_positive: - smallest_positive = value + values.append(float(field.ids.input.text)) except ValueError: pass - self.root.ids.result.text = f'Smallest Positive: {smallest_positive}' + self.smallest_positive = Example1App._find_smallest_positive(values) if __name__ == '__main__': diff --git a/example2.py b/example2.py index 4fbaa09..f2b8544 100644 --- a/example2.py +++ b/example2.py @@ -12,10 +12,6 @@ class Field(BoxLayout): class Example2App(App): smallest_positive = NumericProperty(inf) - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.values = [] - def add_field(self): container = self.root.ids.fields container.add_widget(Field(label_text=f'Field #{len(container.children)}: ')) @@ -25,22 +21,23 @@ class Example2App(App): if len(container.children) > 0: container.remove_widget(container.children[0]) - def _find_smallest_positive(self): + @staticmethod + def _find_smallest_positive(values): result = inf - for value in self.values: + for value in values: if 0 < value < result: result = value return result def update(self): container = self.root.ids.fields - self.values = [] + values = [] for field in container.children: try: - self.values.append(float(field.ids.input.text)) + values.append(float(field.ids.input.text)) except ValueError: pass - self.smallest_positive = self._find_smallest_positive() + self.smallest_positive = self._find_smallest_positive(values) if __name__ == '__main__': diff --git a/example3.py b/example3.py index f6e6627..2673a12 100644 --- a/example3.py +++ b/example3.py @@ -19,12 +19,15 @@ class Example3App(App): result = value return result + @staticmethod + def _get_values_from_database(session): + return [record.value for record in session.query(Value).all()] + def on_start(self): url = ValueDatabase.construct_mysql_url('localhost', 3306, 'values', 'root', 'cse1208') value_database = ValueDatabase(url) session = value_database.create_session() - values = [record.value for record in session.query(Value).all()] - self.smallest_positive = Example3App._find_smallest_positive(values) + self.smallest_positive = Example3App._find_smallest_positive(Example3App._get_values_from_database(session)) def main(): diff --git a/test_example1.py b/test_example1.py index f34301c..76acfbc 100644 --- a/test_example1.py +++ b/test_example1.py @@ -5,4 +5,5 @@ from example1 import Example1App class TestExample1App(TestCase): def test_update(self): - self.fail() + actual = Example1App._find_smallest_positive([5.0, 2.0, 4.0]) + self.assertEqual(actual, 2.0) diff --git a/test_example2.py b/test_example2.py index d401b35..1c99490 100644 --- a/test_example2.py +++ b/test_example2.py @@ -5,7 +5,5 @@ from example2 import Example2App class TestExample2App(TestCase): def test_find_smallest_positive(self): - app = Example2App() - app.values = [5.0, 2.0, 4.0] - actual = app._find_smallest_positive() + actual = Example2App._find_smallest_positive([5.0, 2.0, 4.0]) self.assertEqual(actual, 2.0) diff --git a/test_example3.py b/test_example3.py index b8be0d3..7f1ac0b 100644 --- a/test_example3.py +++ b/test_example3.py @@ -7,4 +7,13 @@ from example3 import Example3App class TestExample3App(TestCase): def test_database_access(self): - self.fail() + url = ValueDatabase.construct_in_memory_url() + database = ValueDatabase(url) + database.ensure_tables_exist() + session = database.create_session() + session.add(Value(value=5.0)) + session.add(Value(value=2.0)) + session.add(Value(value=4.0)) + session.commit() + actual = Example3App._get_values_from_database(session) + self.assertEqual(actual, [5.0, 2.0, 4.0]) -- GitLab