Skip to content

Commit

Permalink
feat(log_data_indexes): Added tests for log_data serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
akhercha committed Oct 30, 2023
1 parent 365cf9c commit 1601e2c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/utils/serializable_dict.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ impl ItemImpl<T> of ItemTrait<T> {
}
}

impl ItemPartialEq<
T, impl TCopy: Copy<T>, impl TPartialEq: PartialEq<T>, impl TDrop: Drop<T>
> of PartialEq<Item<T>> {
fn eq(lhs: @Item<T>, rhs: @Item<T>) -> bool {
if lhs.is_single() && rhs.is_single() {
return lhs.unwrap_single() == rhs.unwrap_single();
} else if lhs.is_span() && rhs.is_span() {
return lhs.unwrap_span() == rhs.unwrap_span();
}
return false;
}
fn ne(lhs: @Item<T>, rhs: @Item<T>) -> bool {
if lhs.is_single() && rhs.is_single() {
return !(lhs.unwrap_single() == rhs.unwrap_single());
} else if lhs.is_span() && rhs.is_span() {
return !(lhs.unwrap_span() == rhs.unwrap_span());
}
return true;
}
}

#[derive(Default, Copy)]
struct SerializableFelt252Dict<T> {
keys: Array<felt252>,
Expand Down Expand Up @@ -212,6 +233,7 @@ impl SerializableFelt252DictTraitImpl<
Option::None => panic_with_felt252('err getting value')
};
let value: Item<T> = Item::Single(value);
d.keys.append(*key);
d.values.insert(*key, nullable_from_box(BoxTrait::new(value)));
continue;
};
Expand All @@ -231,6 +253,7 @@ impl SerializableFelt252DictTraitImpl<
};
// ... & insert it
let values: Item<T> = Item::Span(arr_values.span());
d.keys.append(*key);
d.values.insert(*key, nullable_from_box(BoxTrait::new(values)));
},
Option::None => panic_with_felt252('err getting size')
Expand Down
87 changes: 85 additions & 2 deletions tests/event/test_event_utils.cairo
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
//! Test file for `src/event/event_utils.cairo`.

// *************************************************************************
// IMPORTS
// *************************************************************************
use starknet::{
get_caller_address, ContractAddress, Felt252TryIntoContractAddress, ContractAddressIntoFelt252,
contract_address_const
};
use satoru::event::event_utils::{
Felt252IntoBool, Felt252IntoU128, Felt252IntoI128, Felt252IntoContractAddress, I128252DictValue,
ContractAddressDictValue, LogData
ContractAddressDictValue, LogData, LogDataTrait
};
use satoru::utils::traits::{ContractAddressDefault};
use traits::Default;
use satoru::utils::serializable_dict::{
Item, ItemTrait, SerializableFelt252Dict, SerializableFelt252DictTrait,
SerializableFelt252DictTraitImpl
};

// *********************************************************************************************
// * TEST LOGIC *
// *********************************************************************************************

#[test]
fn test_log_data_default() {
let mut log_data: LogData = Default::default();
Expand Down Expand Up @@ -84,3 +92,78 @@ fn test_log_data_multiple_types() {
assert(out_span.at(1) == arr_to_add.at(1), 'wrong at idx 1');
assert(out_span.at(2) == arr_to_add.at(2), 'wrong at idx 2');
}

#[test]
fn test_log_data_serialization() {
let mut log_data: LogData = Default::default();

log_data.address_dict.insert_single('addr_test', contract_address_const::<42>());
log_data.bool_dict.insert_single('bool_test', false);
log_data.felt252_dict.insert_single('felt_test', 1);
log_data.felt252_dict.insert_single('felt_test_two', 2);
log_data.string_dict.insert_single('string_test', 'hello world');
log_data
.string_dict
.insert_span('string_arr_test', array!['hello', 'world', 'from', 'starknet'].span());

// serialize the data
let mut serialized_data = log_data.serialize_into().span();

// deserialize
let mut d_log_data: LogData = LogDataTrait::deserialize(ref serialized_data)
.expect('err while deserializing');

// Check the values inserted before
// addr dict
let mut expected_dict = log_data.address_dict;
let mut out_dict = d_log_data.address_dict;
assert_same_single_value_for_dicts(ref expected_dict, ref out_dict, 'addr_test');

// bool dict
let mut expected_dict = log_data.bool_dict;
let mut out_dict = d_log_data.bool_dict;
assert_same_single_value_for_dicts(ref expected_dict, ref out_dict, 'bool_test');

// felt252 dict
let mut expected_dict = log_data.felt252_dict;
let mut out_dict = d_log_data.felt252_dict;
assert_same_single_value_for_dicts(ref expected_dict, ref out_dict, 'felt_test');
assert_same_single_value_for_dicts(ref expected_dict, ref out_dict, 'felt_test_two');

// string dict
assert(d_log_data.string_dict.contains('string_arr_test'), 'key not found');
let v: Item<felt252> = d_log_data.string_dict.get('string_arr_test').unwrap();
let span_strings: Span<felt252> = v.unwrap_span();
assert(span_strings.len() == 4, 'err span len');
assert(span_strings.at(0) == @'hello', 'err idx 0');
assert(span_strings.at(1) == @'world', 'err idx 1');
assert(span_strings.at(2) == @'from', 'err idx 2');
assert(span_strings.at(3) == @'starknet', 'err idx 3');
}


// *********************************************************************************************
// * UTILITIES *
// *********************************************************************************************

use debug::PrintTrait;

fn assert_same_single_value_for_dicts<
T,
impl TDefault: Felt252DictValue<T>,
impl TDrop: Drop<T>,
impl TCopy: Copy<T>,
impl FeltIntoT: Into<felt252, T>,
impl TIntoFelt: Into<T, felt252>,
impl TPartialEq: PartialEq<T>,
>(
ref lhs: SerializableFelt252Dict<T>, ref rhs: SerializableFelt252Dict<T>, key: felt252
) {
assert(lhs.contains(key), 'key not found: lhs');
assert(rhs.contains(key), 'key not found: rhs');

let lhs_value: Item<T> = lhs.get(key).unwrap();
let rhs_value: Item<T> = rhs.get(key).unwrap();

assert(lhs_value == rhs_value, 'err value');
}
6 changes: 3 additions & 3 deletions tests/utils/test_serializable_dict.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn test_serializable_dict_insert_span() {
let retrieved_item: Item = dict.get(key).expect('key should be in dict');
let out_span: Span<u128> = retrieved_item.unwrap_span();

assert(dict.keys.contains(key), 'key should be in dict');
assert(dict.contains(key), 'key should be in dict');
assert(out_span.at(0) == expected_array.at(0), 'wrong at idx 0');
assert(out_span.at(1) == expected_array.at(1), 'wrong at idx 1');
assert(out_span.at(2) == expected_array.at(2), 'wrong at idx 2');
Expand All @@ -106,11 +106,11 @@ fn test_serializable_dict_serialize() {
Option::None => panic_with_felt252('err while recreating d')
};

assert(dict.keys.contains('test'), 'key should be in dict');
assert(dict.contains('test'), 'key should be in dict');
let retrieved_item: Item<u128> = dict.get('test').expect('key should be in dict');
let out_value: u128 = retrieved_item.unwrap_single();

assert(dict.keys.contains('test_span'), 'key should be in dict');
assert(dict.contains('test_span'), 'key should be in dict');
let retrieved_item: Item<u128> = deserialized_dict
.get('test_span')
.expect('key should be in dict');
Expand Down

0 comments on commit 1601e2c

Please sign in to comment.