def test_get_pi_points(managed_feeds_postgres_manager, postgres_session): postgres_session.add_all([ PiPoint(pi_point='point1', subscription_status=SubscriptionStatus.subscribed, update_timestamp=datetime.datetime(2017, 1, 1)), PiPoint(pi_point='point2', subscription_status=SubscriptionStatus.pending, update_timestamp=datetime.datetime(2017, 1, 2)), PiPoint(pi_point='point3', subscription_status=SubscriptionStatus.unsubscribed, update_timestamp=datetime.datetime(2017, 1, 3)) ]) points_data = managed_feeds_postgres_manager.get_pi_points() points_data['pi_points'] = sorted(points_data['pi_points'], key=itemgetter('pi_point')) assert points_data == { 'pi_points': [{ 'pi_point': 'point1', 'subscription_status': SubscriptionStatus.subscribed, 'update_timestamp': datetime.datetime(2017, 1, 1, 0, 0) }, { 'pi_point': 'point2', 'subscription_status': SubscriptionStatus.pending, 'update_timestamp': datetime.datetime(2017, 1, 2, 0, 0) }, { 'pi_point': 'point3', 'subscription_status': SubscriptionStatus.unsubscribed, 'update_timestamp': datetime.datetime(2017, 1, 3, 0, 0) }], 'total_count': 3 }
def test_send_unsubscribe_request(managed_feeds_postgres_manager, incoming_queue, sqs_uuid4, postgres_session): postgres_session.add_all( [PiPoint(pi_point='point1'), PiPoint(pi_point='point2')]) sqs_uuid4.return_value = '1' managed_feeds_postgres_manager.send_unsubscribe_request( ['point1', 'point2']) points = postgres_session.query(PiPoint).all() event = postgres_session.query(Event).get('1') assert all(point.subscription_status == SubscriptionStatus.pending for point in points) assert event.pi_points == ['point1', 'point2'] assert event.event_type == 'unsubscribe' assert event.status == EventStatus.pending assert incoming_queue.messages == [{ 'id': '1', 'action': 'unsubscribe', 'created_at': '2016-01-02T11:12:13', 'payload': { 'points': ['point1', 'point2'] } }]
def test_handle_sync_pi_points(managed_feeds_postgres_manager, postgres_session, s3_resource): postgres_session.add_all([ PiPoint(pi_point='point1', subscription_status=SubscriptionStatus.pending), PiPoint(pi_point='point2', subscription_status=SubscriptionStatus.pending), PiPoint(pi_point='point3', subscription_status=SubscriptionStatus.subscribed), PiPoint(pi_point='point4', subscription_status=SubscriptionStatus.subscribed), ]) postgres_session.add( SyncPiPointsEvent(id='1', status=EventStatus.pending, s3_bucket='bucket', s3_key='pi_points.json')) s3_resource.Bucket('bucket').upload_fileobj( BytesIO(b'["point1","point3","point5"]'), 'pi_points.json') payload = {} managed_feeds_postgres_manager.handle_sync_pi_points('1', payload) points = postgres_session.query(PiPoint).all() point1 = postgres_session.query(PiPoint).get('point1') point3 = postgres_session.query(PiPoint).get('point3') point5 = postgres_session.query(PiPoint).get('point5') event = postgres_session.query(Event).get('1') assert len(points) == 3 assert point1.subscription_status == SubscriptionStatus.pending assert point3.subscription_status == SubscriptionStatus.subscribed assert point5.subscription_status == SubscriptionStatus.unsubscribed assert event.status == EventStatus.success
def test_search_pi_points_with_status(managed_feeds_postgres_manager, postgres_session): postgres_session.add_all([ PiPoint(pi_point='name1', subscription_status=SubscriptionStatus.subscribed, update_timestamp=datetime.datetime(2017, 1, 1)), PiPoint(pi_point='name2', subscription_status=SubscriptionStatus.pending, update_timestamp=datetime.datetime(2017, 1, 2)), PiPoint(pi_point='name3', subscription_status=SubscriptionStatus.unsubscribed, update_timestamp=datetime.datetime(2017, 1, 3)) ]) points_data = managed_feeds_postgres_manager.search_pi_points( status='subscribed') assert points_data == { 'pi_points': [{ 'pi_point': 'name1', 'subscription_status': SubscriptionStatus.subscribed, 'update_timestamp': datetime.datetime(2017, 1, 1, 0, 0) }], 'total_count': 1 }
def test_search_pi_points_with_pi_points(managed_feeds_postgres_manager, postgres_session): postgres_session.add_all([ PiPoint(pi_point='name1', subscription_status=SubscriptionStatus.subscribed, update_timestamp=datetime.datetime(2017, 1, 1)), PiPoint(pi_point='name2', subscription_status=SubscriptionStatus.subscribed, update_timestamp=datetime.datetime(2017, 1, 2)), PiPoint(pi_point='name3', subscription_status=SubscriptionStatus.subscribed, update_timestamp=datetime.datetime(2017, 1, 3)) ]) points_data = managed_feeds_postgres_manager.search_pi_points( pi_points=['name1', 'name3']) points_data['pi_points'] = sorted(points_data['pi_points'], key=itemgetter('pi_point')) assert points_data == { 'pi_points': [{ 'pi_point': 'name1', 'subscription_status': SubscriptionStatus.subscribed, 'update_timestamp': datetime.datetime(2017, 1, 1, 0, 0) }, { 'pi_point': 'name3', 'subscription_status': SubscriptionStatus.subscribed, 'update_timestamp': datetime.datetime(2017, 1, 3, 0, 0) }], 'total_count': 2 }
def test_get_pi_points_with_pagination(managed_feeds_postgres_manager, postgres_session): for i in range(10): pi_point = PiPoint(pi_point='point{}'.format(i), subscription_status=SubscriptionStatus.subscribed, update_timestamp=datetime.datetime(2017, 1, 1)) postgres_session.add(pi_point) points_data = managed_feeds_postgres_manager.get_pi_points(page=2, page_size=2) points_data['pi_points'] = sorted(points_data['pi_points'], key=itemgetter('pi_point')) assert points_data == { 'pi_points': [{ 'pi_point': 'point4', 'subscription_status': SubscriptionStatus.subscribed, 'update_timestamp': datetime.datetime(2017, 1, 1, 0, 0) }, { 'pi_point': 'point5', 'subscription_status': SubscriptionStatus.subscribed, 'update_timestamp': datetime.datetime(2017, 1, 1, 0, 0) }], 'total_count': 10 }
def test_handle_failed_unsubscribe_request(managed_feeds_postgres_manager, postgres_session): postgres_session.add_all( [PiPoint(pi_point='point1'), PiPoint(pi_point='point2')]) postgres_session.add( UnsubscribeEvent(id='1', status=EventStatus.pending, pi_points=['point1', 'point2'])) payload = {'points': ['point1'], 'error_message': 'point2 failed'} managed_feeds_postgres_manager.handle_unsubscribe_request('1', payload) point1 = postgres_session.query(PiPoint).get('point1') point2 = postgres_session.query(PiPoint).get('point2') event = postgres_session.query(Event).get('1') assert point1.subscription_status == SubscriptionStatus.unsubscribed assert point2.subscription_status == SubscriptionStatus.subscribed assert event.status == EventStatus.failure
def test_handle_unsubscribe_request(managed_feeds_postgres_manager, postgres_session): postgres_session.add_all( [PiPoint(pi_point='point1'), PiPoint(pi_point='point2')]) postgres_session.add( UnsubscribeEvent(id='1', status=EventStatus.pending, pi_points=['point1', 'point2'])) payload = {'points': ['point1', 'point2']} managed_feeds_postgres_manager.handle_unsubscribe_request('1', payload) points = postgres_session.query(PiPoint).all() event = postgres_session.query(Event).get('1') assert all(point.subscription_status == SubscriptionStatus.unsubscribed for point in points) assert event.status == EventStatus.success assert event.pi_points == ['point1', 'point2']
def test_handle_subscribe_request(managed_feeds_postgres_manager, postgres_session, iot_service): postgres_session.add_all( [PiPoint(pi_point='point1'), PiPoint(pi_point='point2')]) postgres_session.add( SubscribeEvent(id='1', status=EventStatus.pending, pi_points=['point1', 'point2'])) payload = {'points': ['point1', 'point2']} managed_feeds_postgres_manager.handle_subscribe_request('1', payload) points = postgres_session.query(PiPoint).all() event = postgres_session.query(Event).get('1') assert all(point.subscription_status == SubscriptionStatus.subscribed for point in points) assert event.status == EventStatus.success assert event.pi_points == ['point1', 'point2'] assert iot_service.iot_client.create_thing.has_calls( [call(thingName='point1'), call(thingName='point2')])
def test_search_pi_points_using_regex(managed_feeds_postgres_manager, postgres_session): postgres_session.add_all([ PiPoint(pi_point='test1', subscription_status=SubscriptionStatus.subscribed, update_timestamp=datetime.datetime(2017, 1, 1)), PiPoint(pi_point='Test12', subscription_status=SubscriptionStatus.pending, update_timestamp=datetime.datetime(2017, 1, 2)) ]) points_data = managed_feeds_postgres_manager.search_pi_points( pattern='[A-Z]{1}[a-z]?[0-9]?', use_regex=True) assert points_data == { 'pi_points': [{ 'pi_point': 'Test12', 'subscription_status': SubscriptionStatus.pending, 'update_timestamp': datetime.datetime(2017, 1, 2, 0, 0) }], 'total_count': 1 }