Skip to content

Commit

Permalink
cubestat: test for data manager
Browse files Browse the repository at this point in the history
  • Loading branch information
okuvshynov committed Aug 3, 2024
1 parent cc7aefe commit 273a7f7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions cubestat/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def init_group():

self.data = collections.defaultdict(init_group)

# TODO: UI-related things should be moved out of here
def get_slice(self, series, indent, h_shift, cols, spacing):
data_length = len(series) - h_shift if h_shift > 0 else len(series)
chart_width = cols - 2 * len(spacing) - 2 - len(indent)
Expand Down
62 changes: 62 additions & 0 deletions cubestat/tests/test_data_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from cubestat.data import DataManager

import unittest
import collections


class TestDataManager(unittest.TestCase):
def test_init(self):
buffer_size = 10
dm = DataManager(buffer_size)
self.assertIsInstance(dm.data, collections.defaultdict)
self.assertEqual(len(dm.data), 0)

def test_update(self):
buffer_size = 10
dm = DataManager(buffer_size)
updates = [("group1", "title1", 1), ("group1", "title2", 2), ("group2", "title3", 3)]
dm.update(updates)
self.assertEqual(len(dm.data), 2)
self.assertEqual(len(dm.data["group1"]), 2)
self.assertEqual(len(dm.data["group1"]["title1"]), 1)
self.assertEqual(len(dm.data["group1"]["title2"]), 1)
self.assertEqual(len(dm.data["group2"]), 1)
self.assertEqual(len(dm.data["group2"]["title3"]), 1)

def test_get_slice(self):
buffer_size = 10
dm = DataManager(buffer_size)
series = collections.deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=buffer_size)
indent = " "
h_shift = 2
cols = 20
spacing = " "
expected_slice = [1, 2, 3, 4, 5, 6, 7, 8]
self.assertEqual(dm.get_slice(series, indent, h_shift, cols, spacing), expected_slice)

def test_get_slice_shift(self):
buffer_size = 10
dm = DataManager(buffer_size)
series = collections.deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=buffer_size)
indent = " "
h_shift = 2
cols = 10
spacing = " "
expected_slice = [5, 6, 7, 8]
self.assertEqual(dm.get_slice(series, indent, h_shift, cols, spacing), expected_slice)

def test_data_gen(self):
buffer_size = 10
dm = DataManager(buffer_size)
updates = [("group1", "title1", 1), ("group1", "title2", 2), ("group2", "title3", 3)]
dm.update(updates)
expected_data = [
("group1", "title1", collections.deque([1], maxlen=buffer_size)),
("group1", "title2", collections.deque([2], maxlen=buffer_size)),
("group2", "title3", collections.deque([3], maxlen=buffer_size)),
]
self.assertEqual(list(dm.data_gen()), expected_data)


if __name__ == "__main__":
unittest.main()

0 comments on commit 273a7f7

Please sign in to comment.