Skip to content

Commit

Permalink
Setup automated tests for pull requests (#29)
Browse files Browse the repository at this point in the history
Resolves #28
  • Loading branch information
karelklima authored Oct 3, 2022
1 parent f6d3830 commit 8a3b5a7
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 69 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/deno-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This workflow will run tests using Deno

name: Test using Deno

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
fmt-lint-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: deno task fmt:check
- run: deno task lint
- run: deno task test
3 changes: 3 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"importMap": ".vscode/import_map.json",
"tasks": {
"fmt:fix": "deno fmt library/ specs/ www/ docs/",
"fmt:check": "deno fmt --check library/ specs/ www/ docs/",
"lint": "deno lint library/ specs/ www/ docs/",
"test": "deno test --allow-env --allow-net ./specs",
"dnt": "deno run -A --importmap ./scripts/dnt_import_map.json ./scripts/dnt.ts"
},
Expand Down
11 changes: 6 additions & 5 deletions library/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Encoder {
});
}

encodeNodeProperty(value: any, property: Property, nodeId: NodeId) {
encodeNodeProperty(value: unknown, property: Property, nodeId: NodeId) {
if (value === undefined) {
return;
}
Expand All @@ -107,10 +107,11 @@ class Encoder {
}

if (property["@multilang"]) {
Object.keys(value).forEach((language) => {
const languageValue: string[] = Array.isArray(value[language])
? value[language]
: [value[language]];
const multiValue = value as unknown as Record<string, unknown>;
Object.keys(multiValue).forEach((language) => {
const languageValue: string[] = Array.isArray(multiValue[language])
? multiValue[language] as string[]
: [multiValue[language]] as string[];
languageValue.forEach((singleValue) => {
this.push(
nodeId,
Expand Down
2 changes: 1 addition & 1 deletion library/namespaces/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const createNamespace = <
//acc[term] = `${namespaceSpec.prefix}${term}`
acc[term] = `${namespaceSpec.iri}${term}`;
return acc;
}, {} as any),
}, {} as Record<string, string>),
{
$prefix: namespaceSpec["prefix"],
$iri: namespaceSpec["iri"],
Expand Down
6 changes: 3 additions & 3 deletions library/resource/query_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class QueryHelper {
const value = entity[key];

if (key === "$id") {
output.$id = value;
output.$id = value as string;
return output;
}
if (key === "$type") {
Expand All @@ -90,10 +90,10 @@ export class QueryHelper {
}

if (property["@array"]) {
if (value.length === 0) {
if ((value as Entity[]).length === 0) {
output[key] = null;
} else {
output[key] = value.map((subEntity: Entity) =>
output[key] = (value as Entity[]).map((subEntity) =>
this.replaceVariables(subEntity, property["@context"]!)
);
}
Expand Down
8 changes: 5 additions & 3 deletions library/resource/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { SchemaInterfaceIdentity } from "../schema/mod.ts";

export type Entity<T extends any = Record<string, any>> =
export type Entity<
T extends unknown = Record<string, unknown>,
> =
& DeepPartial<T>
& SchemaInterfaceIdentity;

export type DeepPartial<T> = T extends Function ? T
: T extends object ? { [P in keyof T]?: DeepPartial<T[P]> }
export type DeepPartial<T> = T extends Record<string, unknown>
? { [P in keyof T]?: DeepPartial<T[P]> }
: T;
58 changes: 27 additions & 31 deletions library/schema/interface.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
import type { SupportedDataTypes } from "./data_types.ts";
import type { SchemaPrototype, PropertyPrototype } from "./schema.ts";
import type { PropertyPrototype, SchemaPrototype } from "./schema.ts";

type IsOptional<Property extends PropertyPrototype> = Property extends {
"@optional": true;
}
? true
} ? true
: false;

type IsArray<Property extends PropertyPrototype> = Property extends {
"@array": true;
}
? true
} ? true
: false;

type IsMultilang<Property extends PropertyPrototype> = Property extends {
"@multilang": true;
}
? true
} ? true
: false;

type ValidPropertyDefinition = PropertyPrototype | string;

type ConvertPropertyType<T extends PropertyPrototype> = T extends {
"@context": SchemaPrototype;
}
? // embedded schema
SchemaInterface<T["@context"]>
: // type specified
T extends { "@type": any }
? T["@type"] extends keyof SupportedDataTypes
? // type is built-int
SupportedDataTypes[T["@type"]]
: // type is invalid
never
: // no type -> defaults to string
string;
// embedded schema
? SchemaInterface<T["@context"]>
// type specified
: T extends { "@type": unknown } ? T["@type"] extends keyof SupportedDataTypes
// type is built-int
? SupportedDataTypes[T["@type"]]
// type is invalid
: never
// no type -> defaults to string
: string;

type ConvertPropertyOptional<T extends PropertyPrototype> =
IsOptional<T> extends true
? ConvertPropertyType<T> | undefined
IsOptional<T> extends true ? ConvertPropertyType<T> | undefined
: ConvertPropertyType<T>;

type ConvertPropertyArray<T extends PropertyPrototype> = IsArray<T> extends true
Expand All @@ -47,16 +42,15 @@ type ConvertPropertyArray<T extends PropertyPrototype> = IsArray<T> extends true

type ConvertPropertyMultilang<T extends PropertyPrototype> =
IsMultilang<T> extends true
? IsArray<T> extends true
? Record<string, ConvertPropertyType<T>[]>
: Record<string, ConvertPropertyType<T>>
? IsArray<T> extends true ? Record<string, ConvertPropertyType<T>[]>
: Record<string, ConvertPropertyType<T>>
: ConvertPropertyArray<T>;

type ConvertPropertyObject<T extends PropertyPrototype> =
ConvertPropertyMultilang<T>;

type ConvertProperty<T extends ValidPropertyDefinition> =
T extends PropertyPrototype ? ConvertPropertyObject<T> : string;
type ConvertProperty<T extends ValidPropertyDefinition> = T extends
PropertyPrototype ? ConvertPropertyObject<T> : string;

export type SchemaInterfaceIdentity = {
$id: string;
Expand All @@ -66,9 +60,11 @@ export type SchemaInterfaceType = {
$type: string[];
};

export type SchemaInterface<T extends SchemaPrototype> = {
[X in Exclude<keyof T, "@type">]: T[X] extends ValidPropertyDefinition
? ConvertProperty<T[X]>
: never;
} & SchemaInterfaceIdentity &
SchemaInterfaceType;
export type SchemaInterface<T extends SchemaPrototype> =
& {
[X in Exclude<keyof T, "@type">]: T[X] extends ValidPropertyDefinition
? ConvertProperty<T[X]>
: never;
}
& SchemaInterfaceIdentity
& SchemaInterfaceType;
4 changes: 2 additions & 2 deletions library/schema/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export type {
} from "./interface.ts";

export type {
Schema,
SchemaPrototype,
Property,
PropertyPrototype,
Schema,
SchemaPrototype,
} from "./schema.ts";

export { expandSchema, getSchemaProperties } from "./utils.ts";
8 changes: 4 additions & 4 deletions library/schema/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {
};

const expandSchemaProperty = (
stringOrProperty: string | PropertyPrototype
stringOrProperty: string | PropertyPrototype,
) => {
if (typeof stringOrProperty === "string") {
return {
Expand All @@ -37,15 +37,15 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {
"@multilang",
] as const;

const baseProperty: Record<string, any> = {
const baseProperty: Record<string, unknown> = {
"@id": "",
};

const expandedProperty = Object.keys(property).reduce((acc, key) => {
if (key === "@context") {
acc[key] = expandSchema(property[key]!);
} else if (validKeys.includes(key as keyof PropertyPrototype)) {
acc[key] = property[key as keyof PropertyPrototype] as any;
acc[key] = property[key as keyof PropertyPrototype] as unknown;
}
return acc;
}, baseProperty);
Expand All @@ -66,7 +66,7 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {
acc[key] = expandArray(schemaPrototype[key]);
} else {
acc[key] = expandSchemaProperty(
schemaPrototype[key] as string | PropertyPrototype
schemaPrototype[key] as string | PropertyPrototype,
);
}
return acc;
Expand Down
19 changes: 10 additions & 9 deletions www/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,20 @@ type MenuItemProps = {
const baseLinkClass = "flex flex-row p-4 border-b-2 hover:border-black";

function MenuItem({ title, url, activeLink }: MenuItemProps) {
const linkClass =
url === activeLink
? `${baseLinkClass} border-black bg-gray-50`
: `${baseLinkClass} border-transparent`;
const linkClass = url === activeLink
? `${baseLinkClass} border-black bg-gray-50`
: `${baseLinkClass} border-transparent`;
return (
<li class="inline-block">
<a href={url} class={linkClass}>
<span>{title}</span>
{url.startsWith("http") ? (
<span class="ml-1 mt-1 w-2 h-2 text-gray-400">
<IconExternalLink />
</span>
) : null}
{url.startsWith("http")
? (
<span class="ml-1 mt-1 w-2 h-2 text-gray-400">
<IconExternalLink />
</span>
)
: null}
</a>
</li>
);
Expand Down
3 changes: 2 additions & 1 deletion www/components/Icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export function IconGitHub() {
stroke-linejoin="round"
class="feather feather-github"
>
<path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"></path>
<path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22">
</path>
</svg>
);
}
Expand Down
22 changes: 12 additions & 10 deletions www/components/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ type TitleProps = {
};

export function Title({ children }: TitleProps) {
return children ? (
<title>
{children}
{" · LDkit"}
</title>
) : (
<title>
{"LDkit · Linked Data query toolkit for TypeScript developers"}
</title>
);
return children
? (
<title>
{children}
{" · LDkit"}
</title>
)
: (
<title>
{"LDkit · Linked Data query toolkit for TypeScript developers"}
</title>
);
}

0 comments on commit 8a3b5a7

Please sign in to comment.