Skip to content

Commit

Permalink
Organized code and fixed tests for new game strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
dversoza committed Jun 28, 2021
1 parent 82867f5 commit 74bb97b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 169 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import tkinter as tk
from tkinter.messagebox import showinfo

from akifood import Game
from models import DishAdjective, Dish
from src.akifood import Game
from src.models import DishAdjective, Dish

DISHES = [
Dish(name='Lasanha', dish_adjectives_id=[1]),
Expand Down
Empty file added src/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions akifood.py → src/akifood.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from tkinter.messagebox import askyesno
from tkinter.simpledialog import askstring

from models import DishAdjective, Dish
from .models import DishAdjective, Dish


class Game:
def __init__(self, root) -> None:
def __init__(self, root=None) -> None:
self.game_name = 'Jogo Gourmet'
self.root = root
pass
Expand Down
File renamed without changes.
205 changes: 40 additions & 165 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,175 +1,50 @@
import unittest
from akifood import DatabaseRepository, Game


class TestDatabaseRepository(unittest.TestCase):
def __init__(self, methodName: str) -> None:
super().__init__(methodName=methodName)
self.database = DatabaseRepository()

def test_database_has_default_data(self):
self.assertIsNotNone(self.database.dishes,
'Database has no initial data !')

def test_database_creates_dishes(self):
fake_dish = {
'_name': 'Macarrão',
'massa': True,
'Italiano': True
}

self.database.create_dish(fake_dish)
self.assertIn(fake_dish, self.database.dishes,
'Database is not creating dishes!')

def test_database_retrieves_dishes_names(self):
fake_dish = {
'_name': 'Macarrão',
'massa': True,
'Italiano': True
}

self.database.create_dish(fake_dish)
dishes_names = self.database.get_dishes_names()
self.assertIn(fake_dish['_name'], dishes_names,
'Database name retrieving not working!')

def test_database_retrieves_all_adjectives(self):
error_msg = 'Database adjectives retrieving not working!'

fake_dish = {
'_name': 'Macarrão',
'massa': True,
'Italiano': True
}

self.database.create_dish(fake_dish)
adjectives = self.database.get_adjectives()
self.assertIn('massa', adjectives, error_msg)
self.assertIn('Italiano', adjectives, error_msg)
self.assertGreaterEqual(len(adjectives), 2, error_msg)

def test_database_retrieves_dish_adjectives(self):
error_msg = 'Database not retrieving a specific dish adjective!'

fake_dish = {
'_name': 'Macarrão',
'massa': True,
'Italiano': True
}

self.database.create_dish(fake_dish)
adjectives = self.database.get_dish_adjectives(fake_dish['_name'])
self.assertIn('massa', adjectives, error_msg)
self.assertIn('Italiano', adjectives, error_msg)
self.assertGreaterEqual(len(adjectives), 2, error_msg)

def test_database_updates_existing_dish_instead_of_duplicating_it(self):
error_msg = 'Existing duplicates. Database not updating dishes, but duplicating it.'

fake_dish = {
'_name': 'Macarrão',
'massa': True,
'Italiano': True
}

self.database.create_dish(fake_dish)

fake_dish_update = {
'_name': 'Macarrão',
'massa': True,
'Italiano': True,
'bom': True
}

self.database.create_dish(fake_dish_update)

self.assertIn(fake_dish_update, self.database.dishes, error_msg)
self.assertNotIn(fake_dish, self.database.dishes, error_msg)

dish_entries = [
x for x in self.database.dishes if x['_name'] == 'Macarrão']
self.assertEqual(len(dish_entries), 1, error_msg)

def test_database_remove_dish(self):
error_msg = 'Database not removing dishes!'

fake_dish = {
'_name': 'Macarrão',
'massa': True,
'Italiano': True
}

self.database.create_dish(fake_dish)
self.database.remove_dish(fake_dish)
self.assertNotIn(fake_dish, self.database.dishes, error_msg)

def test_database_completes_dish_adjectives(self):
error_msg = 'Database not completing dish adjectives!'

fake_dish = {
'_name': 'Macarrão',
'massa': True,
'Italiano': True
}

self.database.create_dish(fake_dish)

fake_dish_extra_adjectives = {
'_name': 'Macarrão',
'bom': True,
'Japonês': False
}

result = self.database.complete_dish_adjectives(
fake_dish_extra_adjectives)
self.assertDictEqual(result, fake_dish | fake_dish_extra_adjectives,
error_msg)
from src.models import Dish, DishAdjective
from src.akifood import Game


class TestGame(unittest.TestCase):
def __init__(self, methodName: str) -> None:
super().__init__(methodName=methodName)
database = DatabaseRepository()
self.game = Game(database)

def test_game_starts_with_default_values(self):
error_msg = 'Game starting with no {} default values!'
self.assertIsNotNone(self.game.possible_dishes,
error_msg.format('possible_dishes'))
self.assertIsNotNone(self.game.adjectives_to_ask,
error_msg.format('adjectives_to_ask'))

def test_game_filters_dishes_according_to_user_input(self):
error_msg = 'Game not filtering dishes'
dish_1 = {'_name': 'Sushi', 'Japonês': True, 'Italiano': False}
dish_2 = {'_name': 'Macarrão', 'Japonês': False, 'Italiano': True}

self.game.possible_dishes = [dish_1, dish_2]

self.game.filter_possible_dishes_according_to_user_input(
'Italiano', False)
self.assertNotIn(dish_2, self.game.possible_dishes, error_msg)
self.assertIn(dish_1, self.game.possible_dishes, error_msg)

def test_game_appends_adjective_to_asked_adjectives(self):
error_msg = "Game appending adjectives to it's state"
adjective, situation = ('Italiano', False)
self.game.append_adjective_to_asked_adjectives(adjective, situation)
self.assertIn(adjective, self.game.asked_adjectives, error_msg)
self.assertEqual(
situation, self.game.asked_adjectives[adjective], error_msg)

def test_game_removes_attempted_adjectives(self):
error_msg = "Game is not removing a attempted adjective!"
adjective, situation = ('Italiano', False)
self.game.append_adjective_to_asked_adjectives(adjective, situation)
self.assertNotIn(adjective, self.game.adjectives_to_ask, error_msg)

def test_game_returns_tuple_as_next_step(self):
response = self.game.next_step()
self.assertIsInstance(
response, tuple, 'Game not giving tuple as next step.')
def setUp(self) -> None:
self.TEST_DISHES = [
Dish(name='Sushi', dish_adjectives_id=[2]),
Dish(name='Brigadeiro', dish_adjectives_id=[]),
Dish(name='Pizza de calabresa', dish_adjectives_id=[1])
]

self.TEST_ADJECTIVES = [
DishAdjective(_id=1, name='massa'),
DishAdjective(_id=2, name='salgado')
]

self.game = Game()
return super().setUp()

def tearDown(self) -> None:
return super().tearDown()

def test_filter_with_adjective(self) -> None:
filtered_dishes = self.game.filter_with_adjective(1, self.TEST_DISHES)
self.assertEqual(len(filtered_dishes), 1)

def test_filter_without_adjective(self) -> None:
filtered_dishes = self.game.filter_without_adjective(
1, self.TEST_DISHES)
self.assertEqual(len(filtered_dishes), 2)

def test_create_dish_adjective(self) -> None:
new_dish_adjective = self.game.create_dish_adjective(5, 'doce')
self.assertIsInstance(new_dish_adjective, DishAdjective)
self.assertEqual(new_dish_adjective.name, 'doce')

def test_create_dish(self) -> None:
new_dish = self.game.create_dish(name='Salada', adjectives=[2])
self.assertIsInstance(new_dish, Dish)
self.assertEqual(new_dish.name, 'Salada')
self.assertEqual(new_dish.dish_adjectives_id, [2])
self.assertIsInstance(new_dish.dish_adjectives_id, list)


if __name__ == '__main__':
Expand Down

0 comments on commit 74bb97b

Please sign in to comment.