Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Adds tests for node, observation, resolve and sparql models.
  • Loading branch information
jm-rivera committed Dec 4, 2024
1 parent b339e8a commit 58b9e57
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 0 deletions.
89 changes: 89 additions & 0 deletions datacommons_client/tests/models/test_node_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from datacommons_client.models.node import Arcs, Node, NodeGroup, Properties


def test_node_from_json():
"""Test the Node.from_json method."""
json_data = {
"dcid": "node123",
"name": "Test Node",
"provenanceId": "prov123",
"types": ["TypeA", "TypeB"],
"value": "42",
}
node = Node.from_json(json_data)
assert node.dcid == "node123"
assert node.name == "Test Node"
assert node.provenanceId == "prov123"
assert node.types == ["TypeA", "TypeB"]
assert node.value == "42"


def test_node_from_json_partial():
"""Test Node.from_json with partial data."""
json_data = {
"dcid": "node123",
}
node = Node.from_json(json_data)
assert node.dcid == "node123"
assert node.name is None
assert node.provenanceId is None
assert node.types is None
assert node.value is None


def test_nodegroup_from_json():
"""Test the NodeGroup.from_json method."""
json_data = {
"nodes": [
{"dcid": "node1", "name": "Node 1"},
{"dcid": "node2", "name": "Node 2"},
]
}
node_group = NodeGroup.from_json(json_data)
assert len(node_group.nodes) == 2
assert node_group.nodes[0].dcid == "node1"
assert node_group.nodes[1].name == "Node 2"


def test_nodegroup_from_json_empty():
"""Test NodeGroup.from_json with empty data."""
json_data = {}
node_group = NodeGroup.from_json(json_data)
assert len(node_group.nodes) == 0


def test_arcs_from_json():
"""Test the Arcs.from_json method."""
json_data = {
"label1": {"nodes": [{"dcid": "node1"}, {"dcid": "node2"}]},
"label2": {"nodes": [{"dcid": "node3"}]},
}
arcs = Arcs.from_json(json_data)
assert len(arcs.arcs) == 2
assert "label1" in arcs.arcs
assert len(arcs.arcs["label1"].nodes) == 2
assert arcs.arcs["label1"].nodes[0].dcid == "node1"
assert len(arcs.arcs["label2"].nodes) == 1
assert arcs.arcs["label2"].nodes[0].dcid == "node3"


def test_arcs_from_json_empty():
"""Test Arcs.from_json with empty data."""
json_data = {}
arcs = Arcs.from_json(json_data)
assert len(arcs.arcs) == 0


def test_properties_from_json():
"""Test the Properties.from_json method."""
json_data = {"properties": ["prop1", "prop2", "prop3"]}
properties = Properties.from_json(json_data)
assert len(properties.properties) == 3
assert properties.properties == ["prop1", "prop2", "prop3"]


def test_properties_from_json_empty():
"""Test Properties.from_json with empty data."""
json_data = {}
properties = Properties.from_json(json_data)
assert len(properties.properties) == 0
119 changes: 119 additions & 0 deletions datacommons_client/tests/models/test_observation_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from datacommons_client.models.observation import (
Facet,
Observation,
OrderedFacets,
Variable,
)


def test_observation_from_json():
"""Test the Observation.from_json method."""
json_data = {"date": "2024-01-01", "value": 123.45}
observation = Observation.from_json(json_data)
assert observation.date == "2024-01-01"
assert observation.value == 123.45
assert isinstance(observation.value, float)


def test_observation_from_json_partial():
"""Test Observation.from_json with missing data."""
json_data = {"date": "2024-01-01"}
observation = Observation.from_json(json_data)
assert observation.date == "2024-01-01"
assert observation.value is None


def test_ordered_facets_from_json():
"""Test the OrderedFacets.from_json method."""
json_data = {
"earliestDate": "2023-01-01",
"facetId": "facet123",
"latestDate": "2024-01-01",
"obsCount": 2,
"observations": [
{"date": "2023-01-01", "value": 100.0},
{"date": "2024-01-01", "value": 200.0},
],
}
ordered_facets = OrderedFacets.from_json(json_data)
assert ordered_facets.earliestDate == "2023-01-01"
assert ordered_facets.facetId == "facet123"
assert ordered_facets.latestDate == "2024-01-01"
assert ordered_facets.obsCount == 2
assert len(ordered_facets.observations) == 2
assert ordered_facets.observations[0].value == 100.0


def test_ordered_facets_from_json_empty_observations():
"""Test OrderedFacets.from_json with empty observations."""
json_data = {
"earliestDate": "2023-01-01",
"facetId": "facet123",
"latestDate": "2024-01-01",
"obsCount": 0,
"observations": [],
}
ordered_facets = OrderedFacets.from_json(json_data)
assert len(ordered_facets.observations) == 0


def test_variable_from_json():
"""Test the Variable.from_json method."""
json_data = {
"byEntity": {
"entity1": {
"orderedFacets": [
{
"earliestDate": "2023-01-01",
"facetId": "facet1",
"latestDate": "2023-12-31",
"obsCount": 2,
"observations": [
{"date": "2023-01-01", "value": 50.0},
{"date": "2023-12-31", "value": 75.0},
],
}
]
}
}
}
variable = Variable.from_json(json_data)
assert "entity1" in variable.byEntity
facets = variable.byEntity["entity1"]["orderedFacets"]
assert len(facets) == 1
assert facets[0].facetId == "facet1"
assert facets[0].observations[0].value == 50.0


