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()
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
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"
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()
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"
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
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"
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"
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()
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"
def test_add_valid_log(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": { "pageFrom": "X", "pageTo": "Y" } }] }] result = LogService.add_logs(body) assert result == {'success': True} log = Log.objects() assert log is not None assert len(log) == 2 assert log[0].userId == 'ABC123XYZ' assert log[0].sessionId == 'XYZ456ABC' actions = log[0].actions assert actions is not None assert len(actions) == 3 assert actions[0] == Action(_time="2018-10-18T21:37:28-06:00", _type="CLICK", _properties=ClickProperties(locationX=52, locationY=11)) assert actions[1] == Action( _time="2018-10-18T21:37:30-06:00", _type="VIEW", _properties=ViewProperties(viewedId="FDJKLHSLD")) assert actions[2] == Action(_time="2018-10-18T21:37:30-06:00", _type="NAVIGATE", _properties=NavigateProperties( pageFrom="communities", pageTo="inventory")) assert log[1].userId == 'asd' assert log[1].sessionId == 'asdfg' actions = log[1].actions assert actions is not None assert len(actions) == 2 assert actions[0] == Action(_time="2018-10-18T21:37:28-06:00", _type="CLICK", _properties=ClickProperties(locationX=60, locationY=70)) assert actions[1] == Action(_time="2018-10-20T21:37:28-06:00", _type="NAVIGATE", _properties=NavigateProperties( pageFrom="X", pageTo="Y")) Log.objects().delete()