Example #1
0
    def test_get_value_lru_cache_disabled(self):
        with foreign_value_lru_cache_disabled("get_value"):
            self.log.user_agent = "Mozilla"
            self.log.save()
            log2 = UserAccessLog(user_agent="Mozilla")
            with self.assertNumQueries(1):
                self.assertEqual(log2.user_agent, "Mozilla")

            # successive access to the save value hits the DB
            log2 = UserAccessLog(user_agent="Mozilla")
            with self.assertNumQueries(1):
                self.assertEqual(log2.user_agent, "Mozilla")
Example #2
0
 def test_reuse_foreign_value(self):
     self.log.user_agent = "Mozilla"
     self.log.save()
     log2 = UserAccessLog(user_agent="Mozilla")
     log2.save()
     self.assertNotEqual(self.log.id, log2.id)
     self.assertEqual(self.log.user_agent_fk_id, log2.user_agent_fk_id)
Example #3
0
    def test_get_related_lru_cache_disabled(self):
        with foreign_value_lru_cache_disabled("get_related"):
            self.log.user_agent = "Mozilla"
            log2 = UserAccessLog(user_agent="Mozilla")
            self.assertEqual(self.log.user_agent_fk.id, log2.user_agent_fk.id)

            # different instances indicates LRU cache was not used
            self.assertIsNot(self.log.user_agent_fk, log2.user_agent_fk)
Example #4
0
    def test_lru_cache(self):
        info = UserAccessLog.user_agent.get_related.cache_info()
        self.assertEqual(info.misses, 0)
        self.assertEqual(info.hits, 0)

        self.log.user_agent = "Mozilla"
        info = UserAccessLog.user_agent.get_related.cache_info()
        self.assertEqual(info.misses, 1)
        self.assertEqual(info.hits, 0)

        log2 = UserAccessLog(user_agent="Mozilla")
        info = UserAccessLog.user_agent.get_related.cache_info()
        self.assertEqual(info.misses, 1)
        self.assertEqual(info.hits, 1)

        # matching instances indicates LRU cache was used
        self.assertIs(self.log.user_agent_fk, log2.user_agent_fk)
Example #5
0
 def test_foreign_value_init(self):
     log = UserAccessLog(user_agent="Mozilla")
     self.assertEqual(log.user_agent_fk.value, "Mozilla")
Example #6
0
 def setUp(self):
     self.log = UserAccessLog()
Example #7
0
def test_get_foreign_value():
    log = UserAccessLog(user_agent_fk=UserAgent(value="Mozilla"))
    eq(log.user_agent, "Mozilla")
    eq(log.user_agent_fk.id, None)
    eq(log.user_agent_fk_id, None)
Example #8
0
def test_foreign_value_default_is_none():
    eq(UserAccessLog().user_agent, None)