def test_variable_from_json_empty():
"""Test Variable.from_json with empty byEntity."""
json_data = {"byEntity": {}}
variable = Variable.from_json(json_data)
assert len(variable.byEntity) == 0


def test_facet_from_json():
"""Test the Facet.from_json method."""
json_data = {
"importName": "Import 1",
"measurementMethod": "Method A",
"observationPeriod": "2023",
"provenanceUrl": "http://example.com",
"unit": "usd",
}
facet = Facet.from_json(json_data)
assert facet.importName == "Import 1"
assert facet.measurementMethod == "Method A"
assert facet.observationPeriod == "2023"
assert facet.provenanceUrl == "http://example.com"
assert facet.unit == "usd"


def test_facet_from_json_partial():
"""Test Facet.from_json with missing data."""
json_data = {"importName": "Import 1", "unit": "GTQ"}
facet = Facet.from_json(json_data)
assert facet.importName == "Import 1"
assert facet.measurementMethod is None
assert facet.unit == "GTQ"
assert facet.provenanceUrl is None
61 changes: 61 additions & 0 deletions datacommons_client/tests/models/test_resolve_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from datacommons_client.models.resolve import Candidate, Entity


def test_candidate_from_json():
"""Test the Candidate.from_json method with full data."""
json_data = {"dcid": "dcid123", "dominantType": "Place"}
candidate = Candidate.from_json(json_data)
assert candidate.dcid == "dcid123"
assert candidate.dominantType == "Place"


def test_candidate_from_json_partial():
"""Test Candidate.from_json with missing optional dominantType."""
json_data = {"dcid": "dcid456"}
candidate = Candidate.from_json(json_data)
assert candidate.dcid == "dcid456"
assert candidate.dominantType is None


def test_entity_from_json():
"""Test the Entity.from_json method with multiple candidates."""
json_data = {
"node": "test_query",
"candidates": [
{"dcid": "dcid123", "dominantType": "Place"},
{"dcid": "dcid456", "dominantType": "Event"},
],
}
entity = Entity.from_json(json_data)
assert entity.node == "test_query"
assert len(entity.candidates) == 2
assert entity.candidates[0].dcid == "dcid123"
assert entity.candidates[0].dominantType == "Place"
assert entity.candidates[1].dcid == "dcid456"
assert entity.candidates[1].dominantType == "Event"


def test_entity_from_json_empty_candidates():
"""Test Entity.from_json with no candidates."""
json_data = {"node": "test_query", "candidates": []}
entity = Entity.from_json(json_data)
assert entity.node == "test_query"
assert len(entity.candidates) == 0


def test_entity_from_json_missing_node():
"""Test Entity.from_json with missing node."""
json_data = {"candidates": [{"dcid": "dcid123", "dominantType": "Place"}]}
entity = Entity.from_json(json_data)
assert entity.node is None
assert len(entity.candidates) == 1
assert entity.candidates[0].dcid == "dcid123"
assert entity.candidates[0].dominantType == "Place"


def test_entity_from_json_empty():
"""Test Entity.from_json with empty data."""
json_data = {}
entity = Entity.from_json(json_data)
assert entity.node is None
assert len(entity.candidates) == 0
66 changes: 66 additions & 0 deletions datacommons_client/tests/models/test_sparql_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from datacommons_client.models.sparql import Cell, Row


def test_cell_from_json():
"""Test the Cell.from_json method with valid data."""
json_data = {"value": "Test Value"}
cell = Cell.from_json(json_data)
assert cell.value == "Test Value"


def test_cell_int_from_json():
""" "Test the Cell.from_json method with valid data (int)."""
json_data = {"value": 5}
cell = Cell.from_json(json_data)
assert cell.value == 5


def test_cell_from_json_missing_value():
"""Test Cell.from_json with missing value field."""
json_data = {}
cell = Cell.from_json(json_data)
assert cell.value is None


def test_row_from_json():
"""Test the Row.from_json method with multiple cells."""
json_data = {
"cells": [
{"value": "Cell 1"},
{"value": "Cell 2"},
{"value": "Cell 3"},
]
}
row = Row.from_json(json_data)
assert len(row.cells) == 3
assert row.cells[0].value == "Cell 1"
assert row.cells[1].value == "Cell 2"
assert row.cells[2].value == "Cell 3"


def test_row_from_json_empty_cells():
"""Test Row.from_json with an empty list of cells."""
json_data = {"cells": []}
row = Row.from_json(json_data)
assert len(row.cells) == 0


def test_row_from_json_missing_cells():
"""Test Row.from_json with missing cells field."""
json_data = {}
row = Row.from_json(json_data)
assert len(row.cells) == 0


def test_row_with_nested_empty_cells():
"""Test Row.from_json with cells containing empty cell data."""
json_data = {
"cells": [
{},
{"value": "Cell 2"},
]
}
row = Row.from_json(json_data)
assert len(row.cells) == 2
assert row.cells[0].value is None
assert row.cells[1].value == "Cell 2"

0 comments on commit 58b9e57

Please sign in to comment.