Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #100 from multinet-app/csv_validation
Browse files Browse the repository at this point in the history
Add Syntax Validation on CSV upload
  • Loading branch information
jjnesbitt committed Jul 29, 2019
2 parents 3d256fb + d17ae8f commit 0217a55
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 323 deletions.
27 changes: 24 additions & 3 deletions multinet/multinet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from girder.exceptions import RestException

import csv
import re
from io import StringIO
import itertools
import json
Expand Down Expand Up @@ -35,8 +36,11 @@ def graphql_query(query, variables=None):

def validate_csv(rows):
"""Perform any necessary CSV validation, and raise appropriate exceptions."""
# Check for key uniqueness
if ('_key' in rows.fieldnames):
BASE_ERROR_MSG = 'CSV Validation Failed:'

if '_key' in rows.fieldnames and 'name' in rows.fieldnames:
# Node Table, check for key uniqueness

keys = [row['_key'] for row in rows]
uniqueKeys = set()
duplicates = set()
Expand All @@ -47,7 +51,24 @@ def validate_csv(rows):
uniqueKeys.add(key)

if (len(duplicates) > 0):
raise RestException(f'CSV Validation Failed: Duplicate Keys {", ".join(duplicates)}.')
raise RestException(f'{BASE_ERROR_MSG} Duplicate Keys {", ".join(duplicates)}.')
elif '_from' in rows.fieldnames and '_to' in rows.fieldnames:
# Edge Table, check that each cell has the correct format
valid_cell = re.compile('[^/]+/[^/]+')

for i, row in enumerate(rows):
fields = []
if not valid_cell.match(row['_from']):
fields.append('_from')
if not valid_cell.match(row['_to']):
fields.append('_to')

if fields:
# i+2 -> +1 for index offset, +1 due to header row
raise RestException(f'{BASE_ERROR_MSG} Invalid format on fields '
f'({", ".join(fields)}) in row {i+2}')
else:
raise RestException(f'{BASE_ERROR_MSG} Invalid Header Row Format')


def analyze_nested_json(data, int_table_name, leaf_table_name):
Expand Down
320 changes: 0 additions & 320 deletions test/data/membership_invalid.csv

This file was deleted.

3 changes: 3 additions & 0 deletions test/data/membership_invalid_header.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo,bar
members/7,clubs/2
members/7,clubs/3
7 changes: 7 additions & 0 deletions test/data/membership_invalid_nonexistent_tables.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
_from,_to
herewego/7,clubs/2
members/7,sas/3
neitherhere/8,northere/2
members/8,clubs/3
members/8,clubs/5
members/8,clubs/6
5 changes: 5 additions & 0 deletions test/data/membership_invalid_syntax.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_from,_to
members/17,clubs/5
members17,clubs/6
members/18,clubs4
members19,clubs0

0 comments on commit 0217a55

Please sign in to comment.