Skip to content

Commit

Permalink
Test memory encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
EltonCN committed Dec 4, 2024
1 parent 39428e9 commit f8ebb62
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
83 changes: 83 additions & 0 deletions dev/args.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def a(a, b, c):\n",
" return a+b+c\n",
"\n",
"def b(**aargs):\n",
" if \"a\" in aargs:\n",
" del aargs[\"a\"]\n",
"\n",
" return a(a = 3, **aargs)\n",
"\n",
"b(a=1, b=2, c=3)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "a() missing 2 required positional arguments: 'b' and 'c'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[4], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mb\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[1;32mIn[2], line 5\u001b[0m, in \u001b[0;36mb\u001b[1;34m(**aargs)\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mb\u001b[39m(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39maargs):\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43ma\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43maargs\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[1;31mTypeError\u001b[0m: a() missing 2 required positional arguments: 'b' and 'c'"
]
}
],
"source": [
"b(a=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
60 changes: 60 additions & 0 deletions tests/cst_python/memory_storage/test_memory_encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json
import unittest
from typing import Any
import math

from numpy.testing import assert_array_equal

from cst_python.memory_storage.memory_encoder import MemoryEncoder
from cst_python import MemoryObject

class TestMemoryEncoder(unittest.TestCase):

def test_to_dict(self):
memory = MemoryObject()
memory.set_name("MemoryName")
memory.set_info([1,2,3])
memory.set_id(123)
memory.set_evaluation(0.5)


for i in range(2):
if i == 0:
memory_dict = MemoryEncoder.to_dict(memory)

assert_array_equal(memory_dict["I"], [1,2,3])
else:
memory_dict = MemoryEncoder.to_dict(memory, jsonify_info=True)

assert memory_dict["I"] == "[1, 2, 3]"

assert memory_dict["timestamp"] == memory.get_timestamp()
assert math.isclose(memory_dict["evaluation"], 0.5)
assert memory_dict["name"] == "MemoryName"
assert memory_dict["id"] == 123

def test_load_memory(self):
memory = MemoryObject()
memory_dict = {"evaluation": "0.5", "id":"123", "I":"[5, 3, 4]"}

MemoryEncoder.load_memory(memory, memory_dict)

assert memory.get_evaluation() == 0.5
assert memory.get_id() == 123
assert_array_equal(memory.get_info(), [5, 3, 4])

def test_default(self):
memory = MemoryObject()
memory.set_name("MemoryName")
memory.set_info([1,2,3])
memory.set_id(123)
memory.set_evaluation(0.5)

memory_json = json.dumps(memory, cls=MemoryEncoder)
memory_dict = json.loads(memory_json)

assert_array_equal(memory_dict["I"], [1,2,3])
assert memory_dict["timestamp"] == memory.get_timestamp()
assert math.isclose(memory_dict["evaluation"], 0.5)
assert memory_dict["name"] == "MemoryName"
assert memory_dict["id"] == 123

0 comments on commit f8ebb62

Please sign in to comment.