-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdata-integrity.test.ts
More file actions
140 lines (125 loc) · 4.65 KB
/
data-integrity.test.ts
File metadata and controls
140 lines (125 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { expect, test } from 'vitest';
import type { Paper, Person, Spotlight, FeaturedVenue, News, Venue, Course, BlogPostMeta, BlogPost } from './lib/app-types';
import papersIndexRaw from '../static/papers-index.json?raw';
import peopleRaw from '../static/people.json?raw';
import spotlightRaw from '../static/spotlight.json?raw';
import featuredVenuesRaw from '../static/featured-venues.json?raw';
import venuesRaw from '../static/venues.json?raw';
import newsRaw from '../static/news.json?raw';
import courseRaw from '../static/courses.json?raw';
import postIndexRaw from "../static/blog-index.json?raw";
import tsj from 'ts-json-schema-generator';
import Ajv from 'ajv';
const papers = JSON.parse(papersIndexRaw) as Paper[];
const people = JSON.parse(peopleRaw) as Person[];
const spotlight = JSON.parse(spotlightRaw) as Paper[];
const featuredVenues = JSON.parse(featuredVenuesRaw) as FeaturedVenue[];
const venues = JSON.parse(venuesRaw) as Venue[];
const news = JSON.parse(newsRaw) as Paper[];
const courses = JSON.parse(courseRaw) as Course[];
const postIndex = JSON.parse(postIndexRaw) as BlogPostMeta[];
test('Web names should be unique', () => {
const webNames = new Set(papers.map((paper) => paper.web_name));
expect(webNames.size).toBe(papers.length);
});
test('All papers should have corresponding venues', () => {
const venuesSet = new Set(papers.map((paper) => paper.venue));
const venues = featuredVenues.map((venue) => venue.venue_nickname);
const missingVenues = venues.filter((venue) => !venuesSet.has(venue));
expect(missingVenues).toEqual([]);
});
test('All venue nicknames should be unique', () => {
const venueNicknames = new Set(venues.map((venue) => venue.nickname));
expect(venueNicknames.size).toBe(venues.length);
});
test('All papers should have corresponding people', () => {
const peopleSet = new Set(people.map((person) => `${person.first_name} ${person.last_name}`));
const paperAuthors = papers
.flatMap((paper) => paper.authors.map((author) => `${author.first_name} ${author.last_name}`))
.filter((author) => !peopleSet.has(author));
expect(paperAuthors).toEqual([]);
});
test('All papers should have urls for their authors if possible', () => {
const authorUrls = Object.fromEntries(
people
.filter((person) => (person?.url || '').length)
.map((person) => [`${person.first_name} ${person.last_name}`, person.url])
);
const updatedPapers = papers.map((paper) => {
const authors = paper.authors.map((author) => {
return {
...author,
url: authorUrls[`${author.first_name} ${author.last_name}`]
};
});
return {
...paper,
authors
};
});
expect(updatedPapers).toEqual(papers);
});
test('All blog posts should have date and title', () => {
const postsWithMissingData = postIndex.filter((p) =>
// check if date, title, post (user-provided), and web name (auto-gen) are provided
!(p.meta.date && p.meta.title && p.meta.web_name && p.post)
// check if an external post has a headliner for preview
&& (!p.meta.external || p.meta.headliner)
);
expect(postsWithMissingData).toEqual([]);
});
[
{ key: 'Paper', dataset: papers, accessor: (paper: Paper): string => paper.web_name },
{
key: 'Person',
dataset: people,
accessor: (person: Person): string => `${person.first_name} ${person.last_name}`
},
{
key: 'Spotlight',
dataset: spotlight,
accessor: (spotlight: Spotlight): string => spotlight.title
},
{
key: 'FeaturedVenue',
dataset: featuredVenues,
accessor: (venue: FeaturedVenue): string => venue.desc
},
{
key: 'News',
dataset: news,
accessor: (x: News): string => x.text
},
{
key: 'Venue',
dataset: venues,
accessor: (venue: Venue): string => venue.nickname
},
{
key: 'Course',
dataset: courses as Course[],
accessor: (course: Course): string => course.name
},
{ key: 'BlogPost', dataset: postIndex, accessor: (post: BlogPost): string => post.meta.web_name }
].forEach(({ key, dataset, accessor }) => {
test(`All ${key} values should be filled out`, () => {
const ajv = new Ajv({ allErrors: true });
const config = {
path: 'src/lib/app-types.ts',
tsconfig: 'tsconfig.json',
type: key,
additionalProperties: false
};
const schema = tsj.createGenerator(config).createSchema(config.type);
dataset.forEach((unit) => {
const result = ajv.validate(schema, unit);
if (!result) {
console.log(ajv.errors);
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const unitName = accessor(unit);
expect(result, `${key}: "${unitName}" is invalid`).toBe(true);
});
});
});