PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bokeh/server/test/pandasmodel_test.py

https://gitlab.com/intruxxer/bokeh
Python | 58 lines | 51 code | 7 blank | 0 comment | 0 complexity | 93da69f979d3fcdacdf9b3d9291a28c2 MD5 | raw file
  1. import unittest
  2. from ...specialmodels.pandasmodel import PandasPivotModel, PandasDataSource
  3. from ...bbmodel import make_model, ContinuumModel
  4. import tempfile
  5. import cPickle as pickle
  6. import pandas
  7. class PandasModelTestCase(unittest.TestCase):
  8. def setUp(self):
  9. self.tempfile = tempfile.NamedTemporaryFile()
  10. df = pandas.DataFrame({
  11. 'vals' : [5,4,3,2,1],
  12. 'types' : ['b','b','a','a','a']
  13. })
  14. self.datasource = PandasDataSource(
  15. 'PandasDataSource', df=df, path=self.tempfile.name)
  16. def test_pandas_instantiation(self):
  17. temp = make_model('newtype', x=1)
  18. assert isinstance(temp, ContinuumModel)
  19. assert not isinstance(temp, PandasPivotModel)
  20. temp = PandasPivotModel('PandasPivot',
  21. pandassourceobj=self.datasource)
  22. assert isinstance(temp, PandasPivotModel)
  23. temp = make_model('PandasPivot',
  24. pandassourceobj=self.datasource)
  25. assert isinstance(temp, PandasPivotModel)
  26. def test_data_to_json(self):
  27. model = make_model('PandasPivot',
  28. pandassourceobj=self.datasource)
  29. temp = model.to_json()
  30. data = [{'vals': 5, 'types': 'b'},
  31. {'vals': 4, 'types': 'b'},
  32. {'vals': 3, 'types': 'a'},
  33. {'vals': 2, 'types': 'a'},
  34. {'vals': 1, 'types': 'a'}]
  35. assert data == temp['data']
  36. def test_data_sorting(self):
  37. model = make_model('PandasPivot',
  38. pandassourceobj=self.datasource,
  39. sort=[{'column' : 'vals', 'ascending' : True}]
  40. )
  41. temp = model.to_json()
  42. assert temp['data'][0]['vals'] == 1
  43. def test_data_groupby(self):
  44. model = make_model('PandasPivot',
  45. pandassourceobj=self.datasource,
  46. sort=[{'column' : 'vals', 'ascending' : True}],
  47. groups=['types']
  48. )
  49. temp = model.to_json()
  50. data = [{'vals': '6.00'}, {'vals': '9.00'}]
  51. assert temp['data'] == data