16
16
//
17
17
////////////////////////////////////////////////////////////////////////////
18
18
19
- import Realm , { ObjectSchema , PropertySchema } from "realm" ;
19
+ import { expect } from "chai" ;
20
+ import Realm , { BSON , ObjectSchema , PropertySchema , PropertySchemaShorthand } from "realm" ;
20
21
21
22
import { describePerformance } from "../utils/benchmark" ;
22
23
23
24
type Value = ( ( realm : Realm ) => unknown ) | unknown ;
24
25
25
- function getTypeName ( type : Realm . PropertySchemaShorthand | Realm . PropertySchema ) {
26
- if ( typeof type === "object" ) {
27
- const prefix = type . optional ? "optional " : "" ;
28
- if ( type . objectType ) {
29
- return prefix + `${ type . type } <${ type . objectType } >` ;
30
- } else {
31
- return prefix + type . type ;
32
- }
33
- } else {
34
- return type ;
26
+ const COLLECTION_MARKERS : Readonly < Record < string , string > > = {
27
+ list : "[]" ,
28
+ dictionary : "{}" ,
29
+ set : "<>" ,
30
+ } ;
31
+
32
+ /**
33
+ * Get a representative consistent name of the type depending on the schema.
34
+ *
35
+ * @example
36
+ * "int?[]" -> "int?[]"
37
+ * { type: "list", objectType: "int", optional: true } -> "int?[]"
38
+ */
39
+ function getTypeDisplayName ( schema : PropertySchemaShorthand | PropertySchema ) {
40
+ const isShorthand = typeof schema === "string" ;
41
+ if ( isShorthand ) {
42
+ return schema ;
35
43
}
44
+
45
+ const optionalMarker = schema . optional ? "?" : "" ;
46
+ const collectionMarker = COLLECTION_MARKERS [ schema . type ] ?? "" ;
47
+
48
+ return ( schema . objectType || schema . type ) + optionalMarker + collectionMarker ;
36
49
}
37
50
38
51
type TestParameters = {
39
- name ?: string ;
40
- type : Realm . PropertySchemaShorthand | Realm . PropertySchema ;
52
+ propertySchema : PropertySchemaShorthand | PropertySchema ;
41
53
value : Value ;
42
- schema ?: Realm . ObjectSchema [ ] ;
54
+ extraObjectSchemas ?: ObjectSchema [ ] ;
43
55
} ;
44
56
45
- function describeTypeRead ( { type , value, schema = [ ] } : TestParameters ) {
46
- const typeName = getTypeName ( type ) ;
47
- const objectSchemaName = type + "Class" ;
48
- const propertyName = type + "Prop" ;
57
+ function describeTypeRead ( { propertySchema , value, extraObjectSchemas = [ ] } : TestParameters ) {
58
+ const typeDisplayName = getTypeDisplayName ( propertySchema ) ;
59
+ const objectSchemaName = typeDisplayName + "Class" ;
60
+ const propertyName = typeDisplayName + "Prop" ;
49
61
50
62
const defaultSchema : ObjectSchema = {
51
63
name : objectSchemaName ,
52
64
properties : {
53
- [ propertyName ] :
54
- typeof type === "object"
55
- ? type
56
- : ( {
57
- type,
58
- optional : true ,
59
- } as PropertySchema ) ,
65
+ [ propertyName ] : propertySchema ,
60
66
} ,
61
67
} ;
62
68
63
- describePerformance ( `reading property of type '${ typeName } '` , {
64
- schema : [ defaultSchema , ...schema ] ,
65
- benchmarkTitle : `reads ${ typeName } ` ,
69
+ describePerformance ( `reading property of type '${ typeDisplayName } '` , {
70
+ schema : [ defaultSchema , ...extraObjectSchemas ] ,
71
+ benchmarkTitle : `reads ${ typeDisplayName } ` ,
66
72
before ( this : Partial < RealmObjectContext > & RealmContext & Mocha . Context ) {
67
73
this . realm . write ( ( ) => {
68
74
this . object = this . realm . create ( objectSchemaName , {
@@ -76,15 +82,17 @@ function describeTypeRead({ type, value, schema = [] }: TestParameters) {
76
82
} ,
77
83
test ( this : RealmObjectContext ) {
78
84
const value = this . object [ propertyName ] ;
85
+ // Performing a check to avoid the get of the property to be optimized away.
79
86
if ( typeof value === "undefined" ) {
80
- // Performing a check to avoid the get of the property to be optimized away
81
87
throw new Error ( "Expected a value" ) ;
82
88
}
83
89
} ,
84
90
} ) ;
85
91
}
86
92
87
- const cases : Array < TestParameters | [ Realm . PropertySchemaShorthand | Realm . PropertySchema , Value ] > = [
93
+ type SchemaValuePair = [ PropertySchemaShorthand | PropertySchema , Value ] ;
94
+
95
+ const cases : ( TestParameters | SchemaValuePair ) [ ] = [
88
96
[ "bool?" , true ] ,
89
97
[ "bool" , true ] ,
90
98
[ "int?" , 123 ] ,
@@ -93,15 +101,15 @@ const cases: Array<TestParameters | [Realm.PropertySchemaShorthand | Realm.Prope
93
101
[ "double?" , 123.456 ] ,
94
102
[ "double" , 123.456 ] ,
95
103
[ "string?" , "Hello!" ] ,
96
- [ "decimal128?" , new Realm . BSON . Decimal128 ( "123" ) ] ,
97
- [ "objectId?" , new Realm . BSON . ObjectId ( "0000002a9a7969d24bea4cf4" ) ] ,
98
- [ "uuid?" , new Realm . BSON . UUID ( ) ] ,
104
+ [ "decimal128?" , new BSON . Decimal128 ( "123" ) ] ,
105
+ [ "objectId?" , new BSON . ObjectId ( "0000002a9a7969d24bea4cf4" ) ] ,
106
+ [ "uuid?" , new BSON . UUID ( ) ] ,
99
107
[ "date?" , new Date ( "2000-01-01" ) ] ,
100
108
[ "data?" , new Uint8Array ( [ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 ] ) ] ,
101
109
{
102
- type : "Car" ,
103
- schema : [ { name : "Car" , properties : { model : "string" } } ] ,
110
+ propertySchema : "Car" ,
104
111
value : ( realm : Realm ) => realm . create ( "Car" , { model : "VW Touran" } ) ,
112
+ extraObjectSchemas : [ { name : "Car" , properties : { model : "string" } } ] ,
105
113
} ,
106
114
[ "bool?[]" , [ ] ] ,
107
115
[ "bool?<>" , [ ] ] ,
@@ -111,10 +119,26 @@ const cases: Array<TestParameters | [Realm.PropertySchemaShorthand | Realm.Prope
111
119
describe . skipIf ( environment . performance !== true , "Property read performance" , ( ) => {
112
120
for ( const c of cases ) {
113
121
if ( Array . isArray ( c ) ) {
114
- const [ type , value ] = c ;
115
- describeTypeRead ( { type , value } ) ;
122
+ const [ propertySchema , value ] = c ;
123
+ describeTypeRead ( { propertySchema , value } ) ;
116
124
} else {
117
125
describeTypeRead ( c ) ;
118
126
}
119
127
}
128
+
129
+ describe ( "Helpers" , ( ) => {
130
+ it ( "getTypeDisplayName()" , function ( ) {
131
+ expect ( getTypeDisplayName ( "int" ) ) . equals ( "int" ) ;
132
+ expect ( getTypeDisplayName ( "int?" ) ) . equals ( "int?" ) ;
133
+ expect ( getTypeDisplayName ( "int?[]" ) ) . equals ( "int?[]" ) ;
134
+ expect ( getTypeDisplayName ( "Car" ) ) . equals ( "Car" ) ;
135
+ expect ( getTypeDisplayName ( { type : "int" } ) ) . equals ( "int" ) ;
136
+ expect ( getTypeDisplayName ( { type : "list" , objectType : "int" } ) ) . equals ( "int[]" ) ;
137
+ expect ( getTypeDisplayName ( { type : "list" , objectType : "int" , optional : true } ) ) . equals ( "int?[]" ) ;
138
+ expect ( getTypeDisplayName ( { type : "dictionary" , objectType : "int" } ) ) . equals ( "int{}" ) ;
139
+ expect ( getTypeDisplayName ( { type : "set" , objectType : "int" } ) ) . equals ( "int<>" ) ;
140
+ expect ( getTypeDisplayName ( { type : "object" , objectType : "Car" } ) ) . equals ( "Car" ) ;
141
+ expect ( getTypeDisplayName ( { type : "object" , objectType : "Car" , optional : true } ) ) . equals ( "Car?" ) ;
142
+ } ) ;
143
+ } ) ;
120
144
} ) ;
0 commit comments