-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4704d3c
commit 60b3633
Showing
5 changed files
with
195 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
local state = require("state") | ||
local startTimestamp = 0 | ||
|
||
|
||
local json = require("json") -- Assuming json library for encoding/decoding | ||
|
||
describe("State loading functionality", function() | ||
-- Sample data for testing | ||
local validJson, invalidJson, emptyJson | ||
local startTimestamp = os.time() | ||
|
||
-- Setup function to initialize test data | ||
before_each(function() | ||
validJson = json.encode({ | ||
records = { key1 = { processId = 123, value = "Record1" } }, | ||
gateways = { key2 = { details = "Gateway1" } }, | ||
observations = { key3 = { observation = "Observation1" } }, | ||
distributions = { distribution1 = "Data1" }, | ||
demandFactoring = { demandKey = "Demand1" } | ||
}) | ||
end) | ||
|
||
-- Tests for each specific scenario | ||
it("should correctly load state from valid JSON data", function() | ||
local reply, err = state.loadState(validJson, startTimestamp) | ||
assert.is_nil(err) | ||
assert.is_true((string.find(reply, "Records Updated: 1")) > 0) | ||
assert.is_true((string.find(reply, "Gateways Updated: 1")) > 0) | ||
assert.is_true((string.find(reply, "Observations Updated: 1")) > 0) | ||
assert.is_true((string.find(reply, "Distributions Updated: 1")) > 0) | ||
assert.is_true((string.find(reply, "Demand Updated: 1")) > 0) | ||
end) | ||
|
||
it("should handle partial state update", function() | ||
local partialJson = json.encode({ records = { key1 = { processId = 123, value = "Record2" } } }) | ||
local reply, err = state.loadState(partialJson, startTimestamp) | ||
|
||
assert.is_nil(err) | ||
assert.is_true((string.find(reply, "Records Updated: 1")) > 0) | ||
Records = {} | ||
end) | ||
|
||
|
||
it("should handle JSON with missing expected fields", function() | ||
local incompleteJson = json.encode({ records = 100, gateways = "gateways" }) -- No processId | ||
local reply, err = state.loadState(incompleteJson, startTimestamp) | ||
|
||
assert.is_false(reply) | ||
assert.are.same("The 'records' field is missing or not in the expected format.", err) | ||
end) | ||
|
||
after_each(function() | ||
-- Reset or clear global or shared variables if necessary | ||
Records = nil | ||
Gateways = nil | ||
Observations = nil | ||
Distributions = nil | ||
Demand = nil | ||
end) | ||
end) |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,115 @@ | ||
local state = { _version = '0.0.0' } | ||
local demand = require('demand') | ||
local json = require('json') | ||
|
||
Name = Name or "Test IO" | ||
Ticker = Ticker or "tIO" | ||
Logo = Logo or "Sie_26dvgyok0PZD_-iQAFOhOd5YxDTkczOLoqTTL_A" | ||
|
||
if not Denomination then | ||
Denomination = 6 | ||
end | ||
LastStateLoad = LastStateLoad or nil | ||
|
||
if not Balances then | ||
Balances = {} | ||
end | ||
Balances = Balances or {} | ||
Records = Records or {} | ||
Auctions = Auctions or {} | ||
Reserved = Reserved or {} | ||
Denomination = Denomination or 6 | ||
Gateways = Gateways or {} | ||
Vaults = Vaults or {} | ||
PrescribedObservers = PrescribedObservers or {} | ||
Observations = Observations or {} | ||
Distributions = Distributions or {} | ||
Demand = Demand or {} | ||
|
||
if not Gateways then | ||
Gateways = {} | ||
end | ||
function state.loadState(data, currentTimestamp) | ||
local data, err = json.decode(data) | ||
if not data or err then | ||
-- Handle error (e.g., send an error response) | ||
return false, "Error decoding JSON data" | ||
end | ||
|
||
if not Vaults then | ||
Vaults = {} | ||
end | ||
-- Counter for added or updated records. | ||
local recordsAddedOrUpdated = 0 | ||
-- Ensure 'data.records' is present and iterate through the decoded data to update the Records table accordingly. | ||
if data.records then | ||
if type(data.records) == "table" then | ||
for key, value in pairs(data.records) do | ||
-- Preserve the existing processId if the record already exists. | ||
local existingProcessId = Records[key] and Records[key].processId | ||
|
||
if not PrescribedObservers then | ||
PrescribedObservers = {} | ||
end | ||
-- Check if the record either doesn't exist or differs from the new value. | ||
if not Records[key] or (Records[key] and json.encode(Records[key]) ~= json.encode(value)) then | ||
recordsAddedOrUpdated = recordsAddedOrUpdated + 1 | ||
Records[key] = value | ||
Records[key].processId = existingProcessId or value.processId -- Preserve or initialize processId. | ||
end | ||
end | ||
else | ||
-- Handle the case where 'data.records' is not in the expected format. | ||
return false, "The 'records' field is missing or not in the expected format." | ||
end | ||
end | ||
|
||
if not Observations then | ||
Observations = {} | ||
end | ||
local gatewaysAddedOrUpdated = 0 | ||
if data.gateways then | ||
if type(data.gateways) == "table" then | ||
for key, value in pairs(data.gateways) do | ||
if not Gateways[key] or (Gateways[key] and json.encode(Gateways[key]) ~= json.encode(value)) then | ||
gatewaysAddedOrUpdated = gatewaysAddedOrUpdated + 1 | ||
Gateways[key] = value | ||
end | ||
end | ||
else | ||
-- Handle the case where 'data.gateways' is not in the expected format. | ||
return false, "The 'gateways' field is missing or not in the expected format." | ||
end | ||
end | ||
|
||
if not Distributions then | ||
Distributions = {} | ||
end | ||
local observationsAddedOrUpdated = 0 | ||
if data.observations then | ||
if type(data.observations) == "table" then | ||
for key, value in pairs(data.observations) do | ||
if not Observations[key] or (Observations[key] and json.encode(Observations[key]) ~= json.encode(value)) then | ||
observationsAddedOrUpdated = observationsAddedOrUpdated + 1 | ||
Observations[key] = value | ||
end | ||
end | ||
else | ||
-- Handle the case where 'data.observations' is not in the expected format. | ||
return false, "The 'gatewobservationsays' field is missing or not in the expected format." | ||
end | ||
end | ||
|
||
local distributionsUpdated = 0 | ||
if data.distributions then | ||
if type(data.distributions) == "table" then | ||
Distributions = data.distributions | ||
distributionsUpdated = distributionsUpdated + 1 | ||
else | ||
-- Handle the case where 'data.distributions' is not in the expected format. | ||
return false, "The 'distributions' field is missing or not in the expected format." | ||
end | ||
end | ||
|
||
local demandUpdated = 0 | ||
if data.demandFactoring then | ||
if type(data.demandFactoring) == "table" then | ||
Demand = data.demandFactoring | ||
demandUpdated = demandUpdated + 1 | ||
else | ||
-- Handle the case where 'data.demandFactoring' is not in the expected format. | ||
return false, "The 'demandFactoring' field is missing or not in the expected format." | ||
end | ||
end | ||
|
||
LastStateLoad = currentTimestamp | ||
|
||
if not Demand then | ||
Demand = demand | ||
return "Records Updated: " .. | ||
recordsAddedOrUpdated .. | ||
" Gateways Updated: " .. | ||
gatewaysAddedOrUpdated .. | ||
" Observations Updated: " .. | ||
observationsAddedOrUpdated .. | ||
" Distributions Updated: " .. distributionsUpdated .. " Demand Updated: " .. demandUpdated | ||
end | ||
|
||
return state |