Skip to content

Commit a8a1b96

Browse files
authored
chore: Increase test coverage (#213)
* Added saveResourceDescriptor spec * Added descriptor plugin spec * Added copyFile spec * Added inferTable spec * Removed unused code * Added full schema test * Added terminal dialect tests * Added savePackageDescriptor spec * Fixed linting * Improved terminal components naming * Added field descriptor spec
1 parent 8e4cd4a commit a8a1b96

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2461
-154
lines changed

dataset/file/copy.spec.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import * as fs from "node:fs/promises"
2+
import * as path from "node:path"
3+
import { temporaryDirectory } from "tempy"
4+
import { afterEach, beforeEach, describe, expect, it } from "vitest"
5+
import { copyFile } from "./copy.ts"
6+
import { writeTempFile } from "./temp.ts"
7+
8+
describe("copyFile", () => {
9+
let testDir: string
10+
11+
beforeEach(() => {
12+
testDir = temporaryDirectory()
13+
})
14+
15+
afterEach(async () => {
16+
try {
17+
await fs.rm(testDir, { recursive: true, force: true })
18+
} catch (error) {
19+
if (error instanceof Error && !error.message.includes("ENOENT")) {
20+
console.error(`Failed to clean up test directory: ${testDir}`, error)
21+
}
22+
}
23+
})
24+
25+
it("should copy file from source to target", async () => {
26+
const sourcePath = await writeTempFile("test content")
27+
const targetPath = path.join(testDir, "target.txt")
28+
29+
await copyFile({ sourcePath, targetPath })
30+
31+
const fileExists = await fs
32+
.stat(targetPath)
33+
.then(() => true)
34+
.catch(() => false)
35+
expect(fileExists).toBe(true)
36+
37+
const content = await fs.readFile(targetPath, "utf-8")
38+
expect(content).toBe("test content")
39+
})
40+
41+
it("should copy file with exact content", async () => {
42+
const content = "Hello, World! This is a test file."
43+
const sourcePath = await writeTempFile(content)
44+
const targetPath = path.join(testDir, "copy.txt")
45+
46+
await copyFile({ sourcePath, targetPath })
47+
48+
const copiedContent = await fs.readFile(targetPath, "utf-8")
49+
expect(copiedContent).toBe(content)
50+
})
51+
52+
it("should copy binary file", async () => {
53+
const binaryData = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10])
54+
const sourcePath = await writeTempFile(binaryData)
55+
const targetPath = path.join(testDir, "binary.bin")
56+
57+
await copyFile({ sourcePath, targetPath })
58+
59+
const copiedData = await fs.readFile(targetPath)
60+
expect(Buffer.compare(copiedData, binaryData)).toBe(0)
61+
})
62+
63+
it("should copy empty file", async () => {
64+
const sourcePath = await writeTempFile("")
65+
const targetPath = path.join(testDir, "empty.txt")
66+
67+
await copyFile({ sourcePath, targetPath })
68+
69+
const content = await fs.readFile(targetPath, "utf-8")
70+
expect(content).toBe("")
71+
})
72+
73+
it("should copy large file", async () => {
74+
const largeContent = "x".repeat(100000)
75+
const sourcePath = await writeTempFile(largeContent)
76+
const targetPath = path.join(testDir, "large.txt")
77+
78+
await copyFile({ sourcePath, targetPath })
79+
80+
const copiedContent = await fs.readFile(targetPath, "utf-8")
81+
expect(copiedContent).toBe(largeContent)
82+
expect(copiedContent.length).toBe(100000)
83+
})
84+
85+
it("should copy file with special characters", async () => {
86+
const content = "Special characters: é, ñ, ü, ö, à, 中文, 日本語"
87+
const sourcePath = await writeTempFile(content)
88+
const targetPath = path.join(testDir, "special.txt")
89+
90+
await copyFile({ sourcePath, targetPath })
91+
92+
const copiedContent = await fs.readFile(targetPath, "utf-8")
93+
expect(copiedContent).toBe(content)
94+
})
95+
96+
it("should copy file to nested directory", async () => {
97+
const sourcePath = await writeTempFile("nested content")
98+
const targetPath = path.join(testDir, "nested", "dir", "file.txt")
99+
100+
await copyFile({ sourcePath, targetPath })
101+
102+
const fileExists = await fs
103+
.stat(targetPath)
104+
.then(() => true)
105+
.catch(() => false)
106+
expect(fileExists).toBe(true)
107+
108+
const content = await fs.readFile(targetPath, "utf-8")
109+
expect(content).toBe("nested content")
110+
})
111+
112+
it("should copy json file", async () => {
113+
const jsonContent = JSON.stringify({ name: "test", value: 123 })
114+
const sourcePath = await writeTempFile(jsonContent)
115+
const targetPath = path.join(testDir, "data.json")
116+
117+
await copyFile({ sourcePath, targetPath })
118+
119+
const copiedContent = await fs.readFile(targetPath, "utf-8")
120+
expect(copiedContent).toBe(jsonContent)
121+
expect(JSON.parse(copiedContent)).toEqual({ name: "test", value: 123 })
122+
})
123+
124+
it("should copy file with newlines", async () => {
125+
const content = "Line 1\nLine 2\nLine 3\n"
126+
const sourcePath = await writeTempFile(content)
127+
const targetPath = path.join(testDir, "multiline.txt")
128+
129+
await copyFile({ sourcePath, targetPath })
130+
131+
const copiedContent = await fs.readFile(targetPath, "utf-8")
132+
expect(copiedContent).toBe(content)
133+
})
134+
})
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
import type { Package } from "@dpkit/metadata"
2+
import * as metadataModule from "@dpkit/metadata"
3+
import { beforeEach, describe, expect, it, vi } from "vitest"
4+
import { DescriptorPlugin } from "./plugin.ts"
5+
6+
vi.mock("@dpkit/metadata", async () => {
7+
const actual = await vi.importActual("@dpkit/metadata")
8+
return {
9+
...actual,
10+
loadPackageDescriptor: vi.fn(),
11+
savePackageDescriptor: vi.fn(),
12+
}
13+
})
14+
15+
describe("DescriptorPlugin", () => {
16+
let plugin: DescriptorPlugin
17+
let mockLoadPackageDescriptor: ReturnType<typeof vi.fn>
18+
let mockSavePackageDescriptor: ReturnType<typeof vi.fn>
19+
20+
beforeEach(() => {
21+
plugin = new DescriptorPlugin()
22+
mockLoadPackageDescriptor = vi.mocked(metadataModule.loadPackageDescriptor)
23+
mockSavePackageDescriptor = vi.mocked(metadataModule.savePackageDescriptor)
24+
vi.clearAllMocks()
25+
})
26+
27+
describe("loadPackage", () => {
28+
it("should load package from local datapackage.json file", async () => {
29+
const mockPackage: Package = {
30+
name: "test-package",
31+
resources: [{ name: "test", data: [] }],
32+
}
33+
mockLoadPackageDescriptor.mockResolvedValue(mockPackage)
34+
35+
const result = await plugin.loadPackage("./datapackage.json")
36+
37+
expect(mockLoadPackageDescriptor).toHaveBeenCalledWith(
38+
"./datapackage.json",
39+
)
40+
expect(result).toEqual(mockPackage)
41+
})
42+
43+
it("should load package from local json file", async () => {
44+
const mockPackage: Package = {
45+
name: "test-package",
46+
resources: [{ name: "test", data: [] }],
47+
}
48+
mockLoadPackageDescriptor.mockResolvedValue(mockPackage)
49+
50+
const result = await plugin.loadPackage("./package.json")
51+
52+
expect(mockLoadPackageDescriptor).toHaveBeenCalledWith("./package.json")
53+
expect(result).toEqual(mockPackage)
54+
})
55+
56+
it("should return undefined for remote json urls", async () => {
57+
const result = await plugin.loadPackage(
58+
"https://example.com/datapackage.json",
59+
)
60+
61+
expect(mockLoadPackageDescriptor).not.toHaveBeenCalled()
62+
expect(result).toBeUndefined()
63+
})
64+
65+
it("should return undefined for http remote json urls", async () => {
66+
const result = await plugin.loadPackage(
67+
"http://example.com/datapackage.json",
68+
)
69+
70+
expect(mockLoadPackageDescriptor).not.toHaveBeenCalled()
71+
expect(result).toBeUndefined()
72+
})
73+
74+
it("should return undefined for local csv files", async () => {
75+
const result = await plugin.loadPackage("./data.csv")
76+
77+
expect(mockLoadPackageDescriptor).not.toHaveBeenCalled()
78+
expect(result).toBeUndefined()
79+
})
80+
81+
it("should return undefined for local xlsx files", async () => {
82+
const result = await plugin.loadPackage("./data.xlsx")
83+
84+
expect(mockLoadPackageDescriptor).not.toHaveBeenCalled()
85+
expect(result).toBeUndefined()
86+
})
87+
88+
it("should return undefined for local parquet files", async () => {
89+
const result = await plugin.loadPackage("./data.parquet")
90+
91+
expect(mockLoadPackageDescriptor).not.toHaveBeenCalled()
92+
expect(result).toBeUndefined()
93+
})
94+
95+
it("should handle absolute paths", async () => {
96+
const mockPackage: Package = {
97+
name: "test-package",
98+
resources: [{ name: "test", data: [] }],
99+
}
100+
mockLoadPackageDescriptor.mockResolvedValue(mockPackage)
101+
102+
const result = await plugin.loadPackage("/absolute/path/datapackage.json")
103+
104+
expect(mockLoadPackageDescriptor).toHaveBeenCalledWith(
105+
"/absolute/path/datapackage.json",
106+
)
107+
expect(result).toEqual(mockPackage)
108+
})
109+
110+
it("should return undefined for github urls", async () => {
111+
const result = await plugin.loadPackage(
112+
"https://github.com/owner/repo/datapackage.json",
113+
)
114+
115+
expect(mockLoadPackageDescriptor).not.toHaveBeenCalled()
116+
expect(result).toBeUndefined()
117+
})
118+
119+
it("should return undefined for zenodo urls", async () => {
120+
const result = await plugin.loadPackage("https://zenodo.org/record/123")
121+
122+
expect(mockLoadPackageDescriptor).not.toHaveBeenCalled()
123+
expect(result).toBeUndefined()
124+
})
125+
})
126+
127+
describe("savePackage", () => {
128+
const mockPackage: Package = {
129+
name: "test-package",
130+
resources: [{ name: "test", data: [] }],
131+
}
132+
133+
it("should save package to local datapackage.json file", async () => {
134+
mockSavePackageDescriptor.mockResolvedValue(undefined)
135+
136+
const result = await plugin.savePackage(mockPackage, {
137+
target: "./datapackage.json",
138+
})
139+
140+
expect(mockSavePackageDescriptor).toHaveBeenCalledWith(mockPackage, {
141+
path: "./datapackage.json",
142+
})
143+
expect(result).toEqual({ path: "./datapackage.json" })
144+
})
145+
146+
it("should save package with absolute path", async () => {
147+
mockSavePackageDescriptor.mockResolvedValue(undefined)
148+
149+
const result = await plugin.savePackage(mockPackage, {
150+
target: "/absolute/path/datapackage.json",
151+
})
152+
153+
expect(mockSavePackageDescriptor).toHaveBeenCalledWith(mockPackage, {
154+
path: "/absolute/path/datapackage.json",
155+
})
156+
expect(result).toEqual({ path: "/absolute/path/datapackage.json" })
157+
})
158+
159+
it("should return undefined for remote urls", async () => {
160+
const result = await plugin.savePackage(mockPackage, {
161+
target: "https://example.com/datapackage.json",
162+
})
163+
164+
expect(mockSavePackageDescriptor).not.toHaveBeenCalled()
165+
expect(result).toBeUndefined()
166+
})
167+
168+
it("should return undefined for local json files not named datapackage.json", async () => {
169+
const result = await plugin.savePackage(mockPackage, {
170+
target: "./package.json",
171+
})
172+
173+
expect(mockSavePackageDescriptor).not.toHaveBeenCalled()
174+
expect(result).toBeUndefined()
175+
})
176+
177+
it("should return undefined for local csv files", async () => {
178+
const result = await plugin.savePackage(mockPackage, {
179+
target: "./data.csv",
180+
})
181+
182+
expect(mockSavePackageDescriptor).not.toHaveBeenCalled()
183+
expect(result).toBeUndefined()
184+
})
185+
186+
it("should return undefined for local xlsx files", async () => {
187+
const result = await plugin.savePackage(mockPackage, {
188+
target: "./data.xlsx",
189+
})
190+
191+
expect(mockSavePackageDescriptor).not.toHaveBeenCalled()
192+
expect(result).toBeUndefined()
193+
})
194+
195+
it("should return undefined for http urls", async () => {
196+
const result = await plugin.savePackage(mockPackage, {
197+
target: "http://example.com/datapackage.json",
198+
})
199+
200+
expect(mockSavePackageDescriptor).not.toHaveBeenCalled()
201+
expect(result).toBeUndefined()
202+
})
203+
204+
it("should ignore withRemote option for local files", async () => {
205+
mockSavePackageDescriptor.mockResolvedValue(undefined)
206+
207+
const result = await plugin.savePackage(mockPackage, {
208+
target: "./datapackage.json",
209+
withRemote: true,
210+
})
211+
212+
expect(mockSavePackageDescriptor).toHaveBeenCalledWith(mockPackage, {
213+
path: "./datapackage.json",
214+
})
215+
expect(result).toEqual({ path: "./datapackage.json" })
216+
})
217+
218+
it("should return undefined for local directories", async () => {
219+
const result = await plugin.savePackage(mockPackage, {
220+
target: "./data",
221+
})
222+
223+
expect(mockSavePackageDescriptor).not.toHaveBeenCalled()
224+
expect(result).toBeUndefined()
225+
})
226+
})
227+
})

dataset/plugins/github/package/convert/toGithub.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)