Skip to content

Commit

Permalink
setting cell data types in excel, improving cleaning/deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Nov 29, 2016
1 parent e4cef71 commit cc86139
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 162 deletions.
39 changes: 35 additions & 4 deletions tests/schema/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def test___str__(self):

def test_validate_attribute(self):
root = Root()
root.clean()
self.assertEqual(root.validate(), None)

leaf = Leaf()
Expand Down Expand Up @@ -312,99 +313,129 @@ def test_validate_float_attribute(self):

# max=3.
leaf.float2 = 'a'
leaf.clean()
self.assertIn('float2', [x.attribute.name for x in leaf.validate().attributes])

leaf.float2 = 1
leaf.clean()
self.assertIn('float2', [x.attribute.name for x in leaf.validate().attributes])

leaf.float2 = 4
leaf.clean()
self.assertIn('float2', [x.attribute.name for x in leaf.validate().attributes])

leaf.float2 = 3
leaf.clean()
self.assertNotIn('float2', [x.attribute.name for x in leaf.validate().attributes])

leaf.float2 = 3.
leaf.clean()
self.assertNotIn('float2', [x.attribute.name for x in leaf.validate().attributes])

leaf.float2 = 2.
leaf.clean()
self.assertNotIn('float2', [x.attribute.name for x in leaf.validate().attributes])

leaf.float2 = 2.5
leaf.clean()
self.assertNotIn('float2', [x.attribute.name for x in leaf.validate().attributes])

leaf.float2 = float('nan')
leaf.clean()
self.assertIn('float2', [x.attribute.name for x in leaf.validate().attributes])

# max=nan
leaf.float3 = 2.5
leaf.clean()
self.assertNotIn('float3', [x.attribute.name for x in leaf.validate().attributes])

leaf.float3 = float('nan')
leaf.clean()
self.assertIn('float3', [x.attribute.name for x in leaf.validate().attributes])

def test_validate_int_attribute(self):
root = UniqueRoot(int_attr='1.0.')
root.clean()
self.assertIn('int_attr', [x.attribute.name for x in root.validate().attributes])

root.int_attr = '1.5'
root.clean()
self.assertIn('int_attr', [x.attribute.name for x in root.validate().attributes])

root.int_attr = 1.5
root.clean()
self.assertIn('int_attr', [x.attribute.name for x in root.validate().attributes])

root.int_attr = '1.'
root.clean()
self.assertNotIn('int_attr', [x.attribute.name for x in root.validate().attributes])

root.int_attr = 1.
root.clean()
self.assertNotIn('int_attr', [x.attribute.name for x in root.validate().attributes])

root.int_attr = 1
root.clean()
self.assertNotIn('int_attr', [x.attribute.name for x in root.validate().attributes])

root.int_attr = None
root.clean()
self.assertNotIn('int_attr', [x.attribute.name for x in root.validate().attributes])

def test_validate_pos_int_attribute(self):
root = UniqueRoot(pos_int_attr='0.')
root.clean()
self.assertIn('pos_int_attr', [x.attribute.name for x in root.validate().attributes])

root.pos_int_attr = 1.5
root.clean()
self.assertIn('pos_int_attr', [x.attribute.name for x in root.validate().attributes])

root.pos_int_attr = -1
root.clean()
self.assertIn('pos_int_attr', [x.attribute.name for x in root.validate().attributes])

root.pos_int_attr = 0
root.clean()
self.assertIn('pos_int_attr', [x.attribute.name for x in root.validate().attributes])

root.pos_int_attr = 1.
root.clean()
self.assertNotIn('pos_int_attr', [x.attribute.name for x in root.validate().attributes])

root.pos_int_attr = 1
root.clean()
self.assertNotIn('pos_int_attr', [x.attribute.name for x in root.validate().attributes])

root.pos_int_attr = None
root.clean()
self.assertNotIn('pos_int_attr', [x.attribute.name for x in root.validate().attributes])

def test_validate_enum_attribute(self):
leaf = UnrootedLeaf()
leaf.clean()

self.assertIn('enum2', [x.attribute.name for x in leaf.validate().attributes])
self.assertNotIn('enum3', [x.attribute.name for x in leaf.validate().attributes])

leaf.enum2 = Order['root']
leaf.clean()
self.assertNotIn('enum2', [x.attribute.name for x in leaf.validate().attributes])

leaf.enum2 = 'root'
leaf.clean()
self.assertNotIn('enum2', [x.attribute.name for x in leaf.validate().attributes])

leaf.enum2 = 1
leaf.clean()
self.assertNotIn('enum2', [x.attribute.name for x in leaf.validate().attributes])

leaf.enum2 = 'root2'
leaf.clean()
self.assertIn('enum2', [x.attribute.name for x in leaf.validate().attributes])

leaf.enum2 = 3
leaf.clean()
self.assertIn('enum2', [x.attribute.name for x in leaf.validate().attributes])

def test_validate_manytoone_attribute(self):
Expand All @@ -423,14 +454,14 @@ def set_root():
unrooted_leaf = UnrootedLeaf()
self.assertNotIn('root2', [x.attribute.name for x in unrooted_leaf.validate().attributes])

def test_validate_objects(self):
def test_clean_and_validate_objects(self):
grandparent = Grandparent(id='root')
parents = [
Parent(grandparent=grandparent, id='node-0'),
Parent(grandparent=grandparent),
]

errors = core.validate_objects(parents)
errors = core.clean_and_validate_objects(parents)
self.assertEqual(len(errors.objects), 1)
self.assertEqual(errors.objects[0].object, parents[0])
self.assertEqual(len(errors.objects[0].attributes), 1)
Expand All @@ -441,15 +472,15 @@ def test_validate_objects(self):
Root(label='root-0'),
Root(label='root-0'),
]
errors = core.validate_objects(roots)
errors = core.clean_and_validate_objects(roots)
self.assertEqual(errors, None)

roots = [
UniqueRoot(label='root_0', url='http://www.test.com'),
UniqueRoot(label='root_0', url='http://www.test.com'),
UniqueRoot(label='root_0', url='http://www.test.com'),
]
errors = core.validate_objects(roots)
errors = core.clean_and_validate_objects(roots)
self.assertEqual(len(errors.objects), 0)
self.assertEqual(len(errors.models), 1)
self.assertEqual(errors.models[0].model, UniqueRoot)
Expand Down
4 changes: 3 additions & 1 deletion tests/schema/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def test_write_read(self):
objects = set((root, )) | root.get_related()
objects = utils.group_objects_by_model(objects)

ExcelIo.write(self.filename, {Root: set((root, ))}, [Root, ])
root.clean()

ExcelIo.write(self.filename, set((root,)), [Root, ])
objects2 = ExcelIo.read(self.filename, set((Root, Node, Leaf, )))

self.assertEqual(len(objects2[Root]), 1)
Expand Down
Loading

0 comments on commit cc86139

Please sign in to comment.