Skip to content
Snippets Groups Projects
Commit 2255eefb authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Refactored code for testability and added example test cases.

parent 844dc097
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ BoxLayout: ...@@ -16,7 +16,7 @@ BoxLayout:
on_press: app.remove_field() on_press: app.remove_field()
Label: Label:
id: result id: result
text: 'Smallest Positive: inf' text: f'Smallest Positive: {app.smallest_positive}'
font_size: sp(24) font_size: sp(24)
<Field>: <Field>:
......
...@@ -2,7 +2,7 @@ from math import inf ...@@ -2,7 +2,7 @@ from math import inf
from kivy.app import App from kivy.app import App
from kivy.uix.boxlayout import BoxLayout from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty from kivy.properties import NumericProperty, StringProperty
class Field(BoxLayout): class Field(BoxLayout):
...@@ -10,6 +10,8 @@ class Field(BoxLayout): ...@@ -10,6 +10,8 @@ class Field(BoxLayout):
class Example1App(App): class Example1App(App):
smallest_positive = NumericProperty(inf)
def add_field(self): def add_field(self):
container = self.root.ids.fields container = self.root.ids.fields
container.add_widget(Field(label_text=f'Field #{len(container.children)}: ')) container.add_widget(Field(label_text=f'Field #{len(container.children)}: '))
...@@ -19,17 +21,23 @@ class Example1App(App): ...@@ -19,17 +21,23 @@ class Example1App(App):
if len(container.children) > 0: if len(container.children) > 0:
container.remove_widget(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): def update(self):
container = self.root.ids.fields container = self.root.ids.fields
smallest_positive = inf values = []
for field in container.children: for field in container.children:
try: try:
value = float(field.ids.input.text) values.append(float(field.ids.input.text))
if 0 < value < smallest_positive:
smallest_positive = value
except ValueError: except ValueError:
pass pass
self.root.ids.result.text = f'Smallest Positive: {smallest_positive}' self.smallest_positive = Example1App._find_smallest_positive(values)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -12,10 +12,6 @@ class Field(BoxLayout): ...@@ -12,10 +12,6 @@ class Field(BoxLayout):
class Example2App(App): class Example2App(App):
smallest_positive = NumericProperty(inf) smallest_positive = NumericProperty(inf)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.values = []
def add_field(self): def add_field(self):
container = self.root.ids.fields container = self.root.ids.fields
container.add_widget(Field(label_text=f'Field #{len(container.children)}: ')) container.add_widget(Field(label_text=f'Field #{len(container.children)}: '))
...@@ -25,22 +21,23 @@ class Example2App(App): ...@@ -25,22 +21,23 @@ class Example2App(App):
if len(container.children) > 0: if len(container.children) > 0:
container.remove_widget(container.children[0]) container.remove_widget(container.children[0])
def _find_smallest_positive(self): @staticmethod
def _find_smallest_positive(values):
result = inf result = inf
for value in self.values: for value in values:
if 0 < value < result: if 0 < value < result:
result = value result = value
return result return result
def update(self): def update(self):
container = self.root.ids.fields container = self.root.ids.fields
self.values = [] values = []
for field in container.children: for field in container.children:
try: try:
self.values.append(float(field.ids.input.text)) values.append(float(field.ids.input.text))
except ValueError: except ValueError:
pass pass
self.smallest_positive = self._find_smallest_positive() self.smallest_positive = self._find_smallest_positive(values)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -19,12 +19,15 @@ class Example3App(App): ...@@ -19,12 +19,15 @@ class Example3App(App):
result = value result = value
return result return result
@staticmethod
def _get_values_from_database(session):
return [record.value for record in session.query(Value).all()]
def on_start(self): def on_start(self):
url = ValueDatabase.construct_mysql_url('localhost', 3306, 'values', 'root', 'cse1208') url = ValueDatabase.construct_mysql_url('localhost', 3306, 'values', 'root', 'cse1208')
value_database = ValueDatabase(url) value_database = ValueDatabase(url)
session = value_database.create_session() session = value_database.create_session()
values = [record.value for record in session.query(Value).all()] self.smallest_positive = Example3App._find_smallest_positive(Example3App._get_values_from_database(session))
self.smallest_positive = Example3App._find_smallest_positive(values)
def main(): def main():
......
...@@ -5,4 +5,5 @@ from example1 import Example1App ...@@ -5,4 +5,5 @@ from example1 import Example1App
class TestExample1App(TestCase): class TestExample1App(TestCase):
def test_update(self): def test_update(self):
self.fail() actual = Example1App._find_smallest_positive([5.0, 2.0, 4.0])
self.assertEqual(actual, 2.0)
...@@ -5,7 +5,5 @@ from example2 import Example2App ...@@ -5,7 +5,5 @@ from example2 import Example2App
class TestExample2App(TestCase): class TestExample2App(TestCase):
def test_find_smallest_positive(self): def test_find_smallest_positive(self):
app = Example2App() actual = Example2App._find_smallest_positive([5.0, 2.0, 4.0])
app.values = [5.0, 2.0, 4.0]
actual = app._find_smallest_positive()
self.assertEqual(actual, 2.0) self.assertEqual(actual, 2.0)
...@@ -7,4 +7,13 @@ from example3 import Example3App ...@@ -7,4 +7,13 @@ from example3 import Example3App
class TestExample3App(TestCase): class TestExample3App(TestCase):
def test_database_access(self): 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])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment