TEST_F(QuotaTest, usage) { // put quota MojObject obj; MojAssertNoErr( obj.fromJson(_T("{\"owner\":\"com.foo.bar\",\"size\":1000}")) ); MojAssertNoErr( db.putQuotas(&obj, &obj + 1) ); // empty MojInt64 kindUsage = -1; EXPECT_NO_FATAL_FAILURE( getKindUsage(db, _T("Test:1"), kindUsage) ); EXPECT_EQ( 0, kindUsage ) << "Kind without objects should have zero usage"; MojInt64 quotaUsage = -1; EXPECT_NO_FATAL_FAILURE( getQuotaUsage(db, _T("com.foo.bar"), quotaUsage) ); EXPECT_EQ( 0, quotaUsage ) << "Quota without matching objects should have zero usage"; // new obj EXPECT_NO_FATAL_FAILURE( put(db, MojTestKind1Objects[0]) ); MojInt64 kindUsage1 = -1; EXPECT_NO_FATAL_FAILURE( getKindUsage(db, _T("Test:1"), kindUsage1) ); EXPECT_LT( 0, kindUsage1 ) << "Adding new object into kind should increase kind usage"; MojInt64 quotaUsage1 = -1; EXPECT_NO_FATAL_FAILURE( getQuotaUsage(db, _T("com.foo.bar"), quotaUsage1) ); EXPECT_LT( 0, quotaUsage1 ) << "Adding new object matching quota should increase quota usage"; // add prop to existing obj MojAssertNoErr( obj.fromJson(MojTestKind1Objects[0]) ); MojAssertNoErr( obj.put(_T("bar"), 2) ); MojAssertNoErr( db.put(obj, MojDb::FlagForce) ); MojInt64 kindUsage2 = -1; EXPECT_NO_FATAL_FAILURE( getKindUsage(db, _T("Test:1"), kindUsage2) ); EXPECT_LE( 0, kindUsage2 ); EXPECT_LT( kindUsage1, kindUsage2 ) << "Adding property to existing object should increase kind usage"; MojInt64 quotaUsage2 = -1; EXPECT_NO_FATAL_FAILURE( getQuotaUsage(db, _T("com.foo.bar"), quotaUsage2) ); EXPECT_LE( 0, quotaUsage2 ); EXPECT_LT( quotaUsage1, quotaUsage2 ) << "Adding property to existing object that matches quota should increase usage"; // add 2nd obj EXPECT_NO_FATAL_FAILURE( put(db, MojTestKind1Objects[1]) ); MojInt64 kindUsage3 = -1; EXPECT_NO_FATAL_FAILURE( getKindUsage(db, _T("Test:1"), kindUsage3) ); EXPECT_LE( 0, kindUsage3 ); EXPECT_LT( kindUsage2, kindUsage3 ) << "Adding another object should increase kind usage"; MojInt64 quotaUsage3 = -1; EXPECT_NO_FATAL_FAILURE( getQuotaUsage(db, _T("com.foo.bar"), quotaUsage3) ); EXPECT_LE( 0, quotaUsage3 ); EXPECT_LT( quotaUsage2, quotaUsage3 ) << "Adding another object matching to quota should increase usage"; // del first obj bool found = false; MojExpectNoErr( db.del(1, found, MojDb::FlagPurge) ); EXPECT_TRUE( found ) << "Object should be deleted"; MojInt64 kindUsage4 = -1; EXPECT_NO_FATAL_FAILURE( getKindUsage(db, _T("Test:1"), kindUsage4) ); EXPECT_LE( 0, kindUsage4 ); EXPECT_EQ( kindUsage3 - kindUsage2, kindUsage4 ) << "Deletion of object should bring kind usage to expected value"; MojInt64 quotaUsage4 = -1; EXPECT_NO_FATAL_FAILURE( getQuotaUsage(db, _T("com.foo.bar"), quotaUsage4) ); EXPECT_LE( 0, quotaUsage4 ); EXPECT_EQ( quotaUsage3 - quotaUsage2, quotaUsage4 ) << "Deletion of object should bring quota usage to expected value"; // add index MojAssertNoErr( obj.fromJson(MojTestKind1Str2) ); MojExpectNoErr( db.putKind(obj) ); MojInt64 kindUsage5 = -1; EXPECT_NO_FATAL_FAILURE( getKindUsage(db, _T("Test:1"), kindUsage5) ); EXPECT_LE( 0, kindUsage5 ); EXPECT_LT( kindUsage4, kindUsage5 ) << "Adding new index should increase kind usage"; MojInt64 quotaUsage5 = -1; EXPECT_NO_FATAL_FAILURE( getQuotaUsage(db, _T("com.foo.bar"), quotaUsage5) ); EXPECT_LE( 0, quotaUsage5 ); EXPECT_LT( quotaUsage4, quotaUsage5 ) << "Adding new index should increase quota usage"; // update locale MojExpectNoErr( db.updateLocale(_T("FR_fr")) ); MojExpectNoErr( db.updateLocale(_T("EN_us")) ); MojInt64 kindUsage6 = -1; EXPECT_NO_FATAL_FAILURE( getKindUsage(db, _T("Test:1"), kindUsage6) ); EXPECT_LE( 0, kindUsage6 ); EXPECT_EQ( kindUsage5, kindUsage6 ) << "Switching locale forth and back shouldn't affect kind usage"; MojInt64 quotaUsage6 = -1; EXPECT_NO_FATAL_FAILURE( getQuotaUsage(db, _T("com.foo.bar"), quotaUsage6) ); EXPECT_LE( 0, kindUsage6 ); EXPECT_EQ( quotaUsage5, quotaUsage6 ) << "Switching locale forth and back shouldn't affect quota usage"; // drop index MojAssertNoErr( obj.fromJson(MojTestKind1Str1) ); MojExpectNoErr( db.putKind(obj) ); MojInt64 kindUsage7 = -1; EXPECT_NO_FATAL_FAILURE( getKindUsage(db, _T("Test:1"), kindUsage7) ); EXPECT_LE( 0, kindUsage7 ); EXPECT_EQ( kindUsage4, kindUsage7 ) << "Dropping of index should bring kind usage to expected value"; MojInt64 quotaUsage7 = -1; EXPECT_NO_FATAL_FAILURE( getQuotaUsage(db, _T("com.foo.bar"), quotaUsage7) ); EXPECT_LE( 0, quotaUsage7 ); EXPECT_EQ( quotaUsage4, quotaUsage7 ) << "Dropping of index should bring quota usage to expected value"; // drop kind MojString kindStr; MojAssertNoErr( kindStr.assign(_T("Test:1")) ); MojExpectNoErr( db.delKind(kindStr, found) ); EXPECT_TRUE( found ) << "Kind should be deleted"; MojInt64 kindUsage8 = -1; EXPECT_NO_FATAL_FAILURE( getKindUsage(db, _T("Test:1"), kindUsage8) ); EXPECT_EQ( 0, kindUsage8 ) << "Dropping of kind should bring its usage to zero"; MojInt64 quotaUsage8 = -1; EXPECT_NO_FATAL_FAILURE( getQuotaUsage(db, _T("com.foo.bar"), quotaUsage8) ); EXPECT_EQ( 0, quotaUsage8 ) << "Dropping of kind that matches quota should bring quota usage to zero"; MojExpectNoErr( db.quotaStats(obj) ); MojString statStr; MojExpectNoErr( obj.toJson(statStr) ); std::cerr << "quotaStats: " << statStr.data() << std::endl; }
MojErr MojDbQuotaTest::testUsage(MojDb& db) { // put quota MojObject obj; MojErr err = obj.fromJson(_T("{\"owner\":\"com.foo.bar\",\"size\":1000}")); MojErrCheck(err); err = db.putQuotas(&obj, &obj + 1); MojErrCheck(err); // empty MojInt64 kindUsage = 0; err = getKindUsage(db, _T("Test:1"), kindUsage); MojTestErrCheck(err); MojTestAssert(kindUsage == 0); MojInt64 quotaUsage = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage); MojTestErrCheck(err); MojTestAssert(quotaUsage == 0); // new obj err = put(db, MojTestKind1Objects[0]); MojTestErrCheck(err); MojInt64 kindUsage1 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage1); MojTestErrCheck(err); MojTestAssert(kindUsage1 > 0); MojInt64 quotaUsage1 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage1); MojTestErrCheck(err); MojTestAssert(quotaUsage1 > 0); // add prop to existing obj err = obj.fromJson(MojTestKind1Objects[0]); MojTestErrCheck(err); err = obj.put(_T("bar"), 2); MojTestErrCheck(err); err = db.put(obj, MojDb::FlagForce); MojTestErrCheck(err); MojInt64 kindUsage2 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage2); MojTestErrCheck(err); MojTestAssert(kindUsage2 > kindUsage1); MojInt64 quotaUsage2 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage2); MojTestErrCheck(err); MojTestAssert(quotaUsage2 > quotaUsage1); // add 2nd obj err = put(db, MojTestKind1Objects[1]); MojTestErrCheck(err); MojInt64 kindUsage3 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage3); MojTestErrCheck(err); MojTestAssert(kindUsage3 > kindUsage2); MojInt64 quotaUsage3 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage3); MojTestErrCheck(err); MojTestAssert(quotaUsage3 > quotaUsage2); // del first obj bool found = false; err = db.del(1, found, MojDb::FlagPurge); MojTestErrCheck(err); MojTestAssert(found); MojInt64 kindUsage4 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage4); MojTestErrCheck(err); MojTestAssert(kindUsage4 == kindUsage3 - kindUsage2); MojInt64 quotaUsage4 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage4); MojTestErrCheck(err); MojTestAssert(quotaUsage4 == quotaUsage3 - quotaUsage2); // add index err = obj.fromJson(MojTestKind1Str2); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrCheck(err); MojInt64 kindUsage5 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage5); MojTestErrCheck(err); MojTestAssert(kindUsage5 > kindUsage4); MojInt64 quotaUsage5 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage5); MojTestErrCheck(err); MojTestAssert(quotaUsage5 > quotaUsage4); // update locale err = db.updateLocale(_T("FR_fr")); MojTestErrCheck(err); err = db.updateLocale(_T("EN_us")); MojTestErrCheck(err); MojInt64 kindUsage6 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage6); MojTestErrCheck(err); MojTestAssert(kindUsage6 == kindUsage5); MojInt64 quotaUsage6 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage6); MojTestErrCheck(err); MojTestAssert(quotaUsage6 == quotaUsage5); // drop index err = obj.fromJson(MojTestKind1Str1); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrCheck(err); MojInt64 kindUsage7 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage7); MojTestErrCheck(err); MojTestAssert(kindUsage7 == kindUsage4); MojInt64 quotaUsage7 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage7); MojTestErrCheck(err); MojTestAssert(quotaUsage7 == quotaUsage4); // drop kind MojString kindStr; err = kindStr.assign(_T("Test:1")); MojTestErrCheck(err); err = db.delKind(kindStr, found); MojTestErrCheck(err); MojTestAssert(found); MojInt64 kindUsage8 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage8); MojTestErrCheck(err); MojTestAssert(kindUsage8 == 0); MojInt64 quotaUsage8 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage8); MojTestErrCheck(err); MojTestAssert(quotaUsage8 == 0); err = db.quotaStats(obj); MojErrCheck(err); return MojErrNone; }