Example #1
0
 def test_construction_invalid_definition(self):
     try:
         definition = datasift.Definition(self.user, 1234)
         self.fail('Expected InvalidDataError exception not thrown')
     except datasift.InvalidDataError:
         # Expected exception
         pass
Example #2
0
 def _make_stream(self,
                  broken_method=None,
                  is_running=True,
                  auto_reconnect=True):
     from datasift.streamconsumer_http import (StreamConsumer_HTTP,
                                               StreamConsumer_HTTP_Thread)
     import testdata
     user = datasift.User('fake', 'user')
     client = datasift.mockapiclient.MockApiClient()
     response = {
         'response_code': 200,
         'data': {
             'hash': testdata.definition_hash,
             'created_at': '2011-12-13 14:15:16',
             'dpu': 10,
         },
         'rate_limit': 200,
         'rate_limit_remaining': 150,
     }
     client.set_response(response)
     user.set_api_client(client)
     definition = datasift.Definition(user, 'some cdsl')
     handler = BrokenHandler(broken_method)
     consumer = StreamConsumer_HTTP(user, definition, handler)
     if is_running:
         consumer._state = consumer.STATE_RUNNING
     return StreamConsumer_HTTP_Thread(consumer,
                                       auto_reconnect=auto_reconnect)
Example #3
0
    def test_get_total_dpu(self):
        response = {
            'response_code': 200,
            'data': {
                'created_at': '2011-12-13 14:15:16',
                'dpu':        10,
            },
            'rate_limit':           200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        definition = datasift.Definition(self.user, testdata.definition)
        self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')

        self.assertEqual(definition.get_total_dpu(), response['data']['dpu'], 'Incorrect total DPU')
Example #4
0
    def test_get_created_at(self):
        response = {
            'response_code': 200,
            'data': {
                'created_at': '2011-12-13 14:15:16',
                'dpu':        10,
            },
            'rate_limit':           200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        definition = datasift.Definition(self.user, testdata.definition)
        self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')

        self.assertEqual(definition.get_created_at(), datetime.strptime(response['data']['created_at'], '%Y-%m-%d %H:%M:%S'), 'Incorrect created at date')
Example #5
0
    def test_get_dpu_breakdown(self):
        response = {
            'response_code': 200,
            'data': {
                'hash':       testdata.definition_hash,
                'created_at': '2011-12-13 14:15:16',
                'dpu':        4,
            },
            'rate_limit':           200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        definition = datasift.Definition(self.user, testdata.definition)
        self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')

        self.assertEqual(definition.get_hash(), response['data']['hash'], 'Incorrect hash')

        response = {
            'response_code': 200,
            'data': {
                'detail': {
                    'contains': {
                        'count': 1,
                        'dpu':   4,
                        'targets': {
                            'interaction.content': {
                                'count': 1,
                                'dpu':   4,
                            },
                        },
                    },
                },
                'dpu': 4,
            },
            'rate_limit':           200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        dpu = definition.get_dpu_breakdown()

        self.assertEqual(dpu, response['data'], 'The DPU breakdown is not as expected')
        self.assertEqual(definition.get_total_dpu(), response['data']['dpu'], 'The total DPU is incorrect')
Example #6
0
    def test_get_consumer(self):
        response = {
            'response_code': 200,
            'data': {
                'hash':       testdata.definition_hash,
                'created_at': '2011-12-13 14:15:16',
                'dpu':        4,
            },
            'rate_limit':           200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        definition = datasift.Definition(self.user, testdata.definition)
        self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')

        self.assertEqual(definition.get_hash(), response['data']['hash'], 'Incorrect hash')

        consumer = definition.get_consumer(datasift.StreamConsumerEventHandler())
Example #7
0
    def test_compile_failure(self):
        response = {
            'response_code': 400,
            'data': {
                'error': 'The target interactin.content does not exist',
            },
            'rate_limit':           200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        definition = datasift.Definition(self.user, testdata.invalid_definition)
        self.assertEqual(definition.get(), testdata.invalid_definition, 'Definition CSDL not set correctly')

        try:
            definition.compile()
            self.fail('Expected CompileFailedError not thrown')
        except datasift.InvalidDataError as e:
            self.fail('InvalidDataError: %s' % e)
        except datasift.CompileFailedError as e:
            self.assertEqual(e.__str__(), response['data']['error'])
        except datasift.APIError as (e, c):
            self.fail('APIError: %s' % e)
Example #8
0
    def test_compile_success(self):
        response = {
            'response_code': 200,
            'data': {
                'hash':       testdata.definition_hash,
                'created_at': '2011-12-13 14:15:16',
                'dpu':        10,
            },
            'rate_limit':           200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        definition = datasift.Definition(self.user, testdata.definition)
        self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')

        try:
            definition.compile()
        except datasift.InvalidDataError as e:
            self.fail('InvalidDataError: %s' % e)
        except datasift.CompileFailedError as e:
            self.fail('CompileFailedError: %s' % e)
        except datasift.APIError as (e, c):
            self.fail('APIError: %s' % e)
Example #9
0
 def test_set_and_get(self):
     definition = datasift.Definition(self.user)
     self.assertEqual(definition.get(), '', 'Default definition CSDL is not empty')
     definition.set(testdata.definition)
     self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
Example #10
0
    def test_get_buffered(self):
        response = {
            'response_code': 200,
            'data': {
                'hash':       testdata.definition_hash,
                'created_at': '2011-12-13 14:15:16',
                'dpu':        4,
            },
            'rate_limit':           200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        definition = datasift.Definition(self.user, testdata.definition)
        self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')

        self.assertEqual(definition.get_hash(), response['data']['hash'], 'Incorrect hash')

        response = {
            'response_code': 200,
            'data': {
                'stream': {
                    0: {
                        'interaction': {
                            'source': 'Snaptu',
                            'author': {
                                'username': '******',
                                'name'    : 'nittosoetreznoe',
                                'id'      : 172192091,
                                'avatar'  : 'http://a0.twimg.com/profile_images/1429378181/gendowor_normal.jpg',
                                'link'    : 'http://twitter.com/nittolexia',
                            },
                            'type'      : 'twitter',
                            'link'      : 'http://twitter.com/nittolexia/statuses/89571192838684672',
                            'created_at': 'Sat, 09 Jul 2011 05:46:51 +0000',
                            'content'   : 'RT @ayyuchadel: Haha RT @nittolexia: Mending gak ush maen twitter dehh..RT @sansan_arie:',
                            'id'        : '1e0a9eedc207acc0e074ea8aecb2c5ea',
                        },
                        'twitter': {
                            'user': {
                                'name'           : 'nittosoetreznoe',
                                'description'    : 'f**k all',
                                'location'       : 'denpasar, bali',
                                'statuses_count' : 6830,
                                'followers_count': 88,
                                'friends_count'  : 111,
                                'screen_name'    : 'nittolexia',
                                'lang'           : 'en',
                                'time_zone'      : 'Alaska',
                                'id'             : 172192091,
                                'geo_enabled'    : True,
                            },
                            'mentions': {
                                0: 'ayyuchadel',
                                1: 'nittolexia',
                                2: 'sansan_arie',
                            },
                            'id'        : '89571192838684672',
                            'text'      : 'RT @ayyuchadel: Haha RT @nittolexia: Mending gak ush maen twitter dehh..RT @sansan_arie:',
                            'source'    : '<a href="http://www.snaptu.com" rel="nofollow">Snaptu</a>',
                            'created_at': 'Sat, 09 Jul 2011 05:46:51 +0000',
                        },
                        'klout': {
                            'score'        : 45,
                            'network'      : 55,
                            'amplification': 17,
                            'true_reach'   : 31,
                            'slope'        : 0,
                            'class'        : 'Networker',
                        },
                        'peerindex': {
                            'score': 30,
                        },
                        'language': {
                            'tag': 'da',
                        },
                    },
                },
            },
            'rate_limit':           200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        interactions = definition.get_buffered()

        self.assertEqual(interactions, response['data']['stream'], 'Buffered interactions are not as expected')
Example #11
0
 def test_construction_with_definition(self):
     definition = datasift.Definition(self.user, testdata.definition)
     self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
Example #12
0
 def test_construction(self):
     definition = datasift.Definition(self.user)
     self.assertEqual(definition.get(), '', 'Default definition CSDL is not empty')
Example #13
0
    def test_subscribe_definition(self):
        definition = datasift.Definition(self.user, testdata.definition)
        response = {
            'response_code': 200,
            'data': {
                'hash': testdata.definition_hash,
                'created_at': '2011-12-13 14:15:16',
                'dpu': 10,
            },
            'rate_limit': 200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)
        self.assertEqual(definition.get_hash(), testdata.definition_hash,
                         'Definition hash not set correctly')

        self._populate_pushdef()

        response = {
            'response_code': 200,
            'data': {
                'id': testdata.push_id,
                'name': testdata.push_name,
                'created_at': testdata.push_created_at,
                'status': testdata.push_status,
                'hash': testdata.push_hash,
                'hash_type': testdata.push_hash_stream_type,
                'output_type': testdata.push_output_type,
                'output_params': {
                    'delivery_frequency':
                    testdata.push_output_params['delivery_frequency'],
                    'url':
                    testdata.push_output_params['url'],
                    'auth': {
                        'type': testdata.push_output_params['auth.type'],
                        'username':
                        testdata.push_output_params['auth.username'],
                        'password':
                        testdata.push_output_params['auth.password'],
                    },
                },
                'last_request': None,
                'last_success': None
            },
            'rate_limit': 200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        pushsub = self.pushdef.subscribe_definition(definition,
                                                    testdata.push_name)

        self.assertEqual(pushsub.get_id(), testdata.push_id,
                         'The subscription ID is incorrect')
        self.assertEqual(pushsub.get_name(), testdata.push_name,
                         'The subscription name is incorrect')
        self.assertEqual(pushsub.get_created_at(), testdata.push_created_at,
                         'The subscription created_at is incorrect')
        self.assertEqual(pushsub.get_status(), testdata.push_status,
                         'The subscription status is incorrect')
        self.assertEqual(pushsub.get_output_type(), testdata.push_output_type,
                         'The subscription output_type is incorrect')
        self.assertEqual(pushsub.get_hash_type(),
                         testdata.push_hash_stream_type,
                         'The subscription hash_type is incorrect')
        self.assertEqual(pushsub.get_hash(), testdata.push_hash,
                         'The subscription hash is incorrect')
        self.assertEqual(pushsub.get_output_param('delivery_frequency'),
                         testdata.push_output_params['delivery_frequency'],
                         'The subscription delivery_frequency is incorrect')
        self.assertEqual(pushsub.get_output_param('url'),
                         testdata.push_output_params['url'],
                         'The subscription url is incorrect')
        self.assertEqual(pushsub.get_output_param('auth.type'),
                         testdata.push_output_params['auth.type'],
                         'The subscription auth.type is incorrect')
        self.assertEqual(pushsub.get_output_param('auth.username'),
                         testdata.push_output_params['auth.username'],
                         'The subscription auth.username is incorrect')
        self.assertEqual(pushsub.get_output_param('auth.password'),
                         testdata.push_output_params['auth.password'],
                         'The subscription auth.password is incorrect')
Example #14
0
    def test_subscribe_historic(self):
        definition = datasift.Definition(self.user, testdata.definition)
        response = {
            'response_code': 200,
            'data': {
                'hash': testdata.definition_hash,
                'created_at': '2011-12-13 14:15:16',
                'dpu': 10,
            },
            'rate_limit': 200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)
        self.assertEqual(definition.get_hash(), testdata.definition_hash,
                         'Definition hash not set correctly')

        response = {
            'response_code': 200,
            'data': {
                'dpus': testdata.historic_dpus,
                'id': testdata.historic_id,
                'availability': {
                    'start': 12345678,
                    'end': 124356376,
                    'sources': {
                        'twitter': {
                            'status': '99%',
                            'versions': [3],
                            'augmentations': {
                                'klout': '50%',
                                'links': '100%'
                            }
                        },
                        'facebook': {
                            'status': '99%',
                            'versions': [2, 3],
                            'augmentations': {
                                'links': '95%'
                            }
                        }
                    }
                }
            },
            'rate_limit': 200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        historic = definition.create_historic(testdata.historic_start_date,
                                              testdata.historic_end_date,
                                              testdata.historic_sources,
                                              testdata.historic_sample,
                                              testdata.historic_name)
        self.assertEqual(historic.get_hash(), testdata.historic_id,
                         'The historic playback ID is incorrect')

        self._populate_pushdef()

        response = {
            'response_code': 200,
            'data': {
                'id': testdata.push_id,
                'name': testdata.push_name,
                'created_at': testdata.push_created_at,
                'status': testdata.push_status,
                'hash': testdata.historic_id,
                'hash_type': testdata.push_hash_historic_type,
                'output_type': testdata.push_output_type,
                'output_params': {
                    'delivery_frequency':
                    testdata.push_output_params['delivery_frequency'],
                    'url':
                    testdata.push_output_params['url'],
                    'auth': {
                        'type': testdata.push_output_params['auth.type'],
                        'username':
                        testdata.push_output_params['auth.username'],
                        'password':
                        testdata.push_output_params['auth.password'],
                    },
                },
                'last_request': None,
                'last_success': None
            },
            'rate_limit': 200,
            'rate_limit_remaining': 150,
        }
        self.mock_api_client.set_response(response)

        pushsub = self.pushdef.subscribe_definition(definition,
                                                    testdata.push_name)

        self.assertEqual(pushsub.get_id(), testdata.push_id,
                         'The subscription ID is incorrect')
        self.assertEqual(pushsub.get_name(), testdata.push_name,
                         'The subscription name is incorrect')
        self.assertEqual(pushsub.get_created_at(), testdata.push_created_at,
                         'The subscription created_at is incorrect')
        self.assertEqual(pushsub.get_status(), testdata.push_status,
                         'The subscription status is incorrect')
        self.assertEqual(pushsub.get_output_type(), testdata.push_output_type,
                         'The subscription output_type is incorrect')
        self.assertEqual(pushsub.get_hash_type(),
                         testdata.push_hash_historic_type,
                         'The subscription hash_type is incorrect')
        self.assertEqual(pushsub.get_hash(), testdata.historic_id,
                         'The subscription hash is incorrect')
        self.assertEqual(pushsub.get_output_param('delivery_frequency'),
                         testdata.push_output_params['delivery_frequency'],
                         'The subscription delivery_frequency is incorrect')
        self.assertEqual(pushsub.get_output_param('url'),
                         testdata.push_output_params['url'],
                         'The subscription url is incorrect')
        self.assertEqual(pushsub.get_output_param('auth.type'),
                         testdata.push_output_params['auth.type'],
                         'The subscription auth.type is incorrect')
        self.assertEqual(pushsub.get_output_param('auth.username'),
                         testdata.push_output_params['auth.username'],
                         'The subscription auth.username is incorrect')
        self.assertEqual(pushsub.get_output_param('auth.password'),
                         testdata.push_output_params['auth.password'],
                         'The subscription auth.password is incorrect')