Skip to content
Snippets Groups Projects
Commit 2658d443 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'f-enum' into 'dev'

ENH: add converter that checks input against enum

See merge request caosdb/caosdb-advanced-user-tools!86
parents d5b3e46b 73a0d038
No related branches found
No related tags found
1 merge request!22Release 0.3
Pipeline #14727 failed
......@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Proof-of-concept integration with Bloxberg.
- Introduce a cfood that can create a Record structure based on the contents of a hdf5 file
h5py is now an optional dependency
- string-in-list check for table imports
### Changed ###
......
......@@ -6,7 +6,7 @@
# Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com>
# Copyright (C) 2021 Henrik tom Wörden <h.tomwoerden@indiscale.com>
# Copyright (C) 2021 Alexander Kreft
# Copyright (C) 2021 Laboratory for Fluid Physics and Biocomplexity,
# Copyright (C) 2021 Laboratory for Fluid Physics and Biocomplexity,
# Max-Planck-Insitute für Dynamik und Selbstorganisation <www.lfpn.ds.mpg.de>
#
# This program is free software: you can redistribute it and/or modify
......
......@@ -148,6 +148,43 @@ def win_path_converter(val):
return path.as_posix()
def string_in_list(val, options, ignore_case=True):
"""Return the given value if it is contained in options, raise an
error otherwise.
Parameters
----------
val : str
String value to be checked.
options : list<str>
List of possible values that val may obtain
ignore_case : bool, optional
Specify whether the comparison of val and the possible options
should ignor capitalization. Default is True.
Returns
-------
val : str
The original value if it is contained in options
Raises
------
ValueError
If val is not contained in options.
"""
if ignore_case:
val = val.lower()
options = [o.lower() for o in options]
if val not in options:
raise ValueError(
"Field value is '{}', but it should be one of the following "
"values: {}.".format(val, ", ".join(
["'{}'".format(o) for o in options])))
return val
class TSVImporter(object):
def __init__(self, converters, obligatory_columns=[], unique_columns=[]):
raise NotImplementedError()
......
......@@ -33,6 +33,7 @@ from caosadvancedtools.table_importer import (XLSImporter, assure_name_format,
incomplete_date_converter,
win_path_converter,
win_path_list_converter,
string_in_list,
yes_no_converter)
......@@ -49,6 +50,16 @@ class ConverterTest(unittest.TestCase):
self.assertRaises(ValueError, yes_no_converter, "True")
self.assertRaises(ValueError, yes_no_converter, "true")
def test_string_in_list(self):
self.assertEqual("false", string_in_list("false",
["FALSE", "TRUE"]))
self.assertEqual("FALSE", string_in_list("FALSE",
["FALSE", "TRUE"], False))
self.assertRaises(ValueError, string_in_list, "FALSE", [])
self.assertRaises(ValueError, string_in_list, "FALSE", ["fals"])
self.assertRaises(ValueError, string_in_list,
"FALSE", ["false"], False)
def test_assure_name_format(self):
self.assertEqual(assure_name_format("Müstermann, Max"),
"Müstermann, Max")
......@@ -62,7 +73,7 @@ class ConverterTest(unittest.TestCase):
["/this/computer"])
self.assertEqual(win_path_list_converter(
r"\this\computer,\this\computer"),
["/this/computer", "/this/computer"])
["/this/computer", "/this/computer"])
@pytest.mark.xfail(reason="To be fixed, see Issue #34")
def test_datetime(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment