11"""Tests for the redis orm"""
22from collections import namedtuple
3- from typing import Dict , Any
3+ from typing import Dict , Any , Union
44
55import pytest
66
@@ -234,21 +234,19 @@ def test_select_some_columns(store):
234234 Selecting some columns returns a list of dictionaries of all books models with only those columns
235235 """
236236 Book .insert (books )
237- books_dict = {book .title : book for book in books }
238- columns = ["title" , "author" , "in_stock" ]
237+ columns = ["author" , "in_stock" , "published_on" ]
238+
239+ books_dict = {__make_key_for_book (book ): book for book in books }
239240 response = Book .select (columns = columns )
240- response_dict = {book [ "title" ] : book for book in response }
241+ response_dict = {__make_key_for_book ( book ) : book for book in response }
241242
242- for title , book in books_dict .items ():
243- book_in_response = response_dict [title ]
243+ for k , book in books_dict .items ():
244+ book_in_response = response_dict [k ]
244245 assert isinstance (book_in_response , dict )
245246 assert sorted (book_in_response .keys ()) == sorted (columns )
246247
247248 for column in columns :
248- if column == "author" :
249- assert book_in_response [column ] == getattr (book , column )
250- else :
251- assert f"{ book_in_response [column ]} " == f"{ getattr (book , column )} "
249+ assert f"{ book_in_response [column ]} " == f"{ getattr (book , column )} "
252250
253251
254252@pytest .mark .parametrize ("store" , redis_store_fixture )
@@ -258,7 +256,7 @@ def test_select_some_columns_paginated(store):
258256 skipping `skip` number of models and returning upto `limit` number of items
259257 """
260258 Book .insert (books )
261- columns = ["title " , "author " , "in_stock " ]
259+ columns = ["author " , "in_stock " , "published_on " ]
262260
263261 Record = namedtuple ("Record" , ["skip" , "limit" , "expected" ])
264262 test_data = [
@@ -270,8 +268,8 @@ def test_select_some_columns_paginated(store):
270268 ]
271269 for record in test_data :
272270 response = Book .select (columns = columns , skip = record .skip , limit = record .limit )
273- response_dict = {book [ "title" ] : book for book in response }
274- books_dict = {book . title : book for book in record .expected }
271+ response_dict = {__make_key_for_book ( book ) : book for book in response }
272+ books_dict = {__make_key_for_book ( book ) : book for book in record .expected }
275273 assert len (record .expected ) == len (response_dict )
276274
277275 for title , book in books_dict .items ():
@@ -280,10 +278,7 @@ def test_select_some_columns_paginated(store):
280278 assert sorted (book_in_response .keys ()) == sorted (columns )
281279
282280 for column in columns :
283- if column == "author" :
284- assert book_in_response [column ] == getattr (book , column )
285- else :
286- assert f"{ book_in_response [column ]} " == f"{ getattr (book , column )} "
281+ assert f"{ book_in_response [column ]} " == f"{ getattr (book , column )} "
287282
288283
289284@pytest .mark .parametrize ("store" , redis_store_fixture )
@@ -297,6 +292,28 @@ def test_select_some_ids(store):
297292 assert response == books [:2 ]
298293
299294
295+ @pytest .mark .parametrize ("store" , redis_store_fixture )
296+ def test_select_some_columns_for_some_ids (store ):
297+ """
298+ Selecting some columns for some ids returns only dicts for the given ids with only the given columns
299+ """
300+ columns = ["author" , "in_stock" , "published_on" ]
301+ Book .insert (books )
302+
303+ ids = [book .title for book in books [:2 ]]
304+ books_dict = {__make_key_for_book (book ): book for book in books [:2 ]}
305+ response = Book .select (ids = ids , columns = columns )
306+ response_dict = {__make_key_for_book (book ): book for book in response }
307+
308+ for k , book in books_dict .items ():
309+ book_in_response = response_dict [k ]
310+ assert isinstance (book_in_response , dict )
311+ assert sorted (book_in_response .keys ()) == sorted (columns )
312+
313+ for column in columns :
314+ assert f"{ book_in_response [column ]} " == f"{ getattr (book , column )} "
315+
316+
300317@pytest .mark .parametrize ("store" , redis_store_fixture )
301318def test_update (store ):
302319 """
@@ -419,3 +436,17 @@ def __deserialize_book_data(raw_book_data: Dict[str, Any]) -> Book:
419436
420437 data ["author" ] = Author .select (ids = [author_id ])[0 ]
421438 return Book (** data )
439+
440+
441+ def __make_key_for_book (data : Union [Dict [str , Any ], Book ]):
442+ """Makes a key from a book in case title is not provided"""
443+ author_name = published_on = ""
444+
445+ if isinstance (data , dict ):
446+ author_name = data ["author" ].name
447+ published_on = data ["published_on" ]
448+ elif isinstance (data , Book ):
449+ author_name = data .author .name
450+ published_on = data .published_on
451+
452+ return f"{ author_name } -{ published_on } "
0 commit comments