|
8 | 8 | import random |
9 | 9 | import tempfile |
10 | 10 | import threading |
| 11 | +import math |
11 | 12 |
|
12 | 13 | import pytest |
13 | 14 | from unittest.mock import ANY, patch, create_autospec, Mock, call, MagicMock |
@@ -1415,3 +1416,217 @@ def test_generate_sync_manifest(syn): |
1415 | 1416 | patch_write_manifest_data.assert_called_with( |
1416 | 1417 | manifest_path, ["path", "parent"], patch_walk_directory_tree.return_value |
1417 | 1418 | ) |
| 1419 | + |
| 1420 | + |
| 1421 | +class TestConvertCellInManifestToPythonTypes(object): |
| 1422 | + """ |
| 1423 | + Test the _convert_cell_in_manifest_to_python_types function for each type |
| 1424 | + and single/multiple values. |
| 1425 | + """ |
| 1426 | + |
| 1427 | + def test_datetime_single_item(self) -> None: |
| 1428 | + # GIVEN a single item datetime string |
| 1429 | + datetime_string = "2020-01-01T00:00:00.000Z" |
| 1430 | + datetime_string_in_brackets = "[2020-01-01T00:00:00.000Z]" |
| 1431 | + |
| 1432 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1433 | + # THEN I expect the output to be a datetime object |
| 1434 | + assert synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1435 | + datetime_string |
| 1436 | + ) == datetime.datetime(2020, 1, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc) |
| 1437 | + assert synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1438 | + datetime_string_in_brackets |
| 1439 | + ) == datetime.datetime(2020, 1, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc) |
| 1440 | + |
| 1441 | + def test_datetime_multiple_items(self) -> None: |
| 1442 | + # GIVEN a multiple item datetime string |
| 1443 | + datetime_string = "[2020-01-01T00:00:00.000Z, 2020-01-02T00:00:00.000Z]" |
| 1444 | + |
| 1445 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1446 | + # THEN I expect the output to be a list of datetime objects |
| 1447 | + assert synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1448 | + datetime_string |
| 1449 | + ) == [ |
| 1450 | + datetime.datetime(2020, 1, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc), |
| 1451 | + datetime.datetime(2020, 1, 2, 0, 0, 0, 0, tzinfo=datetime.timezone.utc), |
| 1452 | + ] |
| 1453 | + |
| 1454 | + def test_bool_single_item(self) -> None: |
| 1455 | + # GIVEN a single item bool string |
| 1456 | + bool_string = "TrUe" |
| 1457 | + bool_string_in_brackets = "[tRue]" |
| 1458 | + |
| 1459 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1460 | + # THEN I expect the output to be a bool object |
| 1461 | + assert ( |
| 1462 | + synapseutils.sync._convert_cell_in_manifest_to_python_types(bool_string) |
| 1463 | + is True |
| 1464 | + ) |
| 1465 | + assert ( |
| 1466 | + synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1467 | + bool_string_in_brackets |
| 1468 | + ) |
| 1469 | + is True |
| 1470 | + ) |
| 1471 | + |
| 1472 | + def test_bool_multiple_items(self) -> None: |
| 1473 | + # GIVEN a multiple item bool string |
| 1474 | + bool_string = "[TrUe, fAlse]" |
| 1475 | + |
| 1476 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1477 | + # THEN I expect the output to be a list of bool objects |
| 1478 | + assert synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1479 | + bool_string |
| 1480 | + ) == [True, False] |
| 1481 | + |
| 1482 | + def test_int_single_item(self) -> None: |
| 1483 | + # GIVEN a single item int string |
| 1484 | + int_string = "123" |
| 1485 | + int_string_in_brackets = "[123]" |
| 1486 | + |
| 1487 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1488 | + # THEN I expect the output to be a int object |
| 1489 | + assert ( |
| 1490 | + synapseutils.sync._convert_cell_in_manifest_to_python_types(int_string) |
| 1491 | + == 123 |
| 1492 | + ) |
| 1493 | + assert ( |
| 1494 | + synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1495 | + int_string_in_brackets |
| 1496 | + ) |
| 1497 | + == 123 |
| 1498 | + ) |
| 1499 | + |
| 1500 | + def test_int_multiple_items(self) -> None: |
| 1501 | + # GIVEN a multiple item int string |
| 1502 | + int_string = "[123, 456]" |
| 1503 | + |
| 1504 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1505 | + # THEN I expect the output to be a list of int objects |
| 1506 | + assert synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1507 | + int_string |
| 1508 | + ) == [123, 456] |
| 1509 | + |
| 1510 | + def test_float_single_item(self) -> None: |
| 1511 | + # GIVEN a single item float string |
| 1512 | + float_string = "123.456" |
| 1513 | + float_string_in_brackets = "[123.456]" |
| 1514 | + |
| 1515 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1516 | + # THEN I expect the output to be a float object |
| 1517 | + assert math.isclose( |
| 1518 | + synapseutils.sync._convert_cell_in_manifest_to_python_types(float_string), |
| 1519 | + 123.456, |
| 1520 | + ) |
| 1521 | + assert math.isclose( |
| 1522 | + synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1523 | + float_string_in_brackets |
| 1524 | + ), |
| 1525 | + 123.456, |
| 1526 | + ) |
| 1527 | + |
| 1528 | + def test_float_multiple_items(self) -> None: |
| 1529 | + # GIVEN a multiple item float string |
| 1530 | + float_string = " [123.456, 789.012]" |
| 1531 | + |
| 1532 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1533 | + # THEN I expect the output to be a list of float objects |
| 1534 | + result = synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1535 | + float_string |
| 1536 | + ) |
| 1537 | + assert math.isclose(result[0], 123.456) |
| 1538 | + assert math.isclose(result[1], 789.012) |
| 1539 | + |
| 1540 | + def test_string_single_item(self) -> None: |
| 1541 | + # GIVEN a single item string |
| 1542 | + string = " foo" |
| 1543 | + string_in_brackets = " [foo]" |
| 1544 | + |
| 1545 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1546 | + # THEN I expect the output to be a string object |
| 1547 | + assert ( |
| 1548 | + synapseutils.sync._convert_cell_in_manifest_to_python_types(string) == "foo" |
| 1549 | + ) |
| 1550 | + assert ( |
| 1551 | + synapseutils.sync._convert_cell_in_manifest_to_python_types( |
| 1552 | + string_in_brackets |
| 1553 | + ) |
| 1554 | + == "foo" |
| 1555 | + ) |
| 1556 | + |
| 1557 | + def test_string_multiple_items(self) -> None: |
| 1558 | + # GIVEN a multiple item string |
| 1559 | + string = " [foo, bar]" |
| 1560 | + |
| 1561 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1562 | + # THEN I expect the output to be a list of string objects |
| 1563 | + assert synapseutils.sync._convert_cell_in_manifest_to_python_types(string) == [ |
| 1564 | + "foo", |
| 1565 | + "bar", |
| 1566 | + ] |
| 1567 | + |
| 1568 | + def test_string_single_item_with_comma(self) -> None: |
| 1569 | + # GIVEN a single item string |
| 1570 | + string = "my, sentence, with, commas" |
| 1571 | + |
| 1572 | + # WHEN _convert_cell_in_manifest_to_python_types is called |
| 1573 | + # THEN I expect the output to be a string object |
| 1574 | + assert ( |
| 1575 | + synapseutils.sync._convert_cell_in_manifest_to_python_types(string) |
| 1576 | + == string |
| 1577 | + ) |
| 1578 | + |
| 1579 | + |
| 1580 | +class TestSplitString(object): |
| 1581 | + """ |
| 1582 | + Test the _split_string function. Note as a pre-check to calling this function |
| 1583 | + the string would have started with brackets `[]`, but they were removed before |
| 1584 | + calling this function. |
| 1585 | + """ |
| 1586 | + |
| 1587 | + def test_single_item(self) -> None: |
| 1588 | + # GIVEN single item strings |
| 1589 | + string_with_no_quotes = "foo" |
| 1590 | + string_with_quotes = '"foo"' |
| 1591 | + string_with_quotes_inside_string = 'foo "bar" baz' |
| 1592 | + string_with_commas_inside_string = '"foo, bar, baz"' |
| 1593 | + |
| 1594 | + # WHEN split_strings is called |
| 1595 | + |
| 1596 | + # THEN I expect the output to treat all values as a single item |
| 1597 | + assert synapseutils.sync._split_string(string_with_no_quotes) == [ |
| 1598 | + string_with_no_quotes |
| 1599 | + ] |
| 1600 | + assert synapseutils.sync._split_string(string_with_quotes) == [ |
| 1601 | + string_with_quotes |
| 1602 | + ] |
| 1603 | + assert synapseutils.sync._split_string(string_with_quotes_inside_string) == [ |
| 1604 | + string_with_quotes_inside_string |
| 1605 | + ] |
| 1606 | + assert synapseutils.sync._split_string(string_with_commas_inside_string) == [ |
| 1607 | + string_with_commas_inside_string |
| 1608 | + ] |
| 1609 | + |
| 1610 | + def test_multiple_item(self) -> None: |
| 1611 | + # GIVEN multiple item strings |
| 1612 | + string_with_no_quotes = "foo, bar, baz" |
| 1613 | + string_with_quotes = '"foo", "bar", "baz"' |
| 1614 | + string_with_commas_in_some_items = '"foo, bar", baz, "foo, bar, baz"' |
| 1615 | + |
| 1616 | + # WHEN split_strings is called |
| 1617 | + # THEN I expect the output to split the string into multiple items |
| 1618 | + assert synapseutils.sync._split_string(string_with_no_quotes) == [ |
| 1619 | + "foo", |
| 1620 | + "bar", |
| 1621 | + "baz", |
| 1622 | + ] |
| 1623 | + assert synapseutils.sync._split_string(string_with_quotes) == [ |
| 1624 | + '"foo"', |
| 1625 | + '"bar"', |
| 1626 | + '"baz"', |
| 1627 | + ] |
| 1628 | + assert synapseutils.sync._split_string(string_with_commas_in_some_items) == [ |
| 1629 | + '"foo, bar"', |
| 1630 | + "baz", |
| 1631 | + '"foo, bar, baz"', |
| 1632 | + ] |
0 commit comments