# Coverage for api/tests/test_models.py : 0%

## 50 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1"""

2 Tests all db utilities such as CRUDMixin defined under shopapi/models.py

3 Most of the test cases taken from:

4 https://github.com/cookiecutter-flask/cookiecutter-flask

5"""

6 import pytest

7 from flask_login import UserMixin

8 from init import db

9

10 from shopyo.api.models import PkModel

11

12

13 class ExampleUserModel(PkModel, UserMixin):

14 """Example user model for testing purposes"""

15

16 __tablename__ = "testusers"

17

18 username = db.Column(db.String(100), unique=True, nullable=False)

19 email = db.Column(db.String(120), unique=True, nullable=False)

20

21

22 class TestPKModel:

23 """Tests all functions of PKModel"""

24

25 def test_get_by_id(self):

26 result = ExampleUserModel.get_by_id("myId")

27

28 assert not result

29

30 def test_get_by_id_valid(self, db_session):

31 user_bar = ExampleUserModel(username="bar", email="bar@domain.com")

32 user_foo = ExampleUserModel(username="foo", email="foo@domain.com")

33 user_bar.save()

34 user_foo.save()

35 result_int = ExampleUserModel.get_by_id(user_bar.id)

36 result_str = ExampleUserModel.get_by_id(str(user_bar.id))

37 expected = (

38 db_session.query(ExampleUserModel)

39 .filter(ExampleUserModel.username == "bar")

40 .scalar()

41 )

42

43 assert result_int

44 assert expected

45 assert result_int.username == expected.username

46 assert result_str.username == expected.username

47

48

49 class TestCRUDMixin:

50 """Test class for testing all CRUD functions"""

51

52 def test_create(self, db_session):

53 user = ExampleUserModel.create(username="bar", email="bar@domain.com")

54 result_raw = db_session.execute("""select * from testusers""").fetchone()

55 result_orm = (

56 db_session.query(ExampleUserModel)

57 .filter(ExampleUserModel.id == user.id)

58 .scalar()

59 )

60

61 assert result_orm

62 assert result_raw

63 assert result_raw.username == "bar"

64 assert result_orm.username == "bar"

65

66 @pytest.mark.parametrize("commit,expected", [(True, "foo"), (False, "bar")])

67 def test_update_single(self, db_session, commit, expected):

68 user = ExampleUserModel(username="bar", email="bar@domain.com")

69 user.save()

70 user.update(commit=commit, username="foo")

71 result = db_session.execute("""select * from testusers""").fetchone()

72

73 assert result

74 assert result.username == expected

75

76 @pytest.mark.parametrize(

77 "commit,expected",

78 [

79 (True, {"username": "foo", "email": "foo@domain.com"}),

80 (False, {"username": "bar", "email": "bar@domain.com"}),

81 ],

82 )

83 def test_update_multiple(self, db_session, commit, expected):

84 user = ExampleUserModel(username="bar", email="bar@domain.com")

85 user.save()

86 user.update(commit=commit, username="foo", email="foo@domain.com")

87 result = db_session.execute("""select * from testusers""").fetchone()

88

89 assert result

90 assert result.username == expected["username"]

91 assert result.email == expected["email"]

92

93 # @pytest.mark.parametrize("commit,expected", [(True, None), (False, "bar")])

94 # def test_delete(self, commit, expected):

95 # user = ExampleUserModel(username="bar", email="bar@domain.com")

96 # user.save()

97 # user.delete(commit=commit)

98 # result = ExampleUserModel.get_by_id(user.id)

99 # if result:

100 # result = result.username

101

102 # assert result == expected