Skip to content

Commit

Permalink
Add tests for KeyValueKeysWithFilters
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhojha committed Sep 4, 2024
1 parent ca738a1 commit 9f2371d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/list_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ _test(KeyValueDeleteVsPurge)
_test(KeyValueDiscardOldToNew)
_test(KeyValueHistory)
_test(KeyValueKeys)
_test(KeyValueKeysWithFilters)
_test(KeyValueManager)
_test(KeyValueMirrorCrossDomains)
_test(KeyValueMirrorDirectGet)
Expand Down
134 changes: 134 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31470,6 +31470,140 @@ void test_KeyValueKeys(void)
JS_TEARDOWN;
}

void test_KeyValueKeysWithFilters(void)
{
natsStatus s;
kvStore *kv = NULL;
kvKeysList l;
char *k = NULL;
kvConfig kvc;
kvWatchOptions o;
int i;
const char **defaultSubject = (const char *[]){">"};
l.Count = 0;
l.Keys = NULL;

JS_SETUP(2, 10, 14);

test("Create KV: ");
kvConfig_Init(&kvc);
kvc.Bucket = "KVS";
kvc.History = 2;
s = js_CreateKeyValue(&kv, js, &kvc);
testCond(s == NATS_OK);

test("Populate: ");
s = kvStore_PutString(NULL, kv, "a.b", "a.b");
IFOK(s, kvStore_PutString(NULL, kv, "a.d", "a.d"));
IFOK(s, kvStore_PutString(NULL, kv, "c.d", "c.d"));
IFOK(s, kvStore_PutString(NULL, kv, "e.f", "e.f"));
IFOK(s, kvStore_PutString(NULL, kv, "e.a.f", "e.a.f"));
testCond(s == NATS_OK);

test("Get keys with filters (bad args): List is null");
// list is NULL
s = kvStore_KeysWithFilters(NULL, kv, NULL,defaultSubject,1);
testCond((s == NATS_INVALID_ARG) && (l.Keys == NULL) && (l.Count == 0));
nats_clearLastError();

test("Get keys with filters (bad args): filters is null");
// filters is NULL
s = kvStore_KeysWithFilters(NULL, kv, NULL,NULL,0);
testCond((s == NATS_INVALID_ARG) && (l.Keys == NULL) && (l.Count == 0));
nats_clearLastError();

test("Get keys with filters (bad args): numFilters is 0");
// filters is NULL
s = kvStore_KeysWithFilters(&l, kv, NULL,defaultSubject,0);
testCond((s == NATS_INVALID_ARG) && (l.Keys == NULL) && (l.Count == 0));
nats_clearLastError();

test("Get keys with filters (bad args): kv is NULL");
// filters is NULL
s = kvStore_KeysWithFilters(&l, NULL, NULL,defaultSubject,1);
testCond((s == NATS_INVALID_ARG) && (l.Keys == NULL) && (l.Count == 0));
nats_clearLastError();
kvKeysList_Destroy(&l);

test("filter: a.*");
const char **filter1 = (const char *[]){"a.*"};
s = kvStore_KeysWithFilters(&l, kv, NULL,filter1,1);
testCond((s == NATS_OK) && (l.Keys != NULL) && (l.Count == 2));
nats_clearLastError();
kvKeysList_Destroy(&l);

test("filter: *.a.*");
const char **filter2 = (const char *[]){"*.a.*"};
s = kvStore_KeysWithFilters(&l, kv, NULL,filter2,1);
testCond((s == NATS_OK) && (l.Keys != NULL) && (l.Count == 1));
nats_clearLastError();
kvKeysList_Destroy(&l);

test("filter: *.a");
const char **filter3 = (const char *[]){"*.a"};
s = kvStore_KeysWithFilters(&l, kv, NULL,filter3,1);
testCond((s == NATS_OK) && (l.Keys == NULL) && (l.Count == 0));
nats_clearLastError();
kvKeysList_Destroy(&l);

test("filter: e.a.f");
const char **filter4 = (const char *[]){"e.a.f"};
s = kvStore_KeysWithFilters(&l, kv, NULL,filter4,1);
testCond((s == NATS_OK) && (l.Keys != NULL) && (l.Count == 1));
nats_clearLastError();
kvKeysList_Destroy(&l);

test("filter: >");
const char **filter5 = (const char *[]){">"};
s = kvStore_KeysWithFilters(&l, kv, NULL,filter5,1);
testCond((s == NATS_OK) && (l.Keys != NULL) && (l.Count == 5));
nats_clearLastError();
kvKeysList_Destroy(&l);

test("filter: multiple overlapping filters");
const char **filter6 = (const char *[]){"*.a","a.*","*.a.*"};
s = kvStore_KeysWithFilters(&l, kv, NULL,filter6,3);
// consumer subject filters cannot overlap
testCond((s == NATS_ERR) && (l.Keys == NULL) && (l.Count == 0));
nats_clearLastError();
kvKeysList_Destroy(&l);

test("filter: multiple non overlapping filters");
const char **filter7 = (const char *[]){"a.*","e.*"};
s = kvStore_KeysWithFilters(&l, kv, NULL,filter7,2);
testCond((s == NATS_OK) && (l.Keys != NULL) && (l.Count == 3));
nats_clearLastError();
kvKeysList_Destroy(&l);

// Delete the key and check if returned after filtering
test("Delete a.b: ");
s = kvStore_Delete(kv, "a.b");
testCond(s == NATS_OK);

test("a.b should not be returned post deletion")
const char **filter8 = (const char *[]){"a.b"};
s = kvStore_KeysWithFilters(&l, kv, NULL,filter8,1);
testCond((s == NATS_OK) && (l.Keys == NULL) && (l.Count == 0));
nats_clearLastError();
kvKeysList_Destroy(&l);

// Purge the key and check if returned after filtering
test("Purge a.b:");
s = kvStore_Purge(kv, "a.d",NULL);
testCond(s == NATS_OK);

test("a.d should not be returned post purge")
const char **filter9 = (const char *[]){"a.d"};
s = kvStore_KeysWithFilters(&l, kv, NULL,filter9,1);
testCond((s == NATS_OK) && (l.Keys == NULL) && (l.Count == 0));
nats_clearLastError();
kvKeysList_Destroy(&l);

kvStore_Destroy(kv);

JS_TEARDOWN;
}

void test_KeyValueDeleteVsPurge(void)
{
natsStatus s;
Expand Down

0 comments on commit 9f2371d

Please sign in to comment.