dipscope / TypeManager.TS

Transform JSON strings or plain objects into JS class instances.
https://dipscope.com/type-manager/what-issues-it-solves
Apache License 2.0
25 stars 3 forks source link

Entity Testing #32

Open DellanX opened 1 day ago

DellanX commented 1 day ago

Is your feature request related to a problem? Please describe.

I am now at the point where I am developing unit tests for my codebase and am trying to determine the best way to determine how to unit test my Entity classes. To which, I realize there isn't any documentation for how to do so with this library. This issue is for two tasks:

  1. To define a way to assert that the models are setup properly. I think a simple SerDes process would do just fine.
  2. To document a testing process for developers using this library to validate their models. (I don't mind typing something up).

Describe the solution you'd like

For me, the testing I plan to add is just a simple Serialize Deserialize validation, for this I would do:

  1. I would create a JSON payload such as what my server would transport (Since I am using JSON:API for my codebase)
  2. Then create a test that deserialize the JSON Payload into an Entity and validate that each of its properties were deserialized properly.
  3. Finally do the reverse, creating an Entity, serializing it, and validating it against the JSON payload.

I don't know if the infrastructure exists to interact with the TypeManager in such a way. Especially since the JsonApiEntityProvider is going to be a factor in the Serialization and Deserialization process. But I'd like to get your feedback. Again, I don't mind typing up documentation if desired.

Describe alternatives you've considered

I pretty much have testing for this at the feature level and could leave it like that. That said, I have had issues before that were caused by spelling issues or my declarators being off. It'd be nice to have a way to isolate the problems quickly.

This library already tests itself pretty well, thus the main requirement is to validate the way that I consume this library. I considered using type Metadata for this, but I think that is too low level to be useful, the tests would likely break often for changes unrelated to the application.   If I did really want to work around this, I could also:

Additional context

Testing is usually a hassle that gets put off for too long (I myself a Test Engineer am guilty of this), but it is a key step in ensuring quality code. I figure most users of this library have either developed a test process for their data models or put it off on the backburner.

I also understand if this is too unrelated of an issue, it is a problem I can definitely solve on my own time. Just figured it could be helpful for future developers.

dpimonov commented 1 day ago

Hi DellanX,

Thank you for the precise and detailed description. As I understand, the goal is to test the serialization and deserialization functionality of your JsonApiEntityProvider implementation. In the provider options, you can find the following setting:

    /**
     * Json api fetch interceptor if any.
     * 
     * @type {JsonApiFetchInterceptor}
     */
    jsonApiFetchInterceptor?: JsonApiFetchInterceptor;

For serialization, you can use this setting to bypass actual fetch requests and simply verify whether the serialized document objects are correct. To do this, create an entity, perform the desired operation (add, update, or remove), and use the interceptor to check if the serialized document is accurate. The library includes JSON:API types, so you can ensure type safety here.

For deserialization, you can provide a document object that the library should deserialize and then verify the resulting entities. Use the types to mock a document object. Create a response for the operation you want to test (add, update, or remove) within the interceptor, and check if the resulting entity is correct.

I hope this information is helpful. You're right - there is currently no documentation for these options in the JsonApiEntityProvider. It would be great to add some information about this if you have time.