def test_processPages(table_name): ip = randomIP() this_event = event(ip, table_name) visits = [ itemToVisit(record['dynamodb']['NewImage']) for record in this_event['Records'] if record['dynamodb']['NewImage']['Type']['S'] == 'visit' ] client = DynamoClient(table_name) client.addVisitor(Visitor(ip)) client.addVisits(visits) client.addBrowsers([ itemToBrowser(record['dynamodb']['NewImage']) for record in this_event['Records'] if record['dynamodb']['NewImage']['Type']['S'] == 'browser' ]) for session in [ itemToSession(record['dynamodb']['NewImage']) for record in this_event['Records'] if record['dynamodb']['NewImage']['Type']['S'] == 'session' ]: client.addSession(session) assert processPages( client, this_event ) == 'Successfully added ' + \ f'{ len( { visit.slug for visit in visits } ) } pages and updated 0 ' + \ f'from { len( visits ) } records.'
def test_duplicate_addSession(dynamo_client, table_init, table_name, session): client = DynamoClient(table_name) client.addSession(session) result = client.addSession(session) assert 'error' in result.keys() assert result['error'] == 'Visitor\'s session is already in table ' \ + f'{ session }'
def test_duplicate_addBrowser(dynamo_client, table_init, table_name, browser): client = DynamoClient(table_name) client.addBrowser(browser) result = client.addBrowser(browser) assert 'error' in result.keys() assert result['error'] == 'Visitor\'s browser is already in table ' \ + f'{ browser }'
def test_duplicate_visitor_addNewVisitor(self, table_name, visitor, browsers, visits): client = DynamoClient(table_name) result = client.addVisitor(visitor) result = client.addNewVisitor(visitor, location(), browsers, visits) assert 'error' in result.keys() assert result['error'] == f'Visitor already in table { visitor }'
def test_duplicate_addLocation(table_name): client = DynamoClient(table_name) client.addLocation(location()) result = client.addLocation(location()) assert 'error' in result.keys() assert result['error'] == 'Visitor\'s location is already in table ' \ + f'{ location() }'
def test_none_getSessionDetails( dynamo_client, table_init, table_name, session ): client = DynamoClient( table_name ) result = client.getSessionDetails( session ) assert 'error' in result.keys() assert result['error'] == 'Session not in table'
def test_duplicate_addVisit( dynamo_client, table_init, table_name, visit ): client = DynamoClient( table_name ) client.addVisit( visit ) result = client.addVisit( visit ) assert 'error' in result.keys() assert result['error'] == 'Visitor\'s page visit is already in table ' \ + f'{ visit }'
def test_incrementVisitorSessions(self, table_name, visitor): client = DynamoClient(table_name) client.addVisitor(visitor) result = client.incrementVisitorSessions(visitor) visitor.numberSessions += 1 assert 'visitor' in result.keys() assert result['visitor'] == visitor
def test_updatePage(table_name, year_visits, month_visits, week_visits, day_visits): client = DynamoClient(table_name) result = client.updatePage(month_visits + year_visits + month_visits + week_visits + day_visits) assert 'page' in result.keys() and 'days' in result.keys() and \ 'weeks' in result.keys() and 'months' in result.keys() and \ 'years' in result.keys()
def test_duplicate_session_addNewVisitor(self, table_name, visitor, browsers, visits, session): client = DynamoClient(table_name) result = client.addSession(session) result = client.addNewVisitor(visitor, location(), browsers, visits) assert 'error' in result.keys() assert result['error'] == 'Visitor\'s session is already in table ' + \ f'{ session }'
def test_visitor_addNewSession( dynamo_client, table_init, table_name, visitor, browsers, visits ): client = DynamoClient( table_name ) result = client.addNewSession( visitor, browsers, visits ) assert 'error' in result.keys() assert result['error'] == 'Visitor not in table'
def test_addNewVisitor(self, table_name, visitor, browsers, visits, session): client = DynamoClient(table_name) result = client.addNewVisitor(visitor, location(), browsers, visits) assert 'visitor' in result.keys() assert result['visitor'] == visitor assert 'browsers' in result.keys() assert result['browsers'] == browsers assert 'location' in result.keys() assert dict(result['location']) == dict(location()) assert 'visits' in result.keys() assert result['visits'] == visits assert 'session' in result.keys() assert dict(result['session']) == dict(session)
def test_addNewSession(dynamo_client, table_init, table_name, visitor, browsers, visits): client = DynamoClient(table_name) client.addVisitor(visitor) result = client.addNewSession(visitor, browsers, visits) assert 'visitor' in result.keys() assert result['visitor'] == visitor assert 'browsers' in result.keys() assert result['browsers'] == browsers assert 'visits' in result.keys() assert result['visits'] == visits assert 'session' in result.keys() assert result['session'].ip == visitor.ip assert result['session'].sessionStart == visits[0].date
def test_parameter_title_addPage(dynamo_client, table_init, table_name, year_visits): with pytest.raises(ValueError) as e: assert DynamoClient(table_name).addPage(year_visits + [ Visit('2020-12-23T20:32:26.000Z', '0.0.0.0', '0', 'Resume', '/', '2020-12-23T20:32:26.000Z') ]) assert str(e.value) == 'List of visits must have the same title'
def test_parameter_year_addWeek(dynamo_client, table_init, table_name, week_visits): with pytest.raises(ValueError) as e: assert DynamoClient(table_name).addWeek(week_visits + [ Visit('2021-12-23T20:32:26.000Z', '0.0.0.0', '0', 'Tyler Norlund', '/', '2020-12-23T20:32:26.000Z') ]) assert str(e.value) == 'List of visits must be from the same year and week'
def test_parameter_addBrowsers( dynamo_client, table_init, table_name, ): with pytest.raises(ValueError) as e: assert DynamoClient(table_name).addBrowsers({}) assert str(e.value) == 'Must pass a list'
def test_none_updateSession(dynamo_client, table_init, table_name, browsers, visits, session): client = DynamoClient(table_name) client.addBrowsers(browsers) client.addVisits(visits) result = client.updateSession(session, visits) assert 'error' in result.keys() assert result['error'] == f'Session not in table { session }'
def test_parameter_year_addMonth( dynamo_client, table_init, table_name, month_visits ): with pytest.raises( ValueError ) as e: assert DynamoClient( table_name ).addMonth( month_visits + [ Visit( visitor_id, '2021-12-23T20:32:26.000Z', '0', 'Tyler Norlund', '/', '2020-12-23T20:32:26.000Z', scroll_events ) ] ) assert str( e.value ) == 'List of visits must be from the same year and month'
def test_parameter_title_addDay( dynamo_client, table_init, table_name, day_visits ): with pytest.raises( ValueError ) as e: assert DynamoClient( table_name ).addDay( day_visits + [ Visit( visitor_id, '2020-12-23T20:32:26.000Z', '0', 'Resume', '/', '2020-12-23T20:32:26.000Z', scroll_events ) ] ) assert str( e.value ) == 'List of visits must have the same title'
def test_getVisitorDetails(self, table_name, visitor, browsers, visits): print('location', location()) client = DynamoClient(table_name) result = client.addNewVisitor(visitor, location(), browsers, visits) print('result', result) result = client.getVisitorDetails(visitor) print('result', result) assert 'visitor' in result.keys() assert dict(result['visitor']) == dict(visitor) assert 'browsers' in result.keys() assert all([ dict(result['browsers'][index]) == dict(browsers[index]) for index in range(len(browsers)) ]) assert 'location' in result.keys() assert dict(result['location']) == dict(location()) assert 'visits' in result.keys() assert all([ dict(result['visits'][index]) == dict(visits[index]) for index in range(len(visits)) ]) assert 'sessions' in result.keys()
def test_getPageDetails(table_name, year_visits, month_visits, week_visits, day_visits, page): client = DynamoClient(table_name) client.addVisits(month_visits + year_visits + month_visits + week_visits + day_visits) client.updatePage(month_visits + year_visits + month_visits + week_visits + day_visits) result = client.getPageDetails(page) assert 'page' in result.keys() and 'days' in result.keys() and \ 'weeks' in result.keys() and 'months' in result.keys() and \ 'years' in result.keys()
def test_visits_processPages(table_name): this_event = { 'Records': [{ 'dynamodb': { 'NewImage': { 'Type': { 'S': 'Something' } } } }] } client = DynamoClient(table_name) assert processPages(client, this_event) == 'No pages to process'
def test_getSessionDetails(dynamo_client, table_init, table_name, visitor, browsers, visits, session): client = DynamoClient(table_name) client.addVisitor(visitor) client.addNewSession(visitor, browsers, visits) result = client.getSessionDetails(session) assert 'visits' in result.keys() assert all([ dict(result['visits'][index]) == dict(visits[index]) for index in range(len(visits)) ]) assert 'session' in result.keys() assert result['session'].ip == visitor.ip assert result['session'].sessionStart == visits[0].date
def test_updateSession( dynamo_client, table_init, table_name, visitor, browsers, visits, session, ): client = DynamoClient( table_name ) client.addNewSession( visitor, browsers, visits ) client.addSession( session ) result = client.updateSession( session, visits ) assert 'visits' in result.keys() assert all( [ dict( result['visits'][index] ) == dict(visits[index]) for index in range( len( visits ) ) ] ) assert 'session' in result.keys() assert dict( result['session'] ) == dict( session )
def test_listLocations(table_name): client = DynamoClient(table_name) for loc in locations(): client.addLocation(loc) result = client.listLocations() assert len(result) == len(locations())
def test_parameter_removeLocation(table_name): with pytest.raises(ValueError) as e: assert DynamoClient(table_name).removeLocation({}) assert str(e.value) == 'Must pass a Location object'
def test_none_removeLocation(table_name): result = DynamoClient(table_name).removeLocation(location()) assert 'error' in result.keys() assert result['error'] == f'Location not in table { location() }'
def test_removeLocation(table_name): client = DynamoClient(table_name) client.addLocation(location()) result = client.removeLocation(location()) assert 'location' in result.keys() assert dict(result['location']) == dict(location())
def test_element_addLocations(table_name): with pytest.raises(ValueError) as e: assert DynamoClient(table_name).addLocations([None]) assert str(e.value) == 'Must pass Location objects'
def test_addLocation(table_name): result = DynamoClient(table_name).addLocation(location()) assert 'location' in result.keys() assert dict(result['location']) == dict(location())