model.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import peewee
  2. import pytest
  3. import lc.config as c
  4. import lc.error as e
  5. import lc.request as r
  6. import lc.model as m
  7. class Testdb:
  8. def setup_method(self, _):
  9. c.db.init(":memory:")
  10. c.db.create_tables(m.MODELS)
  11. def teardown_method(self, _):
  12. c.db.close()
  13. def mk_user(self, name="gdritter", password="foo") -> m.User:
  14. return m.User.from_request(r.User(name=name, password=password,))
  15. def test_create_user(self):
  16. name = "gdritter"
  17. u = self.mk_user(name=name)
  18. # it should be the only thing in the db
  19. all_users = m.User.select()
  20. assert len(all_users) == 1
  21. assert all_users[0].id == u.id
  22. assert all_users[0].name == name
  23. # we should be able to find it with the given name, too
  24. named_user = m.User.get(m.User.name == name)
  25. assert named_user.id == u.id
  26. assert named_user.name == name
  27. def test_user_passwords(self):
  28. name = "gdritter"
  29. password = "foo"
  30. u = self.mk_user(name=name, password=password)
  31. print(u.name, u.passhash)
  32. assert u.authenticate(password)
  33. assert u.authenticate("wrong password") is False
  34. def test_no_duplicate_users(self):
  35. name = "gdritter"
  36. u1 = self.mk_user(name=name)
  37. with pytest.raises(e.UserExists):
  38. u2 = self.mk_user(name=name)
  39. def test_get_or_create_tag(self):
  40. u = self.mk_user()
  41. tag_name = "food"
  42. t = m.Tag.get_or_create_tag(u, tag_name)
  43. # we should be able to find the tag with the given name
  44. named_tags = m.Tag.select(m.Tag.user == u and m.Tag.name == tag_name)
  45. assert len(named_tags) == 1
  46. # subsequent calls to get_or_create_tag should return the same db row
  47. t2 = m.Tag.get_or_create_tag(u, tag_name)
  48. assert t.id == t2.id
  49. def test_find_hierarchy(self):
  50. u = self.mk_user()
  51. t = m.Tag.get_or_create_tag(u, "food/bread/rye")
  52. # this should have created three db rows: for 'food', for
  53. # 'food/bread', and for 'food/bread/rye':
  54. assert len(m.Tag.select()) == 3
  55. # searching for a prefix of the tag should yield the same
  56. # parent tag
  57. assert t.parent.id == m.Tag.get(name="food/bread").id
  58. assert t.parent.parent.id == m.Tag.get(name="food").id
  59. # creating a new hierarchical tag with a shared prefix should
  60. # only create the new child tag
  61. t2 = m.Tag.get_or_create_tag(u, "food/bread/baguette")
  62. print([t.name for t in m.Tag.select()])
  63. assert len(m.Tag.select()) == 4
  64. # it should share the same parent tags
  65. assert t2.parent.id == t.parent.id
  66. assert t2.parent.parent.id == t.parent.parent.id
  67. # trying to get a hierarchical tag should result in the same
  68. # one already entered
  69. assert t.id == m.Tag.get(name="food/bread/rye").id
  70. assert t2.id == m.Tag.get(name="food/bread/baguette").id