• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

KeychainMDIP / kc / 9716015453

28 Jun 2024 04:35PM UTC coverage: 89.361% (+10.1%) from 79.24%
9716015453

Pull #205

github

macterra
Added unit tests for testSchema
Pull Request #205: Improve test coverage

651 of 756 branches covered (86.11%)

Branch coverage included in aggregate %.

18 of 22 new or added lines in 3 files covered. (81.82%)

13 existing lines in 1 file now uncovered.

1054 of 1152 relevant lines covered (91.49%)

361.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.38
/db-json.js
1
import fs from 'fs';
2

3
const dataFolder = 'data';
4✔
4
let dbName;
5

6
function loadDb() {
7
    if (fs.existsSync(dbName)) {
6,622✔
8
        return JSON.parse(fs.readFileSync(dbName));
5,886✔
9
    }
10
    else {
11
        return {
736✔
12
            dids: {}
13
        }
14
    }
15
}
16

17
function writeDb(db) {
18
    if (!fs.existsSync(dataFolder)) {
2,558✔
19
        fs.mkdirSync(dataFolder, { recursive: true });
62✔
20
    }
21

22
    fs.writeFileSync(dbName, JSON.stringify(db, null, 4));
2,558✔
23
}
24

25
export async function start(name = 'mdip') {
50✔
26
    dbName = `${dataFolder}/${name}.json`;
428✔
27
}
28

29
export async function stop() {
30
}
31

32
export async function resetDb() {
33
    if (fs.existsSync(dbName)) {
10!
34
        fs.rmSync(dbName);
10✔
35
    }
36
}
37

38
export async function addEvent(did, event) {
39
    const db = loadDb();
1,248✔
40

41
    if (!did) {
1,248✔
42
        throw "Invalid DID";
×
43
    }
44

45
    const suffix = did.split(':').pop();
1,248✔
46

47
    if (Object.keys(db.dids).includes(suffix)) {
1,248✔
48
        db.dids[suffix].push(event);
308✔
49
    }
50
    else {
51
        db.dids[suffix] = [event];
940✔
52
    }
53

54
    writeDb(db);
1,248✔
55
}
56

57
export async function getEvents(did) {
58
    try {
4,042✔
59
        const db = loadDb();
4,042✔
60
        const suffix = did.split(':').pop();
4,042✔
61
        const updates = db.dids[suffix];
4,032✔
62

63
        if (updates && updates.length > 0) {
4,032✔
64
            return updates;
3,072✔
65
        }
66
        else {
67
            return [];
960✔
68
        }
69
    }
70
    catch {
71
        return [];
10✔
72
    }
73
}
74

75
export async function setEvents(did, events) {
76
    if (!did) {
12!
77
        throw "Invalid DID";
×
78
    }
79

80
    const db = loadDb();
12✔
81
    const suffix = did.split(':').pop();
12✔
82

83
    db.dids[suffix] = events;
12✔
84
    writeDb(db);
12✔
85
}
86

87
export async function deleteEvents(did) {
88
    const db = loadDb();
4✔
89
    const suffix = did.split(':').pop();
4✔
90

91
    if (db.dids[suffix]) {
4✔
92
        delete db.dids[suffix];
2✔
93
        writeDb(db);
2✔
94
    }
95
}
96

97
export async function queueOperation(registry, op) {
98
    const db = loadDb();
1,288✔
99

100
    if (!db.queue) {
1,288✔
101
        db.queue = {};
298✔
102
    }
103

104
    if (Object.keys(db.queue).includes(registry)) {
1,288✔
105
        db.queue[registry].push(op);
886✔
106
    }
107
    else {
108
        db.queue[registry] = [op];
402✔
109
    }
110

111
    writeDb(db);
1,288✔
112
}
113

114
export async function getQueue(registry) {
115
    try {
14✔
116
        const db = loadDb();
14✔
117
        const queue = db.queue[registry];
14✔
118

119
        if (queue) {
14!
120
            return queue;
14✔
121
        }
122
        else {
123
            return [];
×
124
        }
125
    }
126
    catch {
127
        return [];
×
128
    }
129
}
130

131
export async function clearQueue(registry, batch) {
132
    try {
10✔
133
        const db = loadDb();
10✔
134

135
        if (!db.queue) {
10✔
136
            return true;
2✔
137
        }
138

139
        const oldQueue = db.queue[registry];
8✔
140

141
        if (!oldQueue) {
8!
NEW
142
            return true;
×
143
        }
144

145
        const newQueue = oldQueue.filter(item => !batch.some(op => op.signature.value === item.signature.value));
84✔
146

147
        db.queue[registry] = newQueue;
8✔
148
        writeDb(db);
8✔
149

150
        return true;
8✔
151
    }
152
    catch (error) {
153
        return false;
×
154
    }
155
}
156

157
export async function getAllKeys() {
158
    const db = loadDb();
4✔
159
    const ids = Object.keys(db.dids);
4✔
160
    return ids;
4✔
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc