Skip to content

Commit

Permalink
WIP on #773.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed May 7, 2024
1 parent 609e6b3 commit fe70000
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 22 deletions.
169 changes: 150 additions & 19 deletions tests/field_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import sys
import os
import io
import unittest
import collections

Expand Down Expand Up @@ -34,7 +33,11 @@ def test_create_with_simple_field(self):

# Create a node with a simple field of cardinality 1, no subdelimiters.
self.field_definitions = {
"field_foo": {"cardinality": 1, "formatted_text": False}
"field_foo": {
"cardinality": 1,
"formatted_text": False,
"field_type": "text",
}
}

field = workbench_fields.SimpleField()
Expand Down Expand Up @@ -79,7 +82,11 @@ def test_create_with_simple_field(self):

# Create a node with a simple field of cardinality unlimited, no subdelimiters.
self.field_definitions = {
"field_foo": {"cardinality": -1, "formatted_text": False}
"field_foo": {
"cardinality": -1,
"formatted_text": False,
"field_type": "text",
}
}

field = workbench_fields.SimpleField()
Expand Down Expand Up @@ -115,7 +122,11 @@ def test_create_with_simple_field(self):

# Create a node with a simple field of cardinality limited, no subdelimiters.
self.field_definitions = {
"field_foo": {"cardinality": 2, "formatted_text": False}
"field_foo": {
"cardinality": 2,
"formatted_text": False,
"field_type": "text",
}
}

field = workbench_fields.SimpleField()
Expand Down Expand Up @@ -164,6 +175,72 @@ def test_create_with_simple_field(self):
r"simple_006 would exceed maximum number of allowed values \(2\)",
)

def test_create_with_simple_integer_field(self):
existing_node = {
"type": [{"target_id": "islandora_object", "target_type": "node_type"}],
"title": [{"value": "Test node"}],
"status": [{"value": 1}],
}

# Create a node with a simple integer field of cardinality 1, no subdelimiters.
self.field_definitions = {
"field_int": {
"cardinality": 1,
"formatted_text": False,
"field_type": "integer",
}
}

field = workbench_fields.SimpleField()
csv_record = collections.OrderedDict()
csv_record["id"] = "simple_001_int"
csv_record["field_int"] = "5"
node = field.create(
self.config, self.field_definitions, existing_node, csv_record, "field_int"
)
expected_node = {
"type": [{"target_id": "islandora_object", "target_type": "node_type"}],
"title": [{"value": "Test node"}],
"status": [{"value": 1}],
"field_int": [{"value": 5}],
}
self.assertDictEqual(node, expected_node)

def test_create_with_simple_float_field(self):
existing_node = {
"type": [{"target_id": "islandora_object", "target_type": "node_type"}],
"title": [{"value": "Test node"}],
"status": [{"value": 1}],
}

# Create a node with a simple integer field of cardinality 1, no subdelimiters.
self.field_definitions = {
"field_float": {
"cardinality": 1,
"formatted_text": False,
"field_type": "float",
}
}

field = workbench_fields.SimpleField()
csv_record = collections.OrderedDict()
csv_record["id"] = "simple_001_int"
csv_record["field_float"] = "6.0"
node = field.create(
self.config,
self.field_definitions,
existing_node,
csv_record,
"field_float",
)
expected_node = {
"type": [{"target_id": "islandora_object", "target_type": "node_type"}],
"title": [{"value": "Test node"}],
"status": [{"value": 1}],
"field_float": [{"value": 6.0}],
}
self.assertDictEqual(node, expected_node)

def test_simple_field_title_update_replace(self):
# Update the node title, first with an 'update_mode' of replace.
existing_node = {
Expand All @@ -172,7 +249,9 @@ def test_simple_field_title_update_replace(self):
"status": [{"value": 1}],
}

self.field_definitions = {"title": {"cardinality": 1, "formatted_text": False}}
self.field_definitions = {
"title": {"cardinality": 1, "formatted_text": False, "field_type": "text"}
}

self.config["task"] = "update"
self.config["update_mode"] = "replace"
Expand Down Expand Up @@ -204,7 +283,9 @@ def test_simple_field_title_update_append(self):
"status": [{"value": 1}],
}

self.field_definitions = {"title": {"cardinality": 1, "formatted_text": False}}
self.field_definitions = {
"title": {"cardinality": 1, "formatted_text": False, "field_type": "text"}
}

self.config["task"] = "update"
self.config["update_mode"] = "append"
Expand Down Expand Up @@ -242,7 +323,11 @@ def test_simple_field_update_replace_cardinality_1_no_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": 1, "formatted_text": False}
"field_foo": {
"cardinality": 1,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -277,7 +362,11 @@ def test_simple_field_update_append_cardinality_1_no_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": 1, "formatted_text": False}
"field_foo": {
"cardinality": 1,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -317,7 +406,11 @@ def test_simple_field_update_replace_cardinality_1_with_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": 1, "formatted_text": False}
"field_foo": {
"cardinality": 1,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -358,7 +451,11 @@ def test_simple_field_update_replace_cardinality_unlimited_no_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": -1, "formatted_text": False}
"field_foo": {
"cardinality": -1,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -394,7 +491,11 @@ def test_simple_field_update_replace_cardinality_unlimited_with_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": -1, "formatted_text": False}
"field_foo": {
"cardinality": -1,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -422,7 +523,11 @@ def test_simple_field_update_replace_cardinality_unlimited_with_subdelims(self):

def test_simple_field_update_append_cardinality_unlimited_no_subdelims(self):
self.field_definitions = {
"field_foo": {"cardinality": -1, "formatted_text": False}
"field_foo": {
"cardinality": -1,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -500,7 +605,11 @@ def test_simple_field_update_append_cardinality_unlimited_with_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": -1, "formatted_text": False}
"field_foo": {
"cardinality": -1,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -539,7 +648,11 @@ def test_simple_field_update_replace_cardinality_limited_no_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": 2, "formatted_text": False}
"field_foo": {
"cardinality": 2,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -577,7 +690,11 @@ def test_simple_field_update_append_cardinality_limited_no_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": 2, "formatted_text": False}
"field_foo": {
"cardinality": 2,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -623,7 +740,11 @@ def test_simple_field_update_replace_cardinality_limited_with_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": 2, "formatted_text": False}
"field_foo": {
"cardinality": 2,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -668,7 +789,11 @@ def test_simple_field_update_append_cardinality_limited_with_subdelims(self):
}

self.field_definitions = {
"field_foo": {"cardinality": 3, "formatted_text": False}
"field_foo": {
"cardinality": 3,
"formatted_text": False,
"field_type": "text",
}
}

self.config["task"] = "update"
Expand Down Expand Up @@ -717,7 +842,11 @@ def test_simple_field_update_delete(self):
}

self.field_definitions = {
"field_foo": {"cardinality": 3, "formatted_text": False}
"field_foo": {
"cardinality": 3,
"formatted_text": False,
"field_type": "text",
}
}

self.config["update_mode"] = "delete"
Expand Down Expand Up @@ -941,7 +1070,9 @@ def test_simple_field_title_update_replace(self):
"status": [{"value": 1}],
}

self.field_definitions = {"title": {"cardinality": 1, "formatted_text": False}}
self.field_definitions = {
"title": {"cardinality": 1, "formatted_text": False, "field_type": "text"}
}

self.config["task"] = "update"
self.config["update_mode"] = "replace"
Expand Down
5 changes: 2 additions & 3 deletions workbench_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""

import json
import copy
from workbench_utils import *


Expand Down Expand Up @@ -200,9 +199,9 @@ def update(
):
field_values.append({"value": subvalue, "format": text_format})
else:
if field_definitions[field_name]["type"] == "integer":
if field_definitions[field_name]["field_type"] == "integer":
subvalue = int(subvalue)
if field_definitions[field_name]["type"] == "float":
if field_definitions[field_name]["field_type"] == "float":
subvalue = float(subvalue)
entity[field_name].append({"value": subvalue})
field_values.append({"value": subvalue})
Expand Down

0 comments on commit fe70000

Please sign in to comment.