Skip to content

Commit

Permalink
updating more persistence tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jvigliotta committed Jan 13, 2025
1 parent ccf2e8c commit f317dcb
Showing 1 changed file with 179 additions and 181 deletions.
360 changes: 179 additions & 181 deletions src/persistence/test/MCWSPersistenceProviderSpec.js
Original file line number Diff line number Diff line change
@@ -1,189 +1,187 @@
/*global define,Promise,describe,it,expect,beforeEach,jasmine*/

define([
// "../src/MCWSPersistenceProvider",
// '../../../services/mcws/mcws'
], function (
// MCWSPersistenceProvider,
// mcwsDefault
) {
"use strict";

// const mcws = mcwsDefault.default;

xdescribe("The MCWS Persistence Provider", function () {
var mockQ,
mockNamespaceService,
persistenceNamespaces,
mockNamespace,
mockOpaqueFile,
persistence;

beforeEach(function () {
mockQ = jasmine.createSpyObj("$q", ["when"]);
spyOn(mcws, 'namespace');
mockNamespaceService = jasmine.createSpyObj(
"namespaceService",
["getPersistenceNamespaces"]
);
persistenceNamespaces = [
{
key: "testSpace",
url: "/test/namespace"
},
{
key: "testSpace2",
url: "/test/namespace2"
}
];

mockNamespaceService
.getPersistenceNamespaces
.and.returnValue(Promise.resolve(persistenceNamespaces));


mockNamespace =
jasmine.createSpyObj("namespace", ["opaqueFile", "read"]);
mockOpaqueFile = jasmine.createSpyObj("opaqueFile", [
"read",
"replace",
"remove",
"create"
]);

mockQ.when.and.callFake(function (resolver) {
return Promise.resolve(resolver);
});

mcws.namespace.and.returnValue(mockNamespace);
mockNamespace.opaqueFile.and.returnValue(mockOpaqueFile);

persistence = new MCWSPersistenceProvider(
mockQ,
mockNamespaceService
);
});

it("provides a promise for available spaces", function (done) {
persistence.listSpaces()
.then(function (spaces) {
expect(spaces).toEqual(['testSpace', 'testSpace2']);
done();
})
});
import MCWSPersistenceProvider from '../MCWSPersistenceProvider';
import mcws from '../../services/mcws/mcws';

fdescribe('The MCWS Persistence Provider', () => {
let mockNamespaceService;
let persistenceNamespaces;
let mockNamespace;
let mockOpaqueFile;
let persistence;

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable persistence.
let openmct;
let mcwsPersistenceProvider;

beforeEach(() => {
openmct = {
user: {
getCurrentUser: () => Promise.resolve({ id: 'myUser' })
}
};

spyOn(mcws, 'namespace');
mockNamespaceService = jasmine.createSpyObj(
'namespaceService',
['getPersistenceNamespaces']
);

persistenceNamespaces = [
{
key: 'testSpace',
url: '/test/namespace'
},
{
key: 'testSpace2',
url: '/test/namespace2'
}
];

mockNamespaceService
.getPersistenceNamespaces
.and.returnValue(Promise.resolve(persistenceNamespaces));


mockNamespace =
jasmine.createSpyObj('namespace', ['opaqueFile', 'read']);
mockOpaqueFile = jasmine.createSpyObj('opaqueFile', [
'read',
'replace',
'remove',
'create'
]);

mcws.namespace.and.returnValue(mockNamespace);
mockNamespace.opaqueFile.and.returnValue(mockOpaqueFile);

mcwsPersistenceProvider = new MCWSPersistenceProvider(
openmct,
persistenceNamespaces
);
});

it("provides a listing of identifiers", function (done) {
mockNamespace.read.and.returnValue(Promise.resolve([
{
Subject: "/test/namespace/a",
Predicate: "has MIO type",
Object: "opaque_file"
},
{
Subject: "/test/namespace/b",
Predicate: "has MIO type",
Object: "namespace"
},
{
Subject: "/test/namespace/c",
Predicate: "something else",
Object: "opaque_file"
},
{
Subject: "/some/namespace/xyz",
Predicate: "has MIO type",
Object: "opaque_file"
},
{
Subject: "/some/namespace/123-ABC",
Predicate: "has MIO type",
Object: "opaque_file"
}
]));
persistence
.listObjects("testSpace")
.then(function (objects) {
expect(objects).toEqual(["a", "xyz", "123-ABC"]);
done();
});
});
it('provides a promise for available namespaces', async (done) => {
const spaces = await mcwsPersistenceProvider.getPersistenceNamespaces();
expect(spaces).toEqual(persistenceNamespaces);
done();
});

// DO WE DELETE THIS TEST? I don't think we use this functionality anymore.
xit('provides a listing of namespaces when provided a namespace definition', async (done) => {
const namespaceTriples = [
{
Subject: '/test/namespace/a',
Predicate: 'has MIO type',
Object: 'opaque_file'
},
{
Subject: '/test/namespace/b',
Predicate: 'has MIO type',
Object: 'namespace'
},
{
Subject: '/test/namespace/c',
Predicate: 'something else',
Object: 'opaque_file'
},
{
Subject: '/some/namespace/xyz',
Predicate: 'has MIO type',
Object: 'opaque_file'
},
{
Subject: '/some/namespace/123-ABC',
Predicate: 'has MIO type',
Object: 'opaque_file'
}
];
mockNamespace.read.and.returnValue(Promise.resolve(namespaceTriples));

const objects = await mcwsPersistenceProvider.getNamespacesFromMCWS({ url: '/test/namespace' });
expect(objects).toEqual(namespaceTriples);
done();
});

it("allows objects to be created", function (done) {
mockOpaqueFile.create.and.returnValue(Promise.resolve(true));
persistence
.createObject(
"testSpace",
"testKey",
{ someKey: "some value" }
)
.then(function (result) {
expect(result).toBe(true);
expect(mockNamespace.opaqueFile)
.toHaveBeenCalledWith("testKey");
expect(mockOpaqueFile.create)
.toHaveBeenCalledWith({ someKey: "some value" });
done();
});
});
it('allows objects to be created', async (done) => {
mockOpaqueFile.create.and.returnValue(Promise.resolve(true));
const domainObject = {
identifier: {
key: 'testKey',
namespace: 'testSpace'
},
someKey: 'some value'
};
const result = await mcwsPersistenceProvider.create(domainObject);

expect(result).toBe(true);
expect(mockNamespace.opaqueFile)
.toHaveBeenCalledWith('testKey');
expect(mockOpaqueFile.create)
.toHaveBeenCalledWith({ someKey: 'some value' });
done();
});

it("allows objects to be read", function (done) {
mockOpaqueFile.read.and.returnValue(Promise.resolve("test object"));
persistence
.readObject("testSpace", "testKey")
.then(function (object) {
expect(object).toBe("test object");
expect(mcws.namespace)
.toHaveBeenCalledWith("/test/namespace");
expect(mockNamespace.opaqueFile)
.toHaveBeenCalledWith("testKey");
expect(mockOpaqueFile.read)
.toHaveBeenCalled();
done();
});
});
it('allows objects to be read', async (done) => {
const identifier = {
key: 'testKey',
namespace: 'testSpace'
};
const model = {
someKey: 'some value'
};
const domainObject = {
identifier,
...model
};
mockOpaqueFile.read.and.returnValue(Promise.resolve({json: () => Promise.resolve(model)}));
const object = await mcwsPersistenceProvider.get(identifier);

expect(object).toEqual(domainObject);
expect(mcws.namespace)
.toHaveBeenCalledWith('/test/namespace');
expect(mockNamespace.opaqueFile)
.toHaveBeenCalledWith('testKey');
expect(mockOpaqueFile.read)
.toHaveBeenCalled();
done();
});

it("allows objects to be updated", function (done) {
mockOpaqueFile.replace.and.returnValue(Promise.resolve(true));
persistence
.updateObject(
"testSpace",
"testKey",
{ someKey: "some value" }
)
.then(function (result) {
expect(result).toBe(true);
expect(mcws.namespace)
.toHaveBeenCalledWith("/test/namespace");
expect(mockNamespace.opaqueFile)
.toHaveBeenCalledWith("testKey");
expect(mockOpaqueFile.replace)
.toHaveBeenCalledWith({ someKey: "some value" });
done();
})
});
it('allows objects to be updated', async (done) => {
mockOpaqueFile.replace.and.returnValue(Promise.resolve(true));
const domainObject = {
identifier: {
key: 'testKey',
namespace: 'testSpace'
},
someKey: 'some value'
};
const result = await mcwsPersistenceProvider.update(domainObject);

expect(result).toBe(true);
expect(mcws.namespace)
.toHaveBeenCalledWith('/test/namespace');
expect(mockNamespace.opaqueFile)
.toHaveBeenCalledWith('testKey');
expect(mockOpaqueFile.replace)
.toHaveBeenCalledWith({ someKey: 'some value' });
done();
});

it("allows objects to be deleted", function (done) {
mockOpaqueFile.remove.and.returnValue(Promise.resolve(true));
persistence
.deleteObject("testSpace", "testKey")
.then(function (result) {
expect(mcws.namespace)
.toHaveBeenCalledWith("/test/namespace");
expect(mockNamespace.opaqueFile)
.toHaveBeenCalledWith("testKey");
expect(mockOpaqueFile.remove).toHaveBeenCalled();
done();
})
});
// We don't allow delete in the core API, so we don't need this test.
xit('allows objects to be deleted', async (done) => {
mockOpaqueFile.remove.and.returnValue(Promise.resolve(true));
await mcwsPersistenceProvider.delete({ key: 'testKey', namespace: 'testSpace' });

expect(mcws.namespace)
.toHaveBeenCalledWith('/test/namespace');
expect(mockNamespace.opaqueFile)
.toHaveBeenCalledWith('testKey');
expect(mockOpaqueFile.remove).toHaveBeenCalled();
done();
});

it("converts rejected promises to promises resolves to undefined", function (done) {
mockOpaqueFile.read.and.returnValue(Promise.reject("hello"));
persistence
.readObject("testSpace", "testKey")
.then(function (result) {
expect(result).toBeUndefined();
done();
})
});
it('converts rejected promises to promises resolves to undefined', async (done) => {
mockOpaqueFile.read.and.returnValue(Promise.reject('hello'));
const result = await mcwsPersistenceProvider.get({ key: 'testKey', namespace: 'testSpace' });

expect(result).toBeUndefined();
done();
});
});

0 comments on commit f317dcb

Please sign in to comment.