示例#1
0
文件: tests.py 项目: slok/dwarf
    def test_click_store_autofields(self):

        token = utils.counter_to_token(random.randrange(0, 100000))
        url = "http://xlarrakoetxea.org"
        OS = "linux"
        ip = "111.222.333.444"
        incr_times = 4

        # Store a link
        sl = ShortLink(token=token, url=url)
        sl.save()

        for i in range(incr_times):
            ShortLink.incr_clicks(token)

        c = Click(token=token, os=OS, ip=ip)

        c.save()

        # The save method has set the click_date
        click_date = c.click_date
        self.assertIsNotNone(click_date)

        # Check the stored object
        key = Click.REDIS_CLICK_KEY.format(token, c.click_id)
        r = get_redis_connection()
        values = r.hgetall(key)

        # Check the key is correctly set (this means that the counter has
        # increased correctly)
        correct_key = Click.REDIS_CLICK_KEY.format(token, incr_times + 1)
        self.assertEquals(correct_key, key)

        self.assertEquals(OS, values["os"])
        self.assertEquals(ip, values["ip"])
示例#2
0
文件: models.py 项目: slok/dwarf
    def save(self):
        """ Save a click in the database. If the instance doens't have the 
        click_id the method will get the correct value, based on the Short link
        click counter"""

        r = get_redis_connection()

        if not self.token:
            raise ClickError("Not enought data to store click instance")

        #if there isn't an identification then get the apropiate id
        if not self.click_id:
            self.click_id = ShortLink.incr_clicks(self.token)

        #set the date if there isn't a date
        if not self.click_date:
            self.click_date = dateutils.unix_now_utc()

        key = Click.REDIS_CLICK_KEY.format(self.token, self.click_id)

        #Create the data
        to_store = {}
        for i in Click.FIELDS:
            value = getattr(self, i)
            if value:
                to_store[i] = value

        r.hmset(key, to_store)
示例#3
0
文件: tests.py 项目: slok/dwarf
    def test_incr_clicks(self):
        clicks = random.randrange(0, 100000)
        counter = random.randrange(0, 100000)
        url = "xlarrakoetxea.org"

        sl = ShortLink(counter=counter, url=url, clicks=clicks)
        sl.save()

        #Increment
        result = ShortLink.incr_clicks(sl.token)

        #Increment manually the old one
        sl.clicks = sl.clicks + 1

        #Find
        sls = ShortLink.find(token=sl.token)

        self.assertEquals(sl.clicks, sls.clicks)
        self.assertEquals(result, sls.clicks)