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
No related branches found
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:
orientation: 'vertical'
BoxLayout:
id: fields
orientation: 'vertical'
Fields:
entries: app.entries
size_hint: (1.0, 6.0)
BoxLayout:
orientation: 'horizontal'
......@@ -15,18 +14,19 @@ BoxLayout:
font_size: sp(24)
on_press: app.remove_field()
Label:
id: result
text: 'Smallest Positive: inf'
text: app.result
font_size: sp(24)
<Fields>:
orientation: 'vertical'
<Field>:
orientation: 'horizontal'
Label:
text: root.label_text
text: f'Field #{root.index}: '
font_size: sp(24)
TextInput:
id: input
multiline: False
write_tab: False
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
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.properties import NumericProperty, StringProperty, ListProperty
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):
entries = ListProperty([]) # elements are strings (some may not be valid numbers)
result = StringProperty('Smallest Positive: inf')
def add_field(self):
container = self.root.ids.fields
container.add_widget(Field(label_text=f'Field #{len(container.children)}: '))
self.entries.append('')
def remove_field(self):
container = self.root.ids.fields
if len(container.children) > 0:
container.remove_widget(container.children[0])
try:
self.entries.pop()
except IndexError:
pass
def update(self):
container = self.root.ids.fields
smallest_positive = inf
for field in container.children:
for entry in self.entries:
try:
value = float(field.ids.input.text)
value = float(entry)
if 0 < value < smallest_positive:
smallest_positive = value
except ValueError:
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__':
......
BoxLayout:
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:
id: result
text: f'Smallest Positive: {app.smallest_positive}'
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 kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty, StringProperty
from sqlalchemy.exc import SQLAlchemyError
from values import ValueDatabase, Value
class Field(BoxLayout):
label_text = StringProperty()
from kivy.app import App
from kivy.properties import NumericProperty
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)}: '))
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):
@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 = []
for field in container.children:
try:
self.values.append(float(field.ids.input.text))
except ValueError:
pass
self.smallest_positive = self._find_smallest_positive()
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 = Example2App._find_smallest_positive(values)
if __name__ == '__main__':
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__':
main()
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 values import ValueDatabase, Value
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()
self.assertEqual(actual, 2.0)
def test_database_access(self):
self.fail()
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
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):
......@@ -21,6 +25,9 @@ def main():
value_database.ensure_tables_exist()
print('Tables created.')
session = value_database.create_session()
if already_has_data(session):
print('Not creating records because some already exist.')
else:
add_starter_data(session)
session.commit()
print('Records created.')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment