Skip to content

Commit

Permalink
Added readme example to test file
Browse files Browse the repository at this point in the history
  • Loading branch information
Attumm committed Nov 23, 2024
1 parent 38018fc commit c7d2a0c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ dic["1"] = "one"
dic["2"] = "two"
dic["3"] = "three"

assert list(dict.keys()) == ["1", "2", "3"]
assert list(dic.keys()) == ["1", "2", "3"]
```

For more information on [extending types](https://github.com/Attumm/redis-dict/blob/main/tests/unit/extend_types_tests.py).
Expand All @@ -357,7 +357,7 @@ redis_config = {
'port': 6380,
}

confid_dic = RedisDict(**redis_config)
config_dic = RedisDict(**redis_config)
```

## Installation
Expand Down
39 changes: 39 additions & 0 deletions tests/misc/tests_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from redis_dict import RedisDict
### Insertion Order
from redis_dict import PythonRedisDict

dic = PythonRedisDict()
dic["1"] = "one"
dic["2"] = "two"
dic["3"] = "three"

assert list(dic.keys()) == ["1", "2", "3"]

### Extending RedisDict with Custom Types
import json

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def encode(self) -> str:
return json.dumps(self.__dict__)

@classmethod
def decode(cls, encoded_str: str) -> 'Person':
return cls(**json.loads(encoded_str))

redis_dict = RedisDict()

# Extend redis dict with the new type
redis_dict.extends_type(Person)

# RedisDict can now seamlessly handle Person instances.
person = Person(name="John", age=32)
redis_dict["person1"] = person

result = redis_dict["person1"]

assert result.name == person.name
assert result.age == person.age

0 comments on commit c7d2a0c

Please sign in to comment.