Esempio n. 1
0
    def test_bytes(self):
        if six.PY3:
            item = bytes(10)
        else:
            item = bytearray(10)

        utils.clean(item)
Esempio n. 2
0
    def test_clean(self):
        simple = {
            'decimal': Decimal('0.142857'),
            'unicode': six.u('woo'),
            'date': datetime.now(),
            'long': 200000000,
            'integer': 1,
            'float': 2.0,
            'bool': True,
            'str': 'woo',
            'none': None
        }

        complicated = {
            'exception': Exception('This should show up'),
            'timedelta': timedelta(microseconds=20),
            'list': [1, 2, 3]
        }

        combined = dict(simple.items())
        combined.update(complicated.items())

        pre_clean_keys = combined.keys()

        utils.clean(combined)
        self.assertEqual(combined.keys(), pre_clean_keys)
Esempio n. 3
0
    def _enqueue(self, msg):
        """Push a new `msg` onto the queue, return `(success, msg)`"""
        timestamp = msg['timestamp']
        if timestamp is None:
            timestamp = datetime.utcnow().replace(tzinfo=tzutc())

        require('integrations', msg['integrations'], dict)
        require('type', msg['type'], string_types)
        require('timestamp', timestamp, datetime)
        require('context', msg['context'], dict)

        # add common
        timestamp = guess_timezone(timestamp)
        msg['timestamp'] = timestamp.isoformat()
        msg['messageId'] = str(uuid4())
        msg['context']['library'] = {
            'name': 'analytics-python',
            'version': VERSION
        }

        clean(msg)

        if self.queue.full():
            self.log.warn('analytics-python queue is full')
            return False, msg

        self.queue.put(msg)
        self.log.debug('enqueued ' + msg['type'] + '.')
        return True, msg
Esempio n. 4
0
    def test_clean(self):
        simple = {
            "decimal": Decimal("0.142857"),
            "unicode": six.u("woo"),
            "date": datetime.now(),
            "long": 200000000,
            "integer": 1,
            "float": 2.0,
            "bool": True,
            "str": "woo",
            "none": None,
        }

        complicated = {
            "exception": Exception("This should show up"),
            "timedelta": timedelta(microseconds=20),
            "list": [1, 2, 3],
        }

        combined = dict(simple.items())
        combined.update(complicated.items())

        pre_clean_keys = combined.keys()

        utils.clean(combined)
        self.assertEqual(combined.keys(), pre_clean_keys)
Esempio n. 5
0
    def _enqueue(self, msg):
        """Push a new `msg` onto the queue, return `(success, msg)`"""
        timestamp = msg['timestamp']
        if timestamp is None:
            timestamp = datetime.utcnow().replace(tzinfo=tzutc())

        require('integrations', msg['integrations'], dict)
        require('type', msg['type'], string_types)
        require('timestamp', timestamp, datetime)
        require('context', msg['context'], dict)

        # add common
        timestamp = guess_timezone(timestamp)
        msg['timestamp'] = timestamp.isoformat()
        msg['messageId'] = str(uuid4())
        msg['context']['library'] = {
            'name': 'analytics-python',
            'version': VERSION
        }

        clean(msg)

        if self.queue.full():
            self.log.warn('analytics-python queue is full')
            return False, msg

        self.queue.put(msg)
        self.log.debug('enqueued ' + msg['type'] + '.')
        return True, msg
Esempio n. 6
0
    def test_clean(self):
        simple = {
            'decimal': Decimal('0.142857'),
            'unicode': six.u('woo'),
            'date': datetime.now(),
            'long': 200000000,
            'integer': 1,
            'float': 2.0,
            'bool': True,
            'str': 'woo',
            'none': None
        }

        complicated = {
            'exception': Exception('This should show up'),
            'timedelta': timedelta(microseconds=20),
            'list': [1, 2, 3]
        }

        combined = dict(simple.items())
        combined.update(complicated.items())

        pre_clean_keys = combined.keys()

        utils.clean(combined)
        self.assertEqual(combined.keys(), pre_clean_keys)
Esempio n. 7
0
    def test_bytes(self):
        if six.PY3:
            item = bytes(10)
        else:
            item = bytearray(10)

        utils.clean(item)
Esempio n. 8
0
    def _enqueue(self, msg):
        """Push a new `msg` onto the queue, return `(success, msg)`"""
        timestamp = msg['timestamp']
        if timestamp is None:
            timestamp = datetime.utcnow().replace(tzinfo=tzutc())

        require('integrations', msg['integrations'], dict)
        require('type', msg['type'], string_types)
        require('timestamp', timestamp, datetime)
        require('context', msg['context'], dict)

        # add common
        timestamp = guess_timezone(timestamp)
        msg['timestamp'] = timestamp.isoformat()
        msg['messageId'] = str(uuid4())
        msg['context']['library'] = {
            'name': 'analytics-python',
            'version': VERSION
        }

        msg = clean(msg)
        self.log.debug('queueing: %s', msg)

        # if send is False, return msg as if it was successfully queued
        if not self.send:
            return True, msg

        try:
            self.queue.put(msg, block=False)
            self.log.debug('enqueued %s.', msg['type'])
            return True, msg
        except queue.Full:
            self.log.warn('analytics-python queue is full')
            return False, msg
