nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

commit 743c7cbfd6dc8354ed8cffb27f58f4a3ec0042bf
parent 48d62971a6c396f1e0dc0e03c58d64932403beb7
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 31 Aug 2023 15:39:25 -0700

test: add test for profile replacement logic

Diffstat:
Mtest.c | 45+++++++++++++++++++++++++++++++++++++++++++++
Atestdata/old-new.json | 2++
2 files changed, 47 insertions(+), 0 deletions(-)

diff --git a/test.c b/test.c @@ -249,6 +249,50 @@ static void test_parse_contact_list() free(buf); } +static void test_replacement() +{ + static const int alloc_size = 1024 * 1024; + char *json = malloc(alloc_size); + unsigned char *buf = malloc(alloc_size); + struct ndb *ndb; + size_t mapsize, len; + int written, ingester_threads; + + mapsize = 1024 * 1024 * 100; + ingester_threads = 1; + assert(ndb_init(&ndb, test_dir, mapsize, ingester_threads)); + + read_file("testdata/old-new.json", (unsigned char*)json, alloc_size, &written); + assert(ndb_process_events(ndb, json, written)); + + ndb_destroy(ndb); + assert(ndb_init(&ndb, test_dir, mapsize, ingester_threads)); + + struct ndb_txn txn; + assert(ndb_begin_query(ndb, &txn)); + + unsigned char pubkey[32] = { 0xb8, 0x2b, 0x25, 0x00, 0x60, 0x47, 0xd9, 0xb3, 0xf7, 0x64, 0x1f, 0x2a, + 0x9c, 0xde, 0x04, 0x07, 0x69, 0x7b, 0xb5, 0x7d, 0x97, 0x93, 0x9a, 0xd1, + 0xd1, 0x49, 0xa7, 0x00, 0xc4, 0x3b, 0x59, 0x74 }; + + void *root = ndb_get_profile_by_pubkey(&txn, pubkey, &len); + + assert(root); + int res = NdbProfileRecord_verify_as_root(root, len); + assert(res == 0); + + NdbProfileRecord_table_t profile_record = NdbProfileRecord_as_root(root); + NdbProfile_table_t profile = NdbProfileRecord_profile_get(profile_record); + const char *name = NdbProfile_name_get(profile); + + assert(!strcmp(name, "jb55")); + + ndb_end_query(&txn); + + free(json); + free(buf); +} + static void test_fetch_last_noteid() { static const int alloc_size = 1024 * 1024; @@ -633,6 +677,7 @@ int main(int argc, const char *argv[]) { // profiles test_load_profiles(); + test_replacement(); printf("All tests passed!\n"); // Print this if all tests pass. } diff --git a/testdata/old-new.json b/testdata/old-new.json @@ -0,0 +1,2 @@ +["EVENT","a",{"id": "35cadc4ddab69648bb6a1d7ea583178e7005594088b14b46a12c32b828692177","pubkey": "b82b25006047d9b3f7641f2a9cde0407697bb57d97939ad1d149a700c43b5974","created_at": 1693519672,"kind": 0,"tags": [],"content": "{}","sig": "34216cf29eae39c1212eec270c173b38736e97be2ddb2025ff55aba65638a73f687f9252e6a9bcf7cfde5ba0329ae177562a24059f03de3ee306877bc22a309b"}] +["EVENT","b",{"id": "80e5308829a48da47dcba7bd3e5a4cf193d3de91146b98721aa39348524ce0e7","pubkey": "b82b25006047d9b3f7641f2a9cde0407697bb57d97939ad1d149a700c43b5974","created_at": 1693519687,"kind": 0,"tags": [],"content": "{\"name\":\"jb55\"}","sig": "b521639be8583e31c9464083ff1b1df3b65b0b4cccc50ac2cee8436ba945376cf5c057204077261314f18c942a89982b7305a77591ae42c05b3670a0e79e00b7"}]