def test_retrieving_with_invalid_type(self): Log.objects().delete() answer = [{ 'userId': '12345', 'sessionId': 'asdfg', 'actions': [{ 'type': 'CLICK', 'properties': { 'locationX': 52, 'locationY': 22 }, 'time': '2018-10-17T21:37:28-06:00' }] }] 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')) log = Log(userId='12345', sessionId='asdfg', actions=[action1, action2]) log.save() r1 = self.w.get('/logs/?types=CLICK,WRONGTYPE,VIEW') assert r1.status_int == 200 assert r1.json == answer log.delete()
def test_add_valid_action_non_upserted_different_session(self): Log.objects().delete() body = [{ 'userId': '1234', 'sessionId': '12345', 'actions': [{ "time": "2018-10-20T21:37:28-06:00", "type": "VIEW", "properties": { "viewedId": "12345" } }] }, { 'userId': '1234', '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") result = 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 == "1234" assert logs[1].sessionId == "ASDF" assert logs[1].actions == [action2] Log.objects().delete()
def test_retrieving_valid_log(self): Log.objects().delete() answer = [{ 'userId': '12345', 'sessionId': 'asdfg', 'actions': [{ 'type': 'CLICK', 'properties': { 'locationX': 52, 'locationY': 22 }, 'time': '2018-10-18T21:37:28-06:00' }] }] 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() r = self.w.get('/logs/?') log.delete() assert r.json == answer
def _createActionModel(self, action): if not action: raise ValueError("EMPTY_ACTION") _properties = action.get('properties') _type = action.get('type') _time = action.get('time') if not _type: raise ValueError("MISSING_TYPE_VALUE") if not _properties: raise ValueError("MISSING_PROPERTIES_VALUE") if not _time: raise ValueError("MISSING_TIME_VALUE") try: datetime.strptime(_time,TIME_FORMAT) except: raise ValueError("INVALID_TIME_FORMAT") propertiesModel = None if _type == 'CLICK': if not _properties.get('locationX'): raise ValueError("MISSING_LOCATION_X_VALUE") if not _properties.get('locationY'): raise ValueError("MISSING_LOCATION_Y_VALUE") propertiesModel=ClickProperties(locationX = _properties.get('locationX'), locationY = _properties.get('locationY')) elif _type == 'VIEW': if not _properties.get('viewedId'): raise ValueError("MISSING_VIEWEDID_VALUE") propertiesModel=ViewProperties(viewedId = _properties.get('viewedId')) elif _type == 'NAVIGATE': if not _properties.get('pageFrom') : raise ValueError("MISSING_PAGEFROM_VALUE") if not _properties.get('pageTo'): raise ValueError("MISSING_PAGETO_VALUE") propertiesModel=NavigateProperties(pageFrom=_properties.get('pageFrom'), pageTo=_properties.get('pageTo')) else: raise ValueError("INVALID_ACTION_TYPE") return Action(_properties=propertiesModel, _time = _time, _type=_type)
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()
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_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()
def test_retrieving_log_by_user(self): Log.objects().delete() answer1 = [{ 'userId': 'ASDF', 'sessionId': '123', 'actions': [{ 'type': 'CLICK', 'properties': { 'locationX': 52, 'locationY': 22 }, 'time': '2018-10-18T21:37:28-06:00' }] }] answer2 = [{ 'userId': '12345', 'sessionId': 'asdfg', 'actions': [{ 'type': 'CLICK', 'properties': { 'locationX': 52, 'locationY': 22 }, 'time': '2018-10-18T21:37:28-06:00' }] }] action = Action(_time='2018-10-18T21:37:28-06:00', _type='CLICK', _properties=ClickProperties(locationX=52, locationY=22)) log1 = Log(userId='12345', sessionId='asdfg', actions=[action]) log2 = Log(userId='ASDF', sessionId='123', actions=[action]) log1.save() log2.save() r1 = self.w.get('/logs/?userId=ASDF') assert r1.json == answer1 r2 = self.w.get('/logs/?userId=12345') assert r2.json == answer2 r2 = self.w.get('/logs/?userId=12') assert r2.json == [] log1.delete() log2.delete()
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
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
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
def 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" } }] }] r = self.w.post_json('/logs/', body, expect_errors=True) assert r.status_int == 200 assert r.json == {'success': True} log = Log.objects() assert log is not None assert len(log) == 2 assert log[0].get('userId') == 'ABC123XYZ' assert log[0].get('sessionId') == 'XYZ456ABC' actions = log[0].get('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")) Log.objects().delete() assert log[1].get('userId') == 'asd' assert log[1].get('sessionId') == 'asdfg' actions = log[1].get('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()
def test_retrieving_logs_with_matching_types(self): Log.objects().delete() answer1 = [ { "userId": "12345", "sessionId": "asdfg", "actions": [{ "time": "2018-10-20T21:37:28-06:00", "properties": { "locationX": 52, "locationY": 22 }, "type": "CLICK" }] }, { "userId": "ASDFG", "sessionId": "12345", "actions": [{ "time": "2019-10-20T21:37:28-06:00", "properties": { "locationX": 60, "locationY": 30 }, "type": "CLICK" }] }, ] answer2 = [ { "userId": "12345", "sessionId": "asdfg", "actions": [{ "time": "2018-10-20T21:37:28-06:00", "properties": { "locationX": 52, "locationY": 22 }, "type": "CLICK" }] }, { "userId": "ASDFG", "sessionId": "12345", "actions": [{ "time": "2019-10-19T21:37:28-06:00", "properties": { "viewedId": "12345", }, "type": "VIEW" }, { "time": "2019-10-20T21:37:28-06:00", "properties": { "locationX": 60, "locationY": 30 }, "type": "CLICK" }] }, ] answer3 = [{ "userId": "12345", "sessionId": "asdfg", "actions": [{ "time": "2018-10-20T21:37:28-06:00", "properties": { "pageFrom": "X", "pageTo": "Y" }, "type": "NAVIGATE" }] }] action1 = Action(_time='2018-10-20T21: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')) log1 = Log(userId='12345', sessionId='asdfg', actions=[action1, action2]) action3 = Action(_time='2019-10-19T21:37:28-06:00', _type='VIEW', _properties=ViewProperties(viewedId='12345')) action4 = Action(_time='2019-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() r1 = self.w.get('/logs/?types=CLICK') assert r1.json == answer1 r2 = self.w.get('/logs/?types=CLICK,VIEW') assert r2.json == answer2 r3 = self.w.get('/logs/?types=NAVIGATE') assert r3.json == answer3 log1.delete() log2.delete()
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()