model.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import lc.config as c
  2. import lc.model as m
  3. class TestDB:
  4. def setup_method(self, _):
  5. c.DB.init(":memory:")
  6. c.DB.create_tables(m.MODELS)
  7. def teardown_method(self, _):
  8. c.DB.close()
  9. def test_create_user(self):
  10. name = "gdritter"
  11. u = m.User.create(name=name)
  12. # it should be the only thing in the db
  13. all_users = m.User.select()
  14. assert len(all_users) == 1
  15. assert all_users[0].id == u.id
  16. assert all_users[0].name == name
  17. # we should be able to find it with the given name, too
  18. named_user = m.User.get(m.User.name == name)
  19. assert named_user.id == u.id
  20. assert named_user.name == name
  21. def test_find_tag(self):
  22. tag_name = "food"
  23. u = m.User.create(name="gdritter")
  24. t = m.Tag.find_tag(u, tag_name)
  25. # we should be able to find the tag with the given name
  26. named_tags = m.Tag.select(m.Tag.user == u and m.Tag.name == tag_name)
  27. assert len(named_tags) == 1
  28. # subsequent calls to find_tag should return the same db row
  29. t2 = m.Tag.find_tag(u, tag_name)
  30. assert t.id == t2.id
  31. def test_find_hierarchy(self):
  32. u = m.User.create(name="gdritter")
  33. t = m.Tag.find_tag(u, "food/bread/rye")
  34. # this should have created three DB rows: for 'food', for
  35. # 'food/bread', and for 'food/bread/rye':
  36. assert len(m.Tag.select()) == 3
  37. # searching for a prefix of the tag should yield the same
  38. # parent tag
  39. assert t.parent.id == m.Tag.get(name="food/bread").id
  40. assert t.parent.parent.id == m.Tag.get(name="food").id
  41. # creating a new hierarchical tag with a shared prefix should
  42. # only create the new child tag
  43. t2 = m.Tag.find_tag(u, "food/bread/baguette")
  44. print([t.name for t in m.Tag.select()])
  45. assert len(m.Tag.select()) == 4
  46. # it should share the same parent tags
  47. assert t2.parent.id == t.parent.id
  48. assert t2.parent.parent.id == t.parent.parent.id
  49. # trying to get a hierarchical tag should result in the same
  50. # one already entered
  51. assert t.id == m.Tag.get(name="food/bread/rye").id
  52. assert t2.id == m.Tag.get(name="food/bread/baguette").id