diff --git a/api/hff_entry_forms_api.nimble b/api/hff_entry_forms_api.nimble index 5fbe0e0..5b65a22 100644 --- a/api/hff_entry_forms_api.nimble +++ b/api/hff_entry_forms_api.nimble @@ -18,4 +18,7 @@ requires "https://git.jdb-software.com/jdb/nim-time-utils.git >= 0.5.0" requires "https://git.jdb-software.com/jdb/update-nim-package-version" task updateVersion, "Update the version of this package.": - exec "update_nim_package_version hff_entry_forms_api 'src/hff_entry_forms_apipkg/version.nim'" \ No newline at end of file + exec "update_nim_package_version hff_entry_forms_api 'src/hff_entry_forms_apipkg/version.nim'" + +task unittest, "Runs the unit test suite.": + exec "nim c -r test/runner" diff --git a/api/src/hff_entry_forms_api.nim b/api/src/hff_entry_forms_api.nim index a02de3b..4164233 100644 --- a/api/src/hff_entry_forms_api.nim +++ b/api/src/hff_entry_forms_api.nim @@ -42,7 +42,7 @@ Usage: Options: - -C, --config Location of the config file to use (defaults to + -c, --config Location of the config file to use (defaults to hff_entry_forms_api.config.json) -d, --debug Log debugging information. @@ -52,7 +52,8 @@ Options: let consoleLogger = newConsoleLogger( levelThreshold=lvlInfo, - fmtStr="$app - $levelname: ") + fmtStr="$appname - $levelname: ", + useStderr=true) logging.addHandler(consoleLogger) # Initialize our service context diff --git a/api/src/hff_entry_forms_apipkg/models.nim b/api/src/hff_entry_forms_apipkg/models.nim index 4b7ce5a..e1ba6b5 100644 --- a/api/src/hff_entry_forms_apipkg/models.nim +++ b/api/src/hff_entry_forms_apipkg/models.nim @@ -1,4 +1,7 @@ -import json, times, timeutils +import std/json, std/times +import timeutils + +import ../../../../notion_utils/src/notion_utils type EventProposal* = object @@ -26,14 +29,6 @@ proc getOrFail(n: JsonNode, key: string): JsonNode = proc parseIso8601(n: JsonNode, key: string): DateTime = return parseIso8601(n.getOrFail(key).getStr) -proc textProp(value: string): JsonNode = - return %*[ - { - "type": "text", - "text": { "content": value } - } - ] - proc parseEventProposal*(n: JsonNode): EventProposal {.raises: [JsonParsingError].} = try: @@ -52,33 +47,33 @@ proc parseEventProposal*(n: JsonNode): EventProposal {.raises: [JsonParsingError proc asNotionPage*(ep: EventProposal): JsonNode = result = %*{ "properties": { - "Event": { "title": textProp(ep.name) }, + "Event": makeTextProp("title", ep.name), "Date": { "date": { "start": formatIso8601(ep.date) } }, "Department": { "multi_select": [ { "name": ep.department } ] }, - "Location": { "rich_text": textProp(ep.location) }, - "Owner": { "rich_text": textProp(ep.owner) }, + "Location": makeTextProp("rich_text", ep.location), + "Owner": makeTextProp("rich_text", ep.owner), "State": { "select": { "name": "Proposed" } } }, "children": [ { "object": "block", "type": "heading_2", - "heading_2": { "text": textProp("Purpose") } + "heading_2": makeTextProp("text", "Purpose") }, { "object": "block", "type": "paragraph", - "paragraph": { "text": textProp(ep.purpose) } + "paragraph": makeTextProp("text", ep.purpose) }, { "object": "block", "type": "heading_2", - "heading_2": { "text": textProp("Description") } + "heading_2": makeTextProp("text", "Description") }, { "object": "block", "type": "paragraph", - "paragraph": { "text": textProp(ep.description) } + "paragraph": makeTextProp("text", ep.description) } ] } diff --git a/api/src/hff_entry_forms_apipkg/notion_client.nim b/api/src/hff_entry_forms_apipkg/notion_client.nim index 434de2b..8e5ab7e 100644 --- a/api/src/hff_entry_forms_apipkg/notion_client.nim +++ b/api/src/hff_entry_forms_apipkg/notion_client.nim @@ -1,18 +1,12 @@ import json, logging, std/httpclient, sequtils, strutils +import ../../../../notion_utils/src/notion_utils import ./models, ./service -proc makeHttpClient(cfg: HffEntryFormsApiConfig): HttpClient = - let headers = newHttpHeaders([ - ("Content-Type", "application/json"), - ("Authorization", "Bearer " & cfg.integrationToken), - ("Notion-Version", cfg.notionVersion) - ], true) - debug $headers - return newHttpClient(headers = headers, ) - proc getEventProposalConfig*(cfg: HffEntryFormsApiConfig): EventProposalConfig = - let http = makeHttpClient(cfg) + let http = newNotionClient( + apiVersion = cfg.notionVersion, + integrationToken = cfg.integrationToken) let apiResp = http.get(cfg.notionApiBaseUrl & "/databases/" & cfg.eventParentId) debug apiResp.status @@ -37,7 +31,10 @@ proc getEventProposalConfig*(cfg: HffEntryFormsApiConfig): EventProposalConfig = ) proc createProposedEvent*(cfg: HffEntryFormsApiConfig, ep: EventProposal): bool = - let http = makeHttpClient(cfg) + let http = newNotionClient( + apiVersion = cfg.notionVersion, + integrationToken = cfg.integrationToken) + let epNotionPage = ep.asNotionPage epNotionPage["parent"] = %*{ "database_id": cfg.eventParentId } diff --git a/api/test/config.nims b/api/test/config.nims new file mode 100644 index 0000000..cd13e0a --- /dev/null +++ b/api/test/config.nims @@ -0,0 +1,2 @@ +switch("path", "../src") +switch("verbosity", "0") diff --git a/api/test/runner b/api/test/runner new file mode 100755 index 0000000..0dfedcd Binary files /dev/null and b/api/test/runner differ diff --git a/api/test/runner.nim b/api/test/runner.nim new file mode 100644 index 0000000..c46c65c --- /dev/null +++ b/api/test/runner.nim @@ -0,0 +1,3 @@ +import std/unittest + +import ./tmodels diff --git a/api/test/tmodels.nim b/api/test/tmodels.nim new file mode 100644 index 0000000..2c6ed62 --- /dev/null +++ b/api/test/tmodels.nim @@ -0,0 +1,20 @@ +import std/json, std/times, std/unittest + +import hff_entry_forms_apipkg/models + +suite "models": + + test "asNotionPage(EventProposal)": + let ep = EventProposal( + name: "Test Event", + description: "A test event.", + purpose: "Event example for unit testing.", + department: "Testing", + location: "Hope Family Fellowship", + owner: "Jonathan Bernard", + date: parse("2021-10-30", "YYYY-MM-dd"), + budgetInDollars: 56) + + let expectedJson = """{"properties":{"Event":{"title":[{"type":"text","text":{"content":"Test Event"}}]},"Date":{"date":{"start":"2021-10-30T00:00:00.000-05:00"}},"Department":{"multi_select":[{"name":"Testing"}]},"Location":{"rich_text":[{"type":"text","text":{"content":"Hope Family Fellowship"}}]},"Owner":{"rich_text":[{"type":"text","text":{"content":"Jonathan Bernard"}}]},"State":{"select":{"name":"Proposed"}}},"children":[{"object":"block","type":"heading_2","heading_2":{"text":[{"type":"text","text":{"content":"Purpose"}}]}},{"object":"block","type":"paragraph","paragraph":{"text":[{"type":"text","text":{"content":"Event example for unit testing."}}]}},{"object":"block","type":"heading_2","heading_2":{"text":[{"type":"text","text":{"content":"Description"}}]}},{"object":"block","type":"paragraph","paragraph":{"text":[{"type":"text","text":{"content":"A test event."}}]}}]}""" + + check $(ep.asNotionPage) == expectedJson