Skip to main content

๐Ÿงช Testing

Coppermind provides a mock mode for testing your data logic without hitting real DataStores.


โšก Enabling Mock Modeโ€‹

-- Enable mock mode
Coppermind.setMockMode(true)

-- Check if mock mode is enabled
if Coppermind.isMockMode() then
print("Running in mock mode")
end

-- Disable mock mode
Coppermind.setMockMode(false)

๐Ÿ”ฎ Mock Mode Behaviorโ€‹

What Mock Mode Does

In mock mode, all data operations are simulated in memory!

FeatureBehavior
๐Ÿ’พ StorageData stored in memory
โšก SpeedOperations complete nearly instantly
๐Ÿ“Š Rate LimitsNo rate limiting or budget concerns
๐Ÿ”’ Session LockingStill works (simulated)
๐ŸŽฏ Use CasesUnit tests & local development

๐Ÿ—‘๏ธ Clearing Mock Dataโ€‹

Reset all mock data between tests:

Coppermind.clearMockData()

This clears:

  • โœ… All stored mock data
  • โœ… All mock escrow/transaction data

๐Ÿงช Testing Patternsโ€‹

๐Ÿ—๏ธ Basic Test Setupโ€‹

local function runTests()
-- Enable mock mode
Coppermind.setMockMode(true)
Coppermind.clearMockData()

-- Run tests...
testDataOperations()
testTransactions()
testMigrations()

-- Cleanup
Coppermind.setMockMode(false)
end

local function testDataOperations()
local testKey = "test_player"

-- Load store
local store = Coppermind.loadStore(PlayerSchema, testKey, {})
task.wait(0.2) -- Wait for async load

-- Verify initial data
local data = Coppermind.getData(PlayerSchema, testKey)
assert(data.coins == 0, "Initial coins should be 0")

-- Test update
Coppermind.updateData(PlayerSchema, testKey, function(d)
d.coins = 100
return nil
end)

data = Coppermind.getData(PlayerSchema, testKey)
assert(data.coins == 100, "Coins should be 100 after update")

-- Cleanup
Coppermind.unloadStore(PlayerSchema, testKey)
task.wait(0.1)

print("โœ“ Data operations test passed")
end

โณ Waiting for Async Operationsโ€‹

Async Operations

Mock mode operations are still asynchronous. Always wait properly!

local store = Coppermind.loadStore(schema, key, {})
task.wait(0.2)

๐Ÿ“‹ Complete Test Suite Exampleโ€‹

local function runTestSuite()
print("Starting Coppermind test suite...")

Coppermind.setMockMode(true)

local tests = {
{ name = "Data Operations", fn = testDataOperations },
{ name = "Persistence", fn = testPersistence },
{ name = "Transactions", fn = testTransactions },
{ name = "Failed Transactions", fn = testFailedTransaction },
{ name = "Events", fn = testEvents },
}

local passed = 0
local failed = 0

for _, test in tests do
Coppermind.clearMockData()

local success, err = pcall(test.fn)

if success then
passed += 1
else
failed += 1
warn(`โœ— {test.name}: {err}`)
end
end

Coppermind.setMockMode(false)

print(`\nResults: {passed}/{passed + failed} tests passed`)
end

โœ… Testing Checklistโ€‹

Test TypeWhat to Verify
๐Ÿ“ Data OperationsRead, write, update cycles work correctly
๐Ÿ’พ PersistenceData survives save/load cycles
๐Ÿ” TransactionsAtomic operations commit/rollback properly
โŒ Failure CasesErrors are handled gracefully
๐Ÿ“ข EventsAll lifecycle events fire correctly
๐Ÿ”„ MigrationsOld data formats upgrade properly