Created
October 9, 2019 23:21
-
-
Save christopheranderson/57081bce1236a6941a553e9b9333c22d to your computer and use it in GitHub Desktop.
Test using upsert with condition on non-existing always succeeds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@ts-check | |
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0"; | |
const { CosmosClient } = require("@azure/cosmos"); | |
const client = new CosmosClient({ | |
endpoint: "https://localhost:8081", | |
key: | |
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" | |
}); | |
async function init() { | |
const { database } = await client.databases.createIfNotExists({ id: "test" }); | |
const { container } = await database.containers.createIfNotExists({ | |
id: "test", | |
partitionKey: { | |
paths: ["/id"] | |
} | |
}); | |
try { | |
await container.item("1", "1").delete(); | |
} catch (e) { | |
// do nothing | |
} | |
try { | |
await container.item("2", "2").delete(); | |
} catch (e) { | |
// do nothing | |
} | |
try { | |
await container.item("3", "3").delete(); | |
} catch (e) { | |
// do nothing | |
} | |
} | |
async function run() { | |
const testC = client.database("test").container("test"); | |
console.log("1. Creating item"); | |
const firstResponse = await testC.items.create({ | |
id: "1" | |
}); | |
console.log(" -> Success!"); | |
try { | |
console.log("2. Trying to upsert existing"); | |
const tryUpsertExisting = await testC.items.upsert( | |
{ | |
id: "1" | |
}, | |
{ | |
accessCondition: { | |
type: "IfMatch", | |
condition: firstResponse.etag | |
} | |
} | |
); | |
console.log(" -> Success!"); | |
} catch (e) { | |
console.error("Failed to upsert existing"); | |
console.error(e); | |
throw e; | |
} | |
try { | |
console.log("3. Trying to upsert existing with bad etag"); | |
const tryUpsertExisting = await testC.items.upsert( | |
{ | |
id: "1" | |
}, | |
{ | |
accessCondition: { | |
type: "IfMatch", | |
condition: "garbage!!!!!" | |
} | |
} | |
); | |
throw new Error("This should have thrown"); | |
} catch (e) { | |
if (e.code === 412) { | |
console.log(" -> Success! It threw"); | |
} else { | |
console.error("Failed to fail upsert existing with bad etag"); | |
console.error(e); | |
throw e; | |
} | |
} | |
try { | |
console.log("4. Trying to upsert not-existent"); | |
const tryUpsertNotExistingWithEtag = await testC.items.upsert( | |
{ | |
id: "2" | |
}, | |
{ | |
accessCondition: { | |
type: "IfMatch", | |
condition: firstResponse.etag | |
} | |
} | |
); | |
console.log(" -> Success!"); | |
} catch (e) { | |
console.error("Failed to upsert non-existing"); | |
console.error(e); | |
throw e; | |
} | |
try { | |
console.log("5. Trying to upsert not-existent & garbage"); | |
const tryUpsertNotExistingWithEtag = await testC.items.upsert( | |
{ | |
id: "3" | |
}, | |
{ | |
accessCondition: { | |
type: "IfMatch", | |
condition: "garbage!!!!!!!!!!!asdfasdfasdf" | |
} | |
} | |
); | |
console.log(" -> Success!"); | |
} catch (e) { | |
console.error("Failed to upsert non-existing"); | |
console.error(e); | |
throw e; | |
} | |
} | |
async function main() { | |
console.log("################### Started ###################### "); | |
console.log("------------ Ensuring resources exist ------------ "); | |
await init(); | |
console.log("------------------- Success ---------------------- "); | |
console.log("------------------ Running sample ---------------- "); | |
await run(); | |
console.log("------------------- Success ---------------------- "); | |
console.log("##################### Done ####################### "); | |
} | |
main() | |
.catch(console.error) | |
.then(() => process.exit(0)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment