示例#1
0
文件: timed.py 项目: crudbug/canopsis
class TimedStorageTest(TestCase):
    """
    MongoTimedStorage UT on data_type = "test_store"
    """

    def setUp(self):
        # create a storage on test_store collections
        self.storage = MongoTimedStorage(data_scope="test_store")

    def test_connect(self):
        self.assertTrue(self.storage.connected())

        self.storage.disconnect()

        self.assertFalse(self.storage.connected())

        self.storage.connect()

        self.assertTrue(self.storage.connected())

    def test_CRUD(self):

        data_id = "test_store_id"

        # start in droping data
        self.storage.drop()

        # ensure count is 0
        count = self.storage.count(data_ids=data_id)
        self.assertEquals(count, 0)

        # let's play with different data_names
        meta = {"min": None, "max": 0}

        timewindow = TimeWindow()

        before_timewindow = [timewindow.start() - 1000]
        in_timewindow = [timewindow.start(), timewindow.start() + 5, timewindow.stop() - 5, timewindow.stop()]
        after_timewindow = [timewindow.stop() + 1000]

        # set timestamps without sort
        timestamps = after_timewindow + before_timewindow + in_timewindow

        for timestamp in timestamps:
            # add a document at starting time window
            self.storage.put(data_id=data_id, value=meta, timestamp=timestamp)

        # check for count equals 2
        count = self.storage.count(data_ids=data_id)
        self.assertEquals(count, 2)

        # check for_data before now
        data = self.storage.get(data_ids=[data_id])
        self.assertEquals(len(data[data_id]), 2)

        # check for data before now with single id
        data = self.storage.get(data_ids=data_id)
        self.assertEquals(len(data), 2)

        # check values are meta
        self.assertEqual(data[0][MongoTimedStorage.VALUE]["max"], meta["max"])

        # check filtering with max == 1
        data = self.storage.get(data_ids=data_id, _filter={"max": 1})
        self.assertEquals(len(data), 0)

        # check filtering with max == 0
        data = self.storage.get(data_ids=data_id, _filter={"max": 0})
        self.assertEquals(len(data), 2)

        # check find
        data = self.storage.find(_filter={"max": 1})
        self.assertEquals(len(data), 0)

        data = self.storage.find(_filter={"max": 0})[data_id]
        self.assertEquals(len(data), 2)

        data = self.storage.find()[data_id]
        self.assertEquals(len(data), 2)

        # add twice same documents with different values
        meta["max"] += 1
        for dat in data:
            # add a document at starting time window
            self.storage.put(data_id=data_id, value=meta, timestamp=dat[MongoTimedStorage.TIMESTAMP])

        # check for_data before now
        data = self.storage.get(data_ids=[data_id])
        self.assertEquals(len(data[data_id]), 2)

        # check for_data before now
        data = self.storage.get(data_ids=[data_id])
        self.assertEquals(len(data[data_id]), 2)

        # check for_data before now with single index
        data = self.storage.get(data_ids=data_id)
        self.assertEquals(len(data), 2)

        # check values are new meta
        self.assertEqual(data[0][MongoTimedStorage.VALUE]["max"], meta["max"])

        # check filtering with max == 0
        data = self.storage.get(data_ids=data_id, _filter={"max": 0})
        self.assertEquals(len(data), 0)

        # check filtering with max == 1
        data = self.storage.get(data_ids=data_id, _filter={"max": 1})
        self.assertEquals(len(data), 2)

        # check find
        data = self.storage.find(_filter={"max": 0})
        self.assertEquals(len(data), 0)

        data = self.storage.find(_filter={"max": 1})[data_id]
        self.assertEquals(len(data), 2)

        data = self.storage.find()[data_id]
        self.assertEquals(len(data), 2)

        # check for data inside timewindow and just before
        data = self.storage.get(data_ids=[data_id], timewindow=timewindow)
        self.assertEquals(len(data), 1)

        # check for data inside timewindow and just before
        data = self.storage.find(timewindow=timewindow)
        self.assertEquals(len(data), 1)

        # remove data inside timewindow
        self.storage.remove(data_ids=[data_id], timewindow=timewindow)
        # check for data outside timewindow
        count = self.storage.count(data_ids=data_id)
        self.assertEquals(count, len(before_timewindow) + len(after_timewindow))

        # remove all data
        self.storage.remove(data_ids=data_id)
        # check for count equals 0
        count = self.storage.count(data_ids=data_id)
        self.assertEquals(count, 0)
示例#2
0
文件: timed.py 项目: Anhmike/canopsis
class TimedStorageTest(TestCase):
    """
    MongoTimedStorage UT on data_type = "test_store"
    """

    def setUp(self):
        # create a storage on test_store collections
        self.storage = MongoTimedStorage(data_scope="test_store")

    def test_connect(self):
        self.assertTrue(self.storage.connected())

        self.storage.disconnect()

        self.assertFalse(self.storage.connected())

        self.storage.connect()

        self.assertTrue(self.storage.connected())

    def test_CRUD(self):

        data_id = 'test_store_id'

        # start in droping data
        self.storage.drop()

        # ensure count is 0
        count = self.storage.count(data_id=data_id)
        self.assertEquals(count, 0)

        # let's play with different data_names
        meta = {'min': None, 'max': 0}

        timewindow = TimeWindow()

        before_timewindow = [timewindow.start() - 1000]
        in_timewindow = [
            timewindow.start(),
            timewindow.start() + 5,
            timewindow.stop() - 5,
            timewindow.stop()
        ]
        after_timewindow = [timewindow.stop() + 1000]

        # set timestamps without sort
        timestamps = after_timewindow + before_timewindow + in_timewindow

        for timestamp in timestamps:
            # add a document at starting time window
            self.storage.put(data_id=data_id, value=meta, timestamp=timestamp)

        # check for count equals 5
        count = self.storage.count(data_id=data_id)
        self.assertEquals(count, 2)

        # check for_data before now
        data = self.storage.get(data_ids=[data_id])
        self.assertEquals(len(data[data_id]), 2)

        # check for data inside timewindow and just before
        data = self.storage.get(data_ids=[data_id], timewindow=timewindow)
        self.assertEquals(len(data), 1)

        # remove data inside timewindow
        self.storage.remove(data_ids=[data_id], timewindow=timewindow)
        # check for data outside timewindow
        count = self.storage.count(data_id=data_id)
        self.assertEquals(
            count, len(before_timewindow) + len(after_timewindow))

        # remove all data
        self.storage.remove(data_ids=[data_id])
        # check for count equals 0
        count = self.storage.count(data_id=data_id)
        self.assertEquals(count, 0)