Skip to content

Commit

Permalink
shovelling coal into the server
Browse files Browse the repository at this point in the history
  • Loading branch information
yassiommi committed May 8, 2020
1 parent d8b8242 commit fbf722c
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 27 deletions.
17 changes: 17 additions & 0 deletions foods/diet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import random

def sevade(list1, list2, list3, calorie):
i = 0
j = 0
k = len(list3)-1
rand = random.randint(1,10)
while i < len(list1):
while j < len(list2) and k >= 0:
if list1[i].get_calorie() + list2[j].get_calorie() + list3[k].get_calorie() in range(n-rand, n+rand, 1):
return list1[i], list2[j], list3[k], list1[i].get_calorie() + list2[j].get_calorie() + list3[k].get_calorie()
elif list1[i].get_calorie() + list2[j].get_calorie() + list3[k].get_calorie() < n-10:
j += 1
else:
k -=1
i +=1
return None
35 changes: 26 additions & 9 deletions foods/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,31 @@ class Food(db.Model):
__tablename__ = 'foods'

id = Column('id', Integer(), primary_key=True)
calories = Column('calories', Integer())
fat = Column('fat', REAL())
fiber = Column('fiber', REAL())
protein = Column('protein', REAL())
category = Column('category', CHAR(20), nullable=False)
image = Column('image', VARCHAR(400), default=None)
title = Column('title', VARCHAR(200), nullable=False)
createdAt = Column('created_at', TIMESTAMP())
Calories = Column('calories', Integer())
Fat = Column('fat', REAL())
Fiber = Column('fiber', REAL())
Protein = Column('protein', REAL())
Category = Column('category', CHAR(20), nullable=False)
Image = Column('image', VARCHAR(400), default=None)
Title = Column('title', VARCHAR(200), nullable=False)
CreatedAt = Column('created_at', TIMESTAMP())

def __repr__(self):
return f"<Food '{self.title}'>"
return f"<Food '{self.Title}'>"

def __str__(self):
return """{
id:{id},
calories:{calories},
fat:{fat},
fiber:{fiber},
protein:{protein},
category:{category},
image:{image},
title:{title}
}""".format(id=self.id, calories=self.Calorie, fat=self.Fat,
fiber=self.Fiber, protein=self.Protein, category=self.Category,
image=self.Image, title=self.Title)

def get_calorie(self):
return self.Calories
8 changes: 8 additions & 0 deletions foods/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from extentions import db
from foods.models import Food

def get_foods_with_categories(categories):
foods = []
for cat in categories:
foods += Food.query.filter_by(Category=cat).all()
return foods
20 changes: 20 additions & 0 deletions foods/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from foods import foods
from foods.utils import get_foods_with_categories
from foods.diet import sevade

@foods.route('/sevade', methods=['GET'])
def get_sevade(calorie):
cats1 = ['breakfast']
cats2 = ['mostly_meat', 'pasta', 'main_dish', 'sandwich']
cats3 = ['dessert', 'other', 'salad']

dogs1 = get_foods_with_categories(cats1)
dogs2 = get_foods_with_categories(cats2)
dogs3 = get_foods_with_categories(cats3)

catdog = sevade(dogs1, dogs2, dogs3, calorie)

if catdog is None:
return {'error': 'Not Found'}, 404
else:
return {'diet': [str(catdog[0]), str(catdog[1]), str(catdog[2]), catdog[3]]}, 200
3 changes: 2 additions & 1 deletion tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import requests
from os import getenv

ip = getenv('TEST_URL')
# ip = getenv('TEST_URL')
# ip = 'http://127.0.0.1:5000'

class TestAPI(unittest.TestCase):

Expand Down
14 changes: 14 additions & 0 deletions tests/diet_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest
import requests
import json

class TestDiet(unittest.TestCase):

def test_sevade(self):
r = requests.get(ip + '/food/sevade', json={'calorie': 2300})
result = r.result()
assert result['diet'][3] in range(2990, 2310, 1), 'Test Failed :('

def test_zero_sevade(self):
r = requests.get(ip + '/food/sevade', json={'calorie': 0})
assert r.status_code == 404, 'Khak bar saret :('
65 changes: 65 additions & 0 deletions tests/user_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import unittest
import requests
from os import getenv

# ip = getenv('TEST_URL')
# ip = 'http://127.0.0.1:5000'

class TestUser(unittest.TestCase):

def test_create1(self):
# correct things :D
r = requests.post(ip + '/users/signup',
json={'full_name':'Ken Adams', 'email':'[email protected]',
'password':'Audio123', 'confirm_password':'Audio123'})
assert r.status_code == 201, 'Test Failed :('

def test_create2(self):
# mismatching passwords
r = requests.post(ip + '/users/signup',
json={'full_name': 'Ken Adams', 'email': '[email protected]',
'password': 'Audio123', 'confirm_password': 'Audio456'})
assert r.status_code == 400, 'Test Failed :('

def test_create3(self):
# invalid email
r = requests.post(ip + '/users/signup',
json={'full_name': 'Ken Adams', 'email': '[email protected]',
'password': 'Audio123', 'confirm_password': 'Audio123'})
assert r.status_code == 400, 'Test Failed :('

def test_signin1(self):
# correct
r = requests.post(ip + '/users/signin',
json={'email':'[email protected]', 'password':'p@$$word123'})
assert r.status_code == 200, 'Test Failed :('

def test_signin2(self):
# mismatching email and passwords
r = requests.post(ip + '/users/signin',
json={'email':'[email protected]', 'password':'probablywrong'})
assert r.status_code == 403, 'Test Failed :('

def test_signin3(self):
# invalid email
r = requests.post(ip + '/users/signin',
json={'email':'[email protected]', 'password':'p@$$word123'})
assert r.status_code == 400, 'Test Failed :('

def test_modify1(self):
# change password
r = requests.post(ip + '/users/signup/modify',
json={'old_password':'p@$$word123', 'new_password':'123456', 'confirm_password':'123456'})
assert r.status_code == 204, 'Test Failed :('

def test_modify2(self):
# mismtach passwords
r = requests.post(ip + '/users/signup/modify',
json={'old_password': 'p@$$word123', 'new_password': '123456', 'confirm_password': '7891011'})
assert r.status_code == 400, 'Test Failed :('




if __name__ == '__main__':
unittest.main()
17 changes: 0 additions & 17 deletions users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,3 @@ def check_password(self, password):

def __repr__(self):
return f'<Email {self.Email}>'


class Food(db.Model):
__tablename__ = 'foods'

id = Column('id', Integer(), primary_key=True)
calories = Column('calories', Integer())
fat = Column('fat', REAL())
fiber = Column('fiber', REAL())
protein = Column('protein', REAL())
category = Column('category', CHAR(20), nullable=False)
image = Column('image', VARCHAR(400), default=None)
title = Column('title', VARCHAR(200), nullable=False)
createdAt = Column('created_at', TIMESTAMP())

def __repr__(self):
return f"<Food '{self.title}'>"

0 comments on commit fbf722c

Please sign in to comment.