-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user_model.py
100 lines (70 loc) · 3.3 KB
/
test_user_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""User model tests"""
import os, requests
from unittest import TestCase
from models import Bookmark, db, Language, User, Glass, Category, Ingredient, Drink
os.environ["DATABASE_URL"] = "postgresql:///mixology-test"
from app import app
app.config["SQLALCHEMY_ECHO"] = False
db.drop_all()
db.create_all()
english = Language(code="EN", name="English")
db.session.add(english)
db.session.commit()
class UserModelTestCase(TestCase):
"""Test cases for User model class"""
def setUp(self) -> None:
"""Clear users table, and create test user"""
db.session.commit()
self.testuser = User.register("test", "test123", 1)
def tearDown(self) -> None:
"""Empty users table"""
User.query.delete()
def test_user_registration(self):
"""Test User class method "register"."""
self.assertEqual(self.testuser.username, "test")
self.assertEqual(self.testuser.language_pref.name, "English")
self.assertEqual(len(User.query.all()), 1)
def test_user_authentication(self):
"""Test User class method authenticate"""
self.assertTrue(User.authenticate("test", "test123"))
self.assertFalse(User.authenticate("test", "123"))
self.assertFalse(User.authenticate("not_user", "123"))
def test_check_username(self):
"""Tests User class method check_username"""
self.assertTrue(User.check_username("test"))
self.assertFalse(User.check_username("123"))
def test_serialize_user(self):
"""Tests User instance serialize method"""
self.assertDictEqual(self.testuser.serialize(), {
"id": self.testuser.id,
"username": self.testuser.username,
"language_pref": self.testuser.language_pref.code
})
def test_has_bookmark(self):
"""Test User method has_bookmark instance method"""
self.assertFalse(self.testuser.has_bookmark(1))
json_data = requests.get(f"https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=11007").json()
drink_data = json_data["drinks"][0]
ingr_data = requests.get("https://www.thecocktaildb.com/api/json/v1/1/list.php?i=list").json()
ingredients = [Ingredient(name=ingr["strIngredient1"].lower()) for ingr in ingr_data["drinks"]]
languages = [
Language(code="DE", name="German"),
Language(code="ES", name="Spanish"),
Language(code="FR", name="French"),
Language(code="IT", name="Italian"),
Language(code="ZH-HANS", name="Mandarin Chinese, Simplified"),
Language(code="ZH-HANT", name="Mandarin Chinese, Traditional")
]
glass = Glass(name=drink_data["strGlass"].lower())
category = Category(name=drink_data["strCategory"].lower())
db.session.add_all([*ingredients, glass, *languages, category])
db.session.commit()
[drink, instructions, drink_ingredients] = Drink.parse_drink_data(drink_data)
db.session.add(drink)
db.session.commit()
db.session.add_all([*instructions, *drink_ingredients])
db.session.commit()
testbookmark = Bookmark(user_id=self.testuser.id, drink_id=drink.id)
db.session.add(testbookmark)
db.session.commit()
self.assertTrue(self.testuser.has_bookmark(drink.id))