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

Condensed examples and included some CPT practice.

parent 844dc097
Branches master
No related tags found
No related merge requests found
# Test Design for `example1.Example1App._find_smallest_positive`
## Categories
* …: …, …, …, …
* …: …, …, …, …
* …: …, …, …, …
* …: …, …, …, …
## Initial Test Frames (by CPT)
* `entries=[…]` (…, …, …, …)
* `entries=[…]` (…, …, …, …)
* `entries=[…]` (…, …, …, …)
* `entries=[…]` (…, …, …, …)
* `entries=[…]` (…, …, …, …)
* `entries=[…]` (…, …, …, …)
* `entries=[…]` (…, …, …, …)
* `entries=[…]` (…, …, …, …)
BoxLayout: BoxLayout:
orientation: 'vertical' orientation: 'vertical'
BoxLayout: Fields:
id: fields entries: app.entries
orientation: 'vertical'
size_hint: (1.0, 6.0) size_hint: (1.0, 6.0)
BoxLayout: BoxLayout:
orientation: 'horizontal' orientation: 'horizontal'
...@@ -15,18 +14,19 @@ BoxLayout: ...@@ -15,18 +14,19 @@ BoxLayout:
font_size: sp(24) font_size: sp(24)
on_press: app.remove_field() on_press: app.remove_field()
Label: Label:
id: result text: app.result
text: 'Smallest Positive: inf'
font_size: sp(24) font_size: sp(24)
<Fields>:
orientation: 'vertical'
<Field>: <Field>:
orientation: 'horizontal' orientation: 'horizontal'
Label: Label:
text: root.label_text text: f'Field #{root.index}: '
font_size: sp(24) font_size: sp(24)
TextInput: TextInput:
id: input
multiline: False multiline: False
write_tab: False write_tab: False
font_size: sp(24) font_size: sp(24)
on_text: app.update() on_text: app.set_entry(root.index, self.text)
...@@ -2,34 +2,58 @@ from math import inf ...@@ -2,34 +2,58 @@ 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, ListProperty
class Field(BoxLayout): class Field(BoxLayout):
label_text = StringProperty() index = NumericProperty()
entry = StringProperty()
class Fields(BoxLayout):
entries = ListProperty([]) # elements are strings (some may not be valid numbers)
def rebuild(self):
while len(self.children) > len(self.entries):
self.remove_widget(self.children[0])
while len(self.children) < len(self.entries):
self.add_widget(Field(index=len(self.children)))
def on_entries(self, _, __):
self.rebuild()
for field, entry in zip(reversed(self.children), self.entries):
field.entry = entry
class Example1App(App): class Example1App(App):
entries = ListProperty([]) # elements are strings (some may not be valid numbers)
result = StringProperty('Smallest Positive: inf')
def add_field(self): def add_field(self):
container = self.root.ids.fields self.entries.append('')
container.add_widget(Field(label_text=f'Field #{len(container.children)}: '))
def remove_field(self): def remove_field(self):
container = self.root.ids.fields try:
if len(container.children) > 0: self.entries.pop()
container.remove_widget(container.children[0]) except IndexError:
pass
def update(self): def update(self):
container = self.root.ids.fields
smallest_positive = inf smallest_positive = inf
for field in container.children: for entry in self.entries:
try: try:
value = float(field.ids.input.text) value = float(entry)
if 0 < value < smallest_positive: if 0 < value < smallest_positive:
smallest_positive = value smallest_positive = value
except ValueError: except ValueError:
pass pass
self.root.ids.result.text = f'Smallest Positive: {smallest_positive}' self.result = f'Smallest Positive: {smallest_positive}'
def on_entries(self, _, __):
self.update()
def set_entry(self, index, entry):
self.entries[index] = entry
if __name__ == '__main__': if __name__ == '__main__':
......
BoxLayout: BoxLayout:
orientation: 'vertical' orientation: 'vertical'
BoxLayout:
id: fields
orientation: 'vertical'
size_hint: (1.0, 6.0)
BoxLayout:
orientation: 'horizontal'
Button:
text: 'Add Field'
font_size: sp(24)
on_press: app.add_field()
Button:
text: 'Remove Field'
font_size: sp(24)
on_press: app.remove_field()
Label: Label:
id: result
text: f'Smallest Positive: {app.smallest_positive}' text: f'Smallest Positive: {app.smallest_positive}'
font_size: sp(24) font_size: sp(24)
<Field>:
orientation: 'horizontal'
Label:
text: root.label_text
font_size: sp(24)
TextInput:
id: input
multiline: False
write_tab: False
font_size: sp(24)
on_text: app.update()
from sys import stderr
from math import inf from math import inf
from kivy.app import App from sqlalchemy.exc import SQLAlchemyError
from kivy.uix.boxlayout import BoxLayout from values import ValueDatabase, Value
from kivy.properties import NumericProperty, StringProperty
class Field(BoxLayout): from kivy.app import App
label_text = StringProperty() from kivy.properties import NumericProperty
class Example2App(App): class Example2App(App):
smallest_positive = NumericProperty(inf) smallest_positive = NumericProperty(inf)
def __init__(self, **kwargs): @staticmethod
super().__init__(**kwargs) def _find_smallest_positive(values):
self.values = []
def add_field(self):
container = self.root.ids.fields
container.add_widget(Field(label_text=f'Field #{len(container.children)}: '))
def remove_field(self):
container = self.root.ids.fields
if len(container.children) > 0:
container.remove_widget(container.children[0])
def _find_smallest_positive(self):
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 on_start(self):
container = self.root.ids.fields url = ValueDatabase.construct_mysql_url('localhost', 3306, 'values', 'root', 'cse1208')
self.values = [] value_database = ValueDatabase(url)
for field in container.children: session = value_database.create_session()
try: values = [record.value for record in session.query(Value).all()]
self.values.append(float(field.ids.input.text)) self.smallest_positive = Example2App._find_smallest_positive(values)
except ValueError:
pass
self.smallest_positive = self._find_smallest_positive() def main():
try:
app = Example2App()
app.run()
except SQLAlchemyError as exception:
print('Database connection failed!', file=stderr)
print(f'Cause: {exception}', file=stderr)
exit(1)
if __name__ == '__main__': if __name__ == '__main__':
app = Example2App() main()
app.run()
BoxLayout:
orientation: 'vertical'
Label:
id: result
text: f'Smallest Positive: {app.smallest_positive}'
font_size: sp(24)
from sys import stderr
from math import inf
from sqlalchemy.exc import SQLAlchemyError
from values import ValueDatabase, Value
from kivy.app import App
from kivy.properties import NumericProperty
class Example3App(App):
smallest_positive = NumericProperty(inf)
@staticmethod
def _find_smallest_positive(values):
result = inf
for value in values:
if 0 < value < result:
result = value
return result
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)
def main():
try:
app = Example3App()
app.run()
except SQLAlchemyError as exception:
print('Database connection failed!', file=stderr)
print(f'Cause: {exception}', file=stderr)
exit(1)
if __name__ == '__main__':
main()
from unittest import TestCase from unittest import TestCase
from values import ValueDatabase, Value
from example2 import Example2App from example2 import Example2App
class TestExample2App(TestCase): class TestExample2App(TestCase):
def test_find_smallest_positive(self): def test_database_access(self):
app = Example2App() self.fail()
app.values = [5.0, 2.0, 4.0]
actual = app._find_smallest_positive()
self.assertEqual(actual, 2.0)
from unittest import TestCase
from values import ValueDatabase, Value
from example3 import Example3App
class TestExample3App(TestCase):
def test_database_access(self):
self.fail()
...@@ -2,7 +2,11 @@ from sys import stderr ...@@ -2,7 +2,11 @@ from sys import stderr
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from values import ValueDatabase, Value from values import ValueDatabase, Persisted, Value
def already_has_data(session):
return any(session.query(table).first() is not None for table in Persisted.metadata.sorted_tables)
def add_starter_data(session): def add_starter_data(session):
...@@ -21,9 +25,12 @@ def main(): ...@@ -21,9 +25,12 @@ def main():
value_database.ensure_tables_exist() value_database.ensure_tables_exist()
print('Tables created.') print('Tables created.')
session = value_database.create_session() session = value_database.create_session()
add_starter_data(session) if already_has_data(session):
session.commit() print('Not creating records because some already exist.')
print('Records created.') else:
add_starter_data(session)
session.commit()
print('Records created.')
except SQLAlchemyError as exception: except SQLAlchemyError as exception:
print('Database setup failed!', file=stderr) print('Database setup failed!', file=stderr)
print(f'Cause: {exception}', file=stderr) print(f'Cause: {exception}', file=stderr)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment