Example #1
0
 def test_click_with_missing_locationY(self):
     try:
         LogService._createActionModel({
             "time": "2018-10-20T21:37:28-06:00",
             "type": "CLICK",
             "properties": {
                 "locationX": 1234
             }
         })
         assert False
     except ValueError as e:
         assert str(e) == "MISSING_LOCATION_Y_VALUE"
Example #2
0
 def test_view_with_missing_viewId(self):
     try:
         LogService._createActionModel({
             "time": "2018-10-20T21:37:28-06:00",
             "type": "VIEW",
             "properties": {
                 "locationX": 12345
             }
         })
         assert False
     except ValueError as e:
         assert str(e) == "MISSING_VIEWEDID_VALUE"
Example #3
0
 def test_navigate_with_missing_pageTo(self):
     try:
         LogService._createActionModel({
             "time": "2018-10-20T21:37:28-06:00",
             "type": "NAVIGATE",
             "properties": {
                 "pageFrom": "X"
             }
         })
         assert False
     except ValueError as e:
         assert str(e) == "MISSING_PAGETO_VALUE"
Example #4
0
 def test_invalid_type_input(self):
     try:
         LogService._createActionModel({
             "time": "2018-10-20T21:37:28-06:00",
             "type": "VIEWS",
             "properties": {
                 "viewedId": "12345"
             }
         })
         assert False
     except ValueError as e:
         assert str(e) == "INVALID_ACTION_TYPE"
Example #5
0
 def test_invalid_time_format_2(self):
     try:
         LogService._createActionModel({
             "time": "2018-10-18T21:37:28-04:00",
             "type": "VIEW",
             "properties": {
                 "viewedId": "12345"
             }
         })
         assert False
     except ValueError as e:
         assert str(e) == "INVALID_TIME_FORMAT"
Example #6
0
    def test_add_valid_action_non_upserted(self):
        Log.objects().delete()
        body = [{
            'userId':
            '1234',
            'sessionId':
            '12345',
            'actions': [{
                "time": "2018-10-20T21:37:28-06:00",
                "type": "VIEW",
                "properties": {
                    "viewedId": "12345"
                }
            }]
        }, {
            'userId':
            '12345',
            'sessionId':
            'ASDF',
            'actions': [{
                "time": "2018-10-20T21:37:28-06:00",
                "type": "NAVIGATE",
                "properties": {
                    "pageFrom": "X",
                    "pageTo": "Y"
                }
            }]
        }]
        action1 = Action(_type="VIEW",
                         _properties=ViewProperties(viewedId="12345"),
                         _time="2018-10-20T21:37:28-06:00")
        action2 = Action(_type="NAVIGATE",
                         _properties=NavigateProperties(pageFrom="X",
                                                        pageTo="Y"),
                         _time="2018-10-20T21:37:28-06:00")
        LogService.add_logs(body)

        logs = Log.objects()
        assert logs is not None
        assert len(logs) == 2
        assert logs[0].userId == "1234"
        assert logs[0].sessionId == "12345"
        assert logs[0].actions == [action1]

        assert logs[1].userId == "12345"
        assert logs[1].sessionId == "ASDF"
        assert logs[1].actions == [action2]
        Log.objects().delete()
Example #7
0
    def test_logs_with_an_existing_log(self):
        Log.objects().delete()
        answer = [{
            'userId':
            '12345',
            'sessionId':
            'asdfg',
            'actions': [{
                'time': '2018-10-18T21:37:28-06:00',
                'type': 'CLICK',
                'properties': {
                    'locationX': 52,
                    'locationY': 22
                }
            }]
        }]
        action = Action(_time='2018-10-18T21:37:28-06:00',
                        _type='CLICK',
                        _properties=ClickProperties(locationX=52,
                                                    locationY=22))
        log = Log(userId='12345', sessionId='asdfg', actions=[action])
        log.save()

        result = LogService.get_logs(None, None, None, None)
        assert result is not None
        assert result == answer
        log.delete()
Example #8
0
 def test_add_no_log(self):
     Log.objects().delete()
     try:
         result = LogService.add_logs(None)
         assert False
     except Exception as e:
         assert str(e) == "NO_LOGS_PROVIDED"
Example #9
0
    def test_add_valid_action(self):
        Log.objects().delete()
        body = [{
            'userId':
            '1234',
            'sessionId':
            '12345',
            'actions': [{
                "time": "2018-10-20T21:37:28-06:00",
                "type": "VIEW",
                "properties": {
                    "viewedId": "12345"
                }
            }]
        }]
        action = Action(_type="VIEW",
                        _properties=ViewProperties(viewedId="12345"),
                        _time="2018-10-20T21:37:28-06:00")
        result = LogService.add_logs(body)

        logs = Log.objects(userId="1234")
        assert logs is not None
        assert len(logs) == 1
        assert logs[0].userId == "1234"
        assert logs[0].sessionId == "12345"
        assert logs[0].actions == [action]
        Log.objects().delete()
Example #10
0
def add_logs():
    data = request.get_json()
    try:
        result = LogService.add_logs(data)
    except ValueError as e:
            return {'success': False, 'code': str(e) }, 400
    return jsonify(result), 200
Example #11
0
 def test_miss_action(self):
     Log.objects().delete()
     try:
         body = [{'userId': '12345', 'sessionId': '12345', 'actions': []}]
         result = LogService.add_logs(body)
         assert False
     except ValueError as e:
         assert str(e) == "MISSING_ACTIONS"
Example #12
0
 def test_invalid_datetime_format_logs_3(self):
     Log.objects().delete()
     try:
         result = LogService.get_logs(None, '2018-10-20T21:37:28-04:00',
                                      '2018-10-20T21:37:28-04:00', None)
         assert False
     except Exception as e:
         assert str(e) == "INVALID_TIME_FORMAT"
Example #13
0
    def test_db_doesnt_add_body_if_any_log_is_invalid(self):
        Log.objects().delete()

        body = [{
            "userId":
            "ABC123XYZ",
            "sessionId":
            "XYZ456ABC",
            "actions": [{
                "time": "2018-10-18T21:37:28-06:00",
                "type": "CLICK",
                "properties": {
                    "locationX": 52,
                    "locationY": 11
                }
            }, {
                "time": "2018-10-18T21:37:30-06:00",
                "type": "VIEW",
                "properties": {
                    "viewedId": "FDJKLHSLD"
                }
            }, {
                "time": "2018-10-18T21:37:30-06:00",
                "type": "NAVIGATE",
                "properties": {
                    "pageFrom": "communities",
                    "pageTo": "inventory"
                }
            }]
        }, {
            "userId":
            "asd",
            "sessionId":
            "asdfg",
            "actions": [{
                "time": "2018-10-18T21:37:28-06:00",
                "type": "CLICK",
                "properties": {
                    "locationX": 60,
                    "locationY": 70
                }
            }, {
                "time": "2018-10-20T21:37:28-06:00",
                "type": "NAVIGATE",
                "properties": {
                    "viewedId": "1234",
                }
            }]
        }]
        try:
            result = LogService.add_logs(body)
            False
        except Exception as e:
            assert str(e) == "MISSING_PAGEFROM_VALUE"
            log = Log.objects()
            assert log is not None
            assert len(log) == 0
Example #14
0
    def test_logs_with_an_matching_log_userId(self):
        Log.objects().delete()
        answer = [{
            "userId":
            "12345",
            "sessionId":
            "asdfg",
            "actions": [{
                "time": "2018-10-18T21:37:28-06:00",
                "properties": {
                    "locationX": 52,
                    "locationY": 22
                },
                "type": "CLICK",
            }, {
                "time": "2018-10-20T21:37:28-06:00",
                "properties": {
                    "pageFrom": "X",
                    "pageTo": "Y"
                },
                "type": "NAVIGATE"
            }]
        }]
        action1 = Action(_time='2018-10-18T21:37:28-06:00',
                         _type='CLICK',
                         _properties=ClickProperties(locationX=52,
                                                     locationY=22))
        action2 = Action(_time='2018-10-20T21:37:28-06:00',
                         _type='NAVIGATE',
                         _properties=NavigateProperties(pageFrom='X',
                                                        pageTo='Y'))
        log = Log(userId='12345',
                  sessionId='asdfg',
                  actions=[action1, action2])
        log.save()

        result1 = LogService.get_logs('12345', None, None, None)
        assert result1 is not None
        assert result1 == answer
        result2 = LogService.get_logs('asdf', None, None, None)
        assert result2 is not None
        assert len(result2) == 0
        log.delete()
Example #15
0
 def test_construct_filter_array_from_to_time(self):
     answer = [{
         '$gte': ['$$action.time', '2017-10-18T21:37:28-06:00']
     }, {
         '$lte': ['$$action.time', '2018-10-18T21:37:28-06:00']
     }]
     result = LogService._construct_filter_array(
         '2017-10-18T21:37:28-06:00', '2018-10-18T21:37:28-06:00', None)
     assert result is not None
     assert result == answer
Example #16
0
 def test_construct_all_filter_fields_not_None(self):
     answer = [{
         '$gte': ['$$action.time', '2017-10-18T21:37:28-06:00']
     }, {
         '$lte': ['$$action.time', '2018-10-18T21:37:28-06:00']
     }, {
         '$in': ['$$action.type', ['CLICK', 'VIEW', 'NAVIGATE']]
     }]
     result = LogService._construct_filter_array(
         '2017-10-18T21:37:28-06:00', '2018-10-18T21:37:28-06:00',
         ['CLICK', 'VIEW', 'NAVIGATE'])
     assert result is not None
     assert result == answer
Example #17
0
def get_logs():
    
    userId = request.args.get('userId')
    from_date = request.args.get('from')
    to_date = request.args.get('to')
    types = request.args.get('types')

    typesList = []
    if types is not None: typesList = types.split(',')

    try:
        result = LogService.get_logs(userId, from_date, to_date, typesList)
    except ValueError as e:
            return {'success': False, 'code': str(e) }, 400
    return jsonify(result)
Example #18
0
 def test_construct_match_array_from_to_time(self):
     answer = [{
         'actions.time': {
             '$gte': '2017-10-17T21:37:28-06:00'
         }
     }, {
         'actions.time': {
             '$lte': '2018-10-17T21:37:28-06:00'
         }
     }]
     result = LogService._construct_match_array(
         None, '2017-10-17T21:37:28-06:00', '2018-10-17T21:37:28-06:00',
         None)
     assert result is not None
     assert result == answer
Example #19
0
 def test_create_valid_view(self):
     try:
         answer = Action(_time="2018-10-20T21:37:28-06:00",
                         _type="VIEW",
                         _properties=ViewProperties(viewedId='12345'))
         result = LogService._createActionModel({
             "time": "2018-10-20T21:37:28-06:00",
             "type": "VIEW",
             "properties": {
                 "viewedId": "12345"
             }
         })
         assert result is not None
         assert answer == result
     except ValueError as e:
         assert False
Example #20
0
 def test_miss_properties(self):
     Log.objects().delete()
     try:
         body = [{
             'userId':
             '12345',
             'sessionId':
             '12345',
             'actions': [{
                 "time": "2018-10-20T21:37:28-06:00",
                 "type": "VIEW"
             }]
         }]
         result = LogService.add_logs(body)
         assert False
     except ValueError as e:
         assert str(e) == "MISSING_PROPERTIES_VALUE"
Example #21
0
 def test_create_valid_navigate(self):
     try:
         answer = Action(_time="2018-10-20T21:37:28-06:00",
                         _type="NAVIGATE",
                         _properties=NavigateProperties(pageFrom='X',
                                                        pageTo='Y'))
         result = LogService._createActionModel({
             "time": "2018-10-20T21:37:28-06:00",
             "type": "NAVIGATE",
             "properties": {
                 "pageFrom": "X",
                 "pageTo": "Y"
             }
         })
         assert result is not None
         assert answer == result
     except ValueError as e:
         assert False
Example #22
0
 def test_miss_sessionId(self):
     Log.objects().delete()
     try:
         body = [{
             'userId':
             '12345',
             'actions': [{
                 "time": "2018-10-20T21:37:28-06:00",
                 "type": "VIEW",
                 "properties": {
                     "viewedId": "12345"
                 }
             }]
         }]
         result = LogService.add_logs(body)
         assert False
     except ValueError as e:
         assert str(e) == "MISSING_SESSIONID"
Example #23
0
    def test_create_valid_click(self):
        try:
            answer = Action(_time="2018-10-20T21:37:28-06:00",
                            _type="CLICK",
                            _properties=ClickProperties(locationX=23,
                                                        locationY=1234))
            result = LogService._createActionModel({
                "time": "2018-10-20T21:37:28-06:00",
                "type": "CLICK",
                "properties": {
                    "locationX": 23,
                    "locationY": 1234
                }
            })

            assert result is not None
            assert answer == result
        except ValueError as e:
            assert False
Example #24
0
 def test_result_success(self):
     Log.objects().delete()
     body = [{
         'userId':
         '12345',
         'sessionId':
         '12345',
         'actions': [{
             "time": "2018-10-20T21:37:28-06:00",
             "type": "VIEW",
             "properties": {
                 "viewedId": "12345"
             }
         }]
     }]
     result = LogService.add_logs(body)
     assert result is not None
     assert result.get('success') is True
     Log.objects().delete()
Example #25
0
 def test_miss_time(self):
     Log.objects().delete()
     try:
         body = [{
             'userId':
             '12345',
             'sessionId':
             '12345',
             'actions': [{
                 "type": "VIEW",
                 "properties": {
                     "viewedId": "12345"
                 }
             }]
         }]
         result = LogService.add_logs(body)
         assert False
     except ValueError as e:
         assert str(e) == "MISSING_TIME_VALUE"
Example #26
0
 def test_construct_all_match_fields_not_None(self):
     answer = [{
         'userId': '12345'
     }, {
         'actions.time': {
             '$gte': '2017-10-17T21:37:28-06:00'
         }
     }, {
         'actions.time': {
             '$lte': '2018-10-17T21:37:28-06:00'
         }
     }, {
         'actions.type': {
             '$in': ['CLICK', 'VIEW', 'NAVIGATE']
         }
     }]
     result = LogService._construct_match_array(
         '12345', '2017-10-17T21:37:28-06:00', '2018-10-17T21:37:28-06:00',
         ['CLICK', 'VIEW', 'NAVIGATE'])
     assert result is not None
     assert result == answer
Example #27
0
 def test_construct_match_types(self):
     answer = [{'actions.type': {'$in': ['CLICK', 'VIEW', 'NAVIGATE']}}]
     result = LogService._construct_match_array(
         None, None, None, ['CLICK', 'VIEW', 'NAVIGATE'])
     assert result is not None
     assert result == answer
Example #28
0
 def test_construct_match_array_userId(self):
     answer = [{'userId': '12345'}]
     result = LogService._construct_match_array('12345', None, None, None)
     assert result is not None
     assert result == answer
Example #29
0
    def test_logs_with_a_from_and_to(self):
        Log.objects().delete()
        answer1 = [
            {
                "userId":
                "12345",
                "sessionId":
                "asdfg",
                "actions": [{
                    "time": "2018-10-17T21:37:28-06:00",
                    "properties": {
                        "locationX": 52,
                        "locationY": 22
                    },
                    "type": "CLICK"
                }]
            },
            {
                "userId":
                "ASDFG",
                "sessionId":
                "12345",
                "actions": [{
                    "time": "2018-10-18T21:37:28-06:00",
                    "properties": {
                        "viewedId": "12345"
                    },
                    "type": "VIEW"
                }]
            },
        ]

        answer2 = [
            {
                "userId":
                "12345",
                "sessionId":
                "asdfg",
                "actions": [{
                    "time": "2018-10-19T21:37:28-06:00",
                    "properties": {
                        "pageFrom": 'X',
                        "pageTo": 'Y'
                    },
                    "type": "NAVIGATE"
                }]
            },
            {
                "userId":
                "ASDFG",
                "sessionId":
                "12345",
                "actions": [{
                    "time": "2018-10-20T21:37:28-06:00",
                    "properties": {
                        "locationX": 60,
                        "locationY": 30
                    },
                    "type": "CLICK"
                }]
            },
        ]
        answer3 = [{
            "userId":
            "ASDFG",
            "sessionId":
            "12345",
            "actions": [{
                "time": "2018-10-18T21:37:28-06:00",
                "properties": {
                    "viewedId": "12345",
                },
                "type": "VIEW"
            }]
        }]

        action1 = Action(_time='2018-10-17T21:37:28-06:00',
                         _type='CLICK',
                         _properties=ClickProperties(locationX=52,
                                                     locationY=22))
        action2 = Action(_time='2018-10-19T21:37:28-06:00',
                         _type='NAVIGATE',
                         _properties=NavigateProperties(pageFrom='X',
                                                        pageTo='Y'))
        log1 = Log(userId='12345',
                   sessionId='asdfg',
                   actions=[action1, action2])
        action3 = Action(_time='2018-10-18T21:37:28-06:00',
                         _type='VIEW',
                         _properties=ViewProperties(viewedId='12345'))
        action4 = Action(_time='2018-10-20T21:37:28-06:00',
                         _type='CLICK',
                         _properties=ClickProperties(locationX=60,
                                                     locationY=30))
        log2 = Log(userId='ASDFG',
                   sessionId='12345',
                   actions=[action3, action4])
        log1.save()
        log2.save()
        result1 = LogService.get_logs(None, None, '2018-10-19T00:00:00-06:00',
                                      None)
        assert result1 is not None
        assert result1 == answer1

        result2 = LogService.get_logs(None, '2018-10-19T00:00:00-06:00', None,
                                      None)
        assert result2 is not None
        assert result2 == answer2

        result3 = LogService.get_logs(None, '2018-10-18T00:00:00-06:00',
                                      '2018-10-19T00:00:00-06:00', None)
        assert result3 is not None
        assert result3 == answer3
        log1.delete()
        log2.delete()
Example #30
0
 def test_empty_logs(self):
     Log.objects().delete()
     result = LogService.get_logs(None, None, None, None)
     assert len(result) == 0