Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean and unify some test utils #13495

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * as userGroups from "./userGroups"
export { generator } from "./generator"
export * as scim from "./scim"
export * as quotas from "./quotas"
export * as tables from "./tables"
68 changes: 68 additions & 0 deletions packages/backend-core/tests/core/utilities/structures/tables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import _ from "lodash"
import {
Datasource,
FieldType,
INTERNAL_TABLE_SOURCE_ID,
Table,
TableSourceType,
} from "@budibase/types"
import { generator } from "./generator"

export function tableForDatasource(
datasource?: Datasource,
...extra: Partial<Table>[]
): Table {
return _.merge(
{
name: generator.guid(),
type: "table",
sourceType: datasource
? TableSourceType.EXTERNAL
: TableSourceType.INTERNAL,
sourceId: datasource ? datasource._id! : INTERNAL_TABLE_SOURCE_ID,
schema: {},
},
...extra
)
}

export function basicTable(
datasource?: Datasource,
...extra: Partial<Table>[]
): Table {
return tableForDatasource(
datasource,
{
name: "TestTable",
schema: {
name: {
type: FieldType.STRING,
name: "name",
constraints: {
type: "string",
},
},
description: {
type: FieldType.STRING,
name: "description",
constraints: {
type: "string",
},
},
},
},
...extra
)
}

export function externalTable(
datasource: Datasource,
...extra: Partial<Table>[]
) {
const baseTable = basicTable(datasource, ...extra)
return {
...baseTable,
sourceId: datasource._id!,
sourceType: TableSourceType.EXTERNAL,
}
}
4 changes: 4 additions & 0 deletions packages/server/src/integration-test/mysql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
getDatasource,
rawQuery,
} from "../integrations/tests/utils"
import { builderSocket } from "../websockets"

Check failure on line 19 in packages/server/src/integration-test/mysql.spec.ts

View workflow job for this annotation

GitHub Actions / lint

'builderSocket' is defined but never used. Allowed unused vars must match /^_/u
import { generator } from "@budibase/backend-core/tests"
// @ts-ignore
fetch.mockSearch()
Expand Down Expand Up @@ -80,6 +80,10 @@
name: "description",
type: FieldType.STRING,
},
date: {
name: "date",
type: FieldType.DATETIME,
},
value: {
name: "value",
type: FieldType.NUMBER,
Expand Down
8 changes: 5 additions & 3 deletions packages/server/src/integrations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Datasource,
FieldType,
TableSourceType,
FieldSchema,
} from "@budibase/types"
import { DocumentType, SEPARATOR } from "../db/utils"
import { InvalidColumns, DEFAULT_BB_DATASOURCE_ID } from "../constants"
Expand Down Expand Up @@ -239,14 +240,15 @@ export function generateColumnDefinition(config: {
constraints.inclusion = options
}

const schema: any = {
const schema = {
type: foundType,
externalType,
autocolumn,
name,
constraints,
}
if (foundType === FieldType.DATETIME) {
} as FieldSchema

if (schema.type === FieldType.DATETIME) {
schema.dateOnly = SQL_DATE_ONLY_TYPES.includes(lowerCaseType)
schema.timeOnly = SQL_TIME_ONLY_TYPES.includes(lowerCaseType)
}
Expand Down
43 changes: 3 additions & 40 deletions packages/server/src/tests/utilities/structures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,29 @@ import {
AutomationTrigger,
AutomationTriggerStepId,
Datasource,
FieldType,
SourceName,
Table,
INTERNAL_TABLE_SOURCE_ID,
TableSourceType,
Query,
Webhook,
WebhookActionType,
} from "@budibase/types"
import { LoopInput, LoopStepType } from "../../definitions/automations"
import { merge } from "lodash"
import { generator } from "@budibase/backend-core/tests"
import { structures } from "@budibase/backend-core/tests"

const { BUILTIN_ROLE_IDS } = roles

export function tableForDatasource(
datasource?: Datasource,
...extra: Partial<Table>[]
): Table {
return merge(
{
name: generator.guid(),
type: "table",
sourceType: datasource
? TableSourceType.EXTERNAL
: TableSourceType.INTERNAL,
sourceId: datasource ? datasource._id! : INTERNAL_TABLE_SOURCE_ID,
schema: {},
},
...extra
)
return structures.tables.tableForDatasource(datasource, ...extra)
}

export function basicTable(
datasource?: Datasource,
...extra: Partial<Table>[]
): Table {
return tableForDatasource(
datasource,
{
name: "TestTable",
schema: {
name: {
type: FieldType.STRING,
name: "name",
constraints: {
type: "string",
},
},
description: {
type: FieldType.STRING,
name: "description",
constraints: {
type: "string",
},
},
},
},
...extra
)
return structures.tables.basicTable(datasource, ...extra)
}

export function basicView(tableId: string) {
Expand Down
13 changes: 7 additions & 6 deletions packages/types/src/documents/app/table/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface JsonFieldMetadata extends Omit<BaseFieldSchema, "subtype"> {
export interface DateFieldMetadata extends Omit<BaseFieldSchema, "subtype"> {
type: FieldType.DATETIME
ignoreTimezones?: boolean
dateOnly?: boolean
timeOnly?: boolean
subtype?: AutoFieldSubType.CREATED_AT | AutoFieldSubType.UPDATED_AT
}
Expand All @@ -114,6 +115,11 @@ export interface BBReferenceFieldMetadata

export interface AttachmentFieldMetadata extends BaseFieldSchema {
type: FieldType.ATTACHMENTS
constraints: FieldConstraints & { type: "array" }
}

export interface AttachmentSingleFieldMetadata extends BaseFieldSchema {
type: FieldType.ATTACHMENT_SINGLE
}

export interface FieldConstraints {
Expand Down Expand Up @@ -177,6 +183,7 @@ export type FieldSchema =
| BBReferenceFieldMetadata
| JsonFieldMetadata
| AttachmentFieldMetadata
| AttachmentSingleFieldMetadata

export interface TableSchema {
[key: string]: FieldSchema
Expand Down Expand Up @@ -211,9 +218,3 @@ export function isBBReferenceField(
): field is BBReferenceFieldMetadata {
return field.type === FieldType.BB_REFERENCE
}

export function isAttachmentField(
field: FieldSchema
): field is AttachmentFieldMetadata {
return field.type === FieldType.ATTACHMENTS
}
Loading