-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcomputed-indexed-properties.test.mjs
More file actions
175 lines (156 loc) · 6.48 KB
/
Copy pathcomputed-indexed-properties.test.mjs
File metadata and controls
175 lines (156 loc) · 6.48 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Computed indexed properties integration tests.
*
* Ported from legacy `apiTests/tests/18_computedIndexedProperties.mjs`. Validates:
* - `@computed(from: "...")` expressions produce correct indexed values
* - `@computed` JS-callback attributes (`setComputedAttribute`) produce correct indexed values
* - Non-indexed computed attributes round-trip correctly
* - REST and operations API both surface computed values
*
* Self-contained: installs a `computed` component that defines a `Product` table
* (schema `data`) with three computed fields, seeds one record, exercises read /
* filter paths, then drops the record, table, and component.
*
* Skipped on Windows: depends on `restart_service http_workers` after component
* install, which crashes Harper on the Windows single-worker model
* (HarperFast/harper#549).
*/
import { suite, test, before, after } from 'node:test';
import assert from 'node:assert/strict';
import request from 'supertest';
import { startHarper, teardownHarper } from '@harperfast/integration-testing';
import { createApiClient } from './utils/client.mjs';
import { installAppComponent } from './utils/components.mjs';
const SCHEMA_GRAPHQL =
'type Product @table @export { \n\t id: ID @primaryKey \n\t price: Float \n\t taxRate: Float \n\t' +
' totalPrice: Float @computed(from: "price + (price * taxRate)") @indexed \n\t' +
' notIndexedTotalPrice: Float @computed(from: "price + (price * taxRate)") \n\t' +
' jsTotalPrice: Float @computed @indexed \n } \n\n';
const RESOURCES_JS =
"tables.Product.setComputedAttribute('jsTotalPrice', (record) => { \n\t return record.price + (record.price * record.taxRate) \n }) \n\n";
const skipSuite = process.platform === 'win32';
suite('Computed indexed properties', { skip: skipSuite }, (ctx) => {
let client;
before(async () => {
await startHarper(ctx, { config: {}, env: {} });
client = createApiClient(ctx.harper);
await installAppComponent(client, {
project: 'computed',
files: { 'schema.graphql': SCHEMA_GRAPHQL, 'resources.js': RESOURCES_JS },
probePath: '/Product/',
});
});
after(async () => {
await teardownHarper(ctx);
});
test('PUT Product record via REST', async () => {
await request(client.restURL)
.put('/Product/1')
.set(client.headers)
.send({ id: '1', price: 100, taxRate: 0.19 })
.expect(204);
});
test('search_by_value returns raw fields', async () => {
const r = await client
.req()
.send({
operation: 'search_by_value',
schema: 'data',
table: 'Product',
search_attribute: 'id',
search_value: '1',
})
.expect(200);
assert.ok(Array.isArray(r.body), r.text);
assert.equal(r.body[0].id, '1', r.text);
assert.equal(r.body[0].price, 100, r.text);
assert.equal(r.body[0].taxRate, 0.19, r.text);
});
test('search_by_value with get_attributes returns computed values', async () => {
const r = await client
.req()
.send({
operation: 'search_by_value',
schema: 'data',
table: 'Product',
search_attribute: 'id',
search_value: '1',
get_attributes: ['id', 'price', 'taxRate', 'totalPrice', 'notIndexedTotalPrice', 'jsTotalPrice'],
})
.expect(200);
assert.ok(Array.isArray(r.body), r.text);
assert.equal(r.body[0].id, '1', r.text);
assert.equal(r.body[0].price, 100, r.text);
assert.equal(r.body[0].taxRate, 0.19, r.text);
assert.equal(r.body[0].totalPrice, 119, r.text);
assert.equal(r.body[0].notIndexedTotalPrice, 119, r.text);
// jsTotalPrice is intentionally not asserted here: search_by_value returns
// the stored indexed value, which can be null if the record was PUT before
// resources.js finished initialising (setComputedAttribute is a runtime
// call, not a schema-time expression). The value is verified via REST GET
// with ?select below, which computes it on-demand.
});
test('REST GET by id returns raw fields', async () => {
const r = await client.reqRest('/Product/1').expect(200);
assert.equal(r.body.id, '1', r.text);
assert.equal(r.body.price, 100, r.text);
assert.equal(r.body.taxRate, 0.19, r.text);
});
test('REST GET by id with select returns all computed values', async () => {
const r = await client
.reqRest('/Product/1?select(id,price,taxRate,totalPrice,notIndexedTotalPrice,jsTotalPrice)')
.expect(200);
assert.equal(r.body.id, '1', r.text);
assert.equal(r.body.price, 100, r.text);
assert.equal(r.body.taxRate, 0.19, r.text);
assert.equal(r.body.totalPrice, 119, r.text);
assert.equal(r.body.notIndexedTotalPrice, 119, r.text);
assert.equal(r.body.jsTotalPrice, 119, r.text);
});
test('REST filter by JS-computed indexed attribute', async () => {
const r = await client
.reqRest('/Product/?jsTotalPrice=119&select(id,price,taxRate,totalPrice,notIndexedTotalPrice,jsTotalPrice)')
.expect(200);
assert.ok(Array.isArray(r.body), r.text);
assert.equal(r.body[0].id, '1', r.text);
assert.equal(r.body[0].price, 100, r.text);
assert.equal(r.body[0].taxRate, 0.19, r.text);
assert.equal(r.body[0].totalPrice, 119, r.text);
assert.equal(r.body[0].notIndexedTotalPrice, 119, r.text);
assert.equal(r.body[0].jsTotalPrice, 119, r.text);
});
test('REST filter by expression-computed indexed attribute', async () => {
const r = await client
.reqRest('/Product/?totalPrice=119&select(id,price,taxRate,totalPrice,notIndexedTotalPrice,jsTotalPrice)')
.expect(200);
assert.ok(Array.isArray(r.body), r.text);
assert.equal(r.body[0].id, '1', r.text);
assert.equal(r.body[0].price, 100, r.text);
assert.equal(r.body[0].taxRate, 0.19, r.text);
assert.equal(r.body[0].totalPrice, 119, r.text);
assert.equal(r.body[0].notIndexedTotalPrice, 119, r.text);
assert.equal(r.body[0].jsTotalPrice, 119, r.text);
});
test('delete Product record', async () => {
await client
.req()
.send({ operation: 'delete', schema: 'data', table: 'Product', ids: ['1'] })
.expect((r) => assert.ok(r.body.message.includes('1 of 1 record successfully deleted'), r.text))
.expect((r) => assert.deepEqual(r.body.deleted_hashes, ['1'], r.text))
.expect(200);
});
test('drop_table Product', async () => {
await client
.req()
.send({ operation: 'drop_table', schema: 'data', table: 'Product' })
.expect((r) => assert.ok(r.body.message.includes(`successfully deleted table 'data.Product'`), r.text))
.expect(200);
});
test('drop_component computed', async () => {
await client
.req()
.send({ operation: 'drop_component', project: 'computed' })
.expect((r) => assert.ok(r.body.message.includes('Successfully dropped: computed'), r.text))
.expect(200);
});
});