Advertisement
cmiN

test_base.py

Mar 8th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. """Tests base utilities used in Datastore interactions."""
  2.  
  3.  
  4. from .conftest import TrueStoryModel, skip_missing_credentials
  5.  
  6.  
  7. pytestmark = skip_missing_credentials
  8.  
  9.  
  10. def test_model_no_save(truestory_model):
  11.     # Check basic property data.
  12.     assert not truestory_model.bool_prop
  13.     assert truestory_model.str_prop == "str_prop"
  14.     assert truestory_model.txt_prop is None
  15.     assert truestory_model.auto_prop == 0
  16.  
  17.     # Check utilities.
  18.     assert truestory_model.model_name() == "TrueStory"
  19.     assert truestory_model.normalize(truestory_model.txt_prop) == "N/A"
  20.     assert not truestory_model.exists
  21.  
  22.  
  23. def test_model_save_delete(truestory_model):
  24.     # Test saving.
  25.     truestory_model.put()
  26.     assert truestory_model.exists
  27.  
  28.     # Test local vs. remote diffs.
  29.     truestory_model.bool_prop = True
  30.     assert truestory_model.bool_prop != truestory_model.myself.bool_prop
  31.  
  32.     # Test urlsafe retrieval.
  33.     truestory_model.bool_prop = True
  34.     truestory_model.put()
  35.     assert truestory_model.get(truestory_model.urlsafe).bool_prop
  36.  
  37.     # Test removal.
  38.     truestory_model.remove()
  39.     assert not truestory_model.exists
  40.  
  41.  
  42. def test_model_query(truestory_model):
  43.     # Save it with some data first in order to be able to query something relevant.
  44.     nr = 4
  45.     total = nr * (nr + 1) // 2
  46.     truestory_model.list_prop = list(range(nr + 1))
  47.     truestory_model.put()
  48.  
  49.     # Check if the computed property correctly sums up the previously set numbers.
  50.     items = TrueStoryModel.all()
  51.     # We try with all of them for the edge case where the DB is dirty.
  52.     flags = [item.auto_prop == total for item in items]
  53.     assert any(flags)
  54.  
  55.     # Test with custom query this time.
  56.     query = TrueStoryModel.query()
  57.     query.add_filter("auto_prop", "=", total)
  58.     assert list(query.fetch())  # At least one item in the list.
  59.  
  60.     # Explicit cleanup (even if not required).
  61.     truestory_model.remove()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement