model.py 2.2 KB

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