Esempio n. 9
0
    def _enqueue(self, msg):
        """Push a new `msg` onto the queue, return `(success, msg)`"""
        timestamp = msg['timestamp']
        if timestamp is None:
            timestamp = datetime.utcnow().replace(tzinfo=tzutc())

        require('integrations', msg['integrations'], dict)
        require('type', msg['type'], string_types)
        require('timestamp', timestamp, datetime)
        require('context', msg['context'], dict)

        # add common
        timestamp = guess_timezone(timestamp)
        msg['timestamp'] = timestamp.isoformat()
        msg['messageId'] = str(uuid4())
        msg['context']['library'] = {
            'name': 'analytics-python',
            'version': VERSION
        }

        msg = clean(msg)
        self.log.debug('queueing: %s', msg)

        # if send is False, return msg as if it was successfully queued
        if not self.send:
            return True, msg

        try:
            self.queue.put(msg, block=False)
            self.log.debug('enqueued %s.', msg['type'])
            return True, msg
        except queue.Full:
            self.log.warn('analytics-python queue is full')
            return False, msg
Esempio n. 10
0
    def _enqueue(self, msg):
        """Push a new `msg` onto the queue, return `(success, msg)`"""
        timestamp = msg['timestamp']
        if timestamp is None:
            timestamp = datetime.utcnow().replace(tzinfo=tzutc())
        message_id = msg.get('messageId')
        if message_id is None:
            message_id = uuid4()

        require('integrations', msg['integrations'], dict)
        require('type', msg['type'], string_types)
        require('timestamp', timestamp, datetime)
        require('context', msg['context'], dict)

        # add common
        timestamp = guess_timezone(timestamp)
        msg['timestamp'] = timestamp.isoformat()
        msg['messageId'] = stringify_id(message_id)
        msg['context']['library'] = {
            'name': 'analytics-python',
            'version': VERSION
        }

        msg['userId'] = stringify_id(msg.get('userId', None))
        msg['anonymousId'] = stringify_id(msg.get('anonymousId', None))

        msg = clean(msg)
        self.log.debug('queueing: %s', msg)

        # if send is False, return msg as if it was successfully queued
        if not self.send:
            return True, msg

        if self.sync_mode:
            self.log.debug('enqueued with blocking %s.', msg['type'])
            post(self.write_key,
                 self.host,
                 gzip=self.gzip,
                 timeout=self.timeout,
                 proxies=self.proxies,
                 batch=[msg])

            return True, msg

        try:
            self.queue.put(msg, block=False)
            self.log.debug('enqueued %s.', msg['type'])
            return True, msg
        except queue.Full:
            self.log.warning('analytics-python queue is full')
            return False, msg
Esempio n. 11
0
    def _enqueue(self, msg):
        """Push a new `msg` onto the queue, return `(success, msg)`"""
        timestamp = msg['timestamp']
        if timestamp is None:
            timestamp = datetime.utcnow().replace(tzinfo=tzutc())

        require('integrations', msg['integrations'], dict)
        require('type', msg['type'], string_types)
        require('timestamp', timestamp, datetime)
        require('context', msg['context'], dict)

        # add common
        timestamp = guess_timezone(timestamp)
        msg['timestamp'] = timestamp.isoformat()
        msg['messageId'] = str(uuid4())
        msg['context']['library'] = {
            'name': 'analytics-python',
            'version': VERSION
        }

        msg['userId'] = stringify_id(msg.get('userId', None))
        msg['anonymousId'] = stringify_id(msg.get('anonymousId', None))

        msg = clean(msg)
        self.log.debug('queueing: %s', msg)

        # if send is False, return msg as if it was successfully queued
        if not self.send:
            return True, msg

        if self.max_queue_size == 0: # If we're not using a queue, send immediately (synchronously)
            post(self.write_key, self.host, batch=[msg])
            return True, msg
        else:
            try:
                self.queue.put(msg, block=False)
                self.log.debug('enqueued %s.', msg['type'])
                return True, msg
            except queue.Full:
                self.log.warn('analytics-python queue is full')
                return False, msg
Esempio n. 12
0
 def test_clean_with_dates(self):
     dict_with_dates = {
         'birthdate': date(1980, 1, 1),
         'registration': datetime.utcnow(),
     }
     self.assertEqual(dict_with_dates, utils.clean(dict_with_dates))
Esempio n. 13
0
 def test_clean_fn(self):
     cleaned = utils.clean({'fn': lambda x: x, 'number': 4})
     self.assertEqual(cleaned['number'], 4)
     if 'fn' in cleaned:
         self.assertEqual(cleaned['fn'], None)
Esempio n. 14
0
 def test_bytes(cls):
     item = bytes(10)
     utils.clean(item)
Esempio n. 15
0
 def test_clean_fn(self):
     cleaned = utils.clean({ 'fn': lambda x: x, 'number': 4 })
     self.assertEqual(cleaned['number'], 4)
     # TODO: fixme, different behavior on python 2 and 3
     if 'fn' in cleaned:
         self.assertEqual(cleaned['fn'], None)
Esempio n. 16
0
 def test_clean_fn(self):
     cleaned = utils.clean({"fn": lambda x: x, "number": 4})
     self.assertEqual(cleaned["number"], 4)
     # TODO: fixme, different behavior on python 2 and 3
     if "fn" in cleaned:
         self.assertEqual(cleaned["fn"], None)
Esempio n. 17
0
 def test_clean_fn(self):
     cleaned = utils.clean({'fn': lambda x: x, 'number': 4})
     self.assertEqual(cleaned['number'], 4)
     # TODO: fixme, different behavior on python 2 and 3
     if 'fn' in cleaned:
         self.assertEqual(cleaned['fn'], None)
Esempio n. 18
0
 def test_clean_with_dates(self):
     dict_with_dates = {
         'birthdate': date(1980, 1, 1),
         'registration': datetime.utcnow(),
     }
     self.assertEqual(dict_with_dates, utils.clean(dict_with_dates))