model.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. t = m.Tag.find_tag(tag_name)
  23. # we should be able to find the tag with the given name
  24. named_tags = m.Tag.select(m.Tag.name == tag_name)
  25. assert len(named_tags) == 1
  26. # subsequent calls to find_tag should return the same db row
  27. t2 = m.Tag.find_tag(tag_name)
  28. assert t.id == t2.id
  29. def test_find_hierarchy(self):
  30. tag_name = 'food/bread/rye'
  31. t = m.Tag.find_tag(tag_name)
  32. # this should have created three DB rows: for 'food', for
  33. # 'food/bread', and for 'food/bread/rye':
  34. assert len(m.Tag.select()) == 3
  35. # searching for a prefix of the tag should yield the same
  36. # parent tag
  37. assert t.parent.id == m.Tag.get(name='food/bread').id
  38. assert t.parent.parent.id == m.Tag.get(name='food').id
  39. # creating a new hierarchical tag with a shared prefix should
  40. # only create the new child tag
  41. t2 = m.Tag.find_tag('food/bread/baguette')
  42. assert len(m.Tag.select()) == 4
  43. # it should share the same parent tags
  44. assert t2.parent.id == t.parent.id
  45. assert t2.parent.parent.id == t.parent.parent.id
  46. # trying to get a hierarchical tag should result in the same
  47. # one already entered
  48. assert t.id == m.Tag.get(name='food/bread/rye').id
  49. assert t2.id == m.Tag.get(name='food/bread/baguette').id
  50. def teardown_method(self, _):
  51. c.DB.close()