Skip to content

Commit

Permalink
iobeam.createDataStore will return previously made DataStore if it ha…
Browse files Browse the repository at this point in the history
…s the same columns
  • Loading branch information
RobAtticus committed Jul 15, 2016
1 parent d7b0d80 commit 6cca6ea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions iobeam/iobeam.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ def createDataStore(self, columns):
DataStore object with those columns and being tracked
by this client for sending.
"""
for store in self._batches:
if store.hasSameColumns(columns):
return store

ds = data.DataStore(columns)
self._batches.append(ds)

Expand Down
10 changes: 10 additions & 0 deletions iobeam/resources/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ def rows(self):
ret.append(r.copy())
return ret

def hasSameColumns(self, cols):
"""Check if this datastore has exactly a list of columns."""
if cols is None or not isinstance(cols, list):
return False
elif len(cols) != len(self._columns):
return False
else:
return set(cols) == set(self._columns)


def split(self, chunkSize):
"""Split a store into multiple batches with `chunkSize` rows.
Expand Down
15 changes: 15 additions & 0 deletions tests/resources/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ def verify(time, data):
for (t, d) in cases:
verify(t, d)

def test_hasSameColumns(self):
columns = ["a", "b", "c"]
ds = data.DataStore(columns)
cases = [
(["c", "b", "a"], True),
(["b", "c", "a"], True),
(["a"], False),
(["a", "b", "d"], False),
(["a", "b", "b"], False),
("abc", False)
]

for c, res in cases:
self.assertEqual(ds.hasSameColumns(c), res)

def test_split(self):
columns = ["a", "b", "c"]
ds = data.DataStore(columns)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_iobeam.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,18 @@ def test_addDataStore(self):
client.addDataStore(ds)
self.assertEqual(1, len(client._batches))

def test_createDataStoreWithSameCols(self):
dummy = DummyBackend()
backend = request.DummyRequester(dummy)
client = self._makeTempClient(backend=backend, deviceId="fake")

ds = client.createDataStore(["col1", "col2", "col3"])
self.assertEqual(1, len(client._batches))
ds2 = client.createDataStore(["col3", "col1", "col2"])
self.assertEqual(1, len(client._batches))
self.assertEqual(ds, ds2)


def test_sendWithBatch(self):
dummy = DummyBackend()
backend = request.DummyRequester(dummy)
Expand Down

0 comments on commit 6cca6ea

Please sign in to comment.