Test a Connector - Fluid Topics - 3.7 - Technical Notes

Develop Connectors with the Fluid Topics API

Product
Fluid Topics
FT_Version
3.7
Category
Technical Notes
language
English
audience
public

Before packaging a connector, it is highly recommended to test it. The Fluid Topics Python API provides the FakeClient object for testing purposes.

The following lines show a basic connector code and an example of a test file for it.

hw.py (connector code):

from fluidtopics.connector import StructuredDocument, Topic, Metadata, Client


class HelloWorldConnector:
def __init__(self, client: Client):
self.client = client

def run(self):
hello = StructuredDocument.create(
document_id="hello",
title="Hello",
toc=[Topic.create(topic_id="hello-topic", title="Hello topic")],
metadata=[Metadata("tutorial", "HelloWorld")]
)

world = StructuredDocument.create(
document_id="world",
title="World",
toc=[]
)

self.client.publish(hello, world)

test_helloworld.py (testing the hw.py connector):

from fluidtopics.connector import StructuredDocument, Metadata, FakeClient
from hw import HelloWorldConnector


def test_hello_title():
client = FakeClient()
connector = HelloWorldConnector(client)

connector.run()

hello = client.publication("hello") # assert that document is published
assert hello.title == "Hello"

def test_hello_metadata():
client = FakeClient()
connector = HelloWorldConnector(client)

connector.run()

hello = client.publication("hello")
# should fail
assert Metadata("invalid", "metadata") in hello.metadata.values()

def test_hello_toc_length():
client = FakeClient()
connector = HelloWorldConnector(client)

connector.run()

hello = client.publication("hello")
assert isinstance(hello, StructuredDocument)
assert len(hello.toc) == 1