示例#1
0
    def test_key_not_in_cache(self):
        unit_system = random.randint(1, 10)
        SUT = Cache(unit_system)

        value = SUT.get_value('foo', 0, None)

        self.assertIsNone(value)
示例#2
0
    def test_get_data_is_not_expired(self):
        unit_system = random.randint(1, 10)
        SUT = Cache(unit_system)
        key = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])
        value = round(random.uniform(1, 100), 2)
        timestamp = time.time()
        SUT.update_value(key, value, unit_system, timestamp)

        cached_value = SUT.get_value(key, timestamp, 1)
        self.assertEqual(cached_value, value)
示例#3
0
    def test_value_is_updated(self):
        unit_system = random.randint(1, 10)
        SUT = Cache(unit_system)

        key = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])
        value = round(random.uniform(1, 100), 2)
        timestamp = time.time()

        SUT.update_value(key, value, unit_system, timestamp)
        self.assertIn(key, SUT.cached_values)
        self.assertEqual(SUT.cached_values[key]['value'], value)
        self.assertEqual(SUT.cached_values[key]['timestamp'], timestamp)
示例#4
0
    def test_mismatch_unit_system(self):
        unit_system = random.randint(1, 10)
        SUT = Cache(unit_system)

        key = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])
        value = round(random.uniform(1, 100), 2)
        timestamp = time.time()

        unit_system_mismatch = random.randint(11, 20)
        self.assertRaises(ValueError, SUT.update_value, key, value,
                          unit_system_mismatch, timestamp)
示例#5
0
    def test_key_exists(self):
        unit_system = random.randint(1, 10)
        SUT = Cache(unit_system)
        key = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])
        value = round(random.uniform(1, 100), 2)
        SUT.update_value(key, value, unit_system, time.time())

        SUT.remove_value(key)
        self.assertNotIn(key, SUT.cached_values)
示例#6
0
    def test_key_exists(self):
        unit_system = random.randint(1, 10)
        SUT = Cache(unit_system)
        key = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])
        value = round(random.uniform(1, 100), 2)
        SUT.update_value(key, value, unit_system, 0)

        new_time = time.time()
        SUT.update_timestamp(key, new_time)
        self.assertEqual(SUT.cached_values[key]['timestamp'], new_time)
示例#7
0
    def test_key_does_not_exist(self):
        # somewhat silly test
        unit_system = random.randint(1, 10)
        SUT = Cache(unit_system)
        key = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])
        value = round(random.uniform(1, 100), 2)
        SUT.update_value(key, value, unit_system, time.time())

        nonexisting_key = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])
        SUT.update_timestamp(nonexisting_key, time.time())
        self.assertNotIn(nonexisting_key, SUT.cached_